币安交易集成服务

提供与币安现货和期货交易操作的全面集成服务,支持多种订单类型、杠杆设置和风险管理功能。

  • by kydlikebtc
  • TypeScript
  • 本地部署
  • 14 星标
  • 更新于
在 GitHub 查看 kydlikebtc/mcp-server-bn

使用说明

mcp服务器cex-bn

此MCP服务器提供与币安现货和期货交易业务的全面集成。

中文说明

特性

现货交易操作

  • 执行现货交易操作(限价/市价订单)
  • 监控账户余额
  • 跟踪和管理未结订单
  • 取消现有订单

期货交易业务

  • 创建各种类型的期货订单(LIMIT、MARKET、STOP、TAKE_PROFIT等)
  • 管理杠杆设置(1-125x)
  • 监控期货头寸和账户信息
  • 跟踪融资率
  • 支持单向和对冲模式头寸
  • 高级订单类型,包括尾随止损和仅减少订单

工具

API配置

configure_api_keys

安全存储您的Binance API凭证:

await configureBinanceApiKeys({
  apiKey: 'your-api-key',
  apiSecret: 'your-api-secret'
});

现货交易工具

create_spot_order

创建限价或市价订单:

// LIMIT order
await createSpotOrder({
  symbol: 'BTCUSDT',
  side: 'BUY',
  type: 'LIMIT',
  quantity: '0.001',
  price: '40000'
});

// MARKET order
await createSpotOrder({
  symbol: 'BTCUSDT',
  side: 'BUY',
  type: 'MARKET',
  quantity: '0.001'
});
cancel_order

取消现有订单:

await cancelOrder({
  symbol: 'BTCUSDT',
  orderId: '12345678'
});
get_balances

检查您的账户余额:

const balances = await getBalances();
// Returns: { BTC: '0.1', USDT: '1000', ... }
get_open_orders

列出所有未结订单:

const orders = await getOpenOrders({
  symbol: 'BTCUSDT' // Optional: specify symbol
});

期货交易工具

create_futures_order

创建各种类型的期货订单:

// LIMIT order
await createFuturesOrder({
  symbol: 'BTCUSDT',
  side: 'BUY',
  type: 'LIMIT',
  quantity: '0.001',
  price: '40000',
  timeInForce: 'GTC'
});

// STOP MARKET order
await createFuturesOrder({
  symbol: 'BTCUSDT',
  side: 'SELL',
  type: 'STOP_MARKET',
  quantity: '0.001',
  stopPrice: '38000'
});

// TRAILING STOP order
await createFuturesOrder({
  symbol: 'BTCUSDT',
  side: 'SELL',
  type: 'TRAILING_STOP_MARKET',
  quantity: '0.001',
  callbackRate: '1.0' // 1% callback rate
});
set_futures_leverage

调整交易对的杠杆:

await setFuturesLeverage({
  symbol: 'BTCUSDT',
  leverage: 10  // 1-125x
});
get_futures_positions

获取所有未平仓期货头寸:

const positions = await getFuturesPositions();
get_futures_account

获取详细的期货账户信息:

const account = await getFuturesAccount();
get_funding_rate

获取期货交易品种的资金利率:

const fundingRate = await getFundingRate({
  symbol: 'BTCUSDT'
});
cancel_futures_order

取消现有期货订单:

await cancelFuturesOrder({
  symbol: 'BTCUSDT',
  orderId: '12345678'
});

期货交易详情

位置模式

  • 单向模式:每个符号一个位置
    • 默认模式,更简单的职位管理
    • 总头寸大小是所有订单的总和
  • 对冲模式:分开多头和空头头寸
    • 允许同时持有多头和空头头寸
    • 每个头寸都有独立的保证金要求

保证金类型

  • 独立保证金:每个头寸的固定保证金
    • 风险仅限于分配的保证金
    • 每个头寸都有自己的杠杆设置
  • 交叉保证金:头寸之间的共享保证金
    • 更高的资本效率
    • 所有头寸共同承担风险

融资利率

永续期货合约使用融资利率使期货价格与现货价格保持一致:

  • 正利率:多头支付空头
  • 负利率:空头付多头
  • 每8小时付款一次

安全考虑

现货交易安全

  • 永远不要将API密钥提交到版本控制
  • 使用环境变量或安全密钥存储
  • 将API密钥权限仅限于所需的操作
  • 定期旋转API键

期货交易安全

  • 根据风险承受能力设置适当的杠杆限额
  • 始终使用止损单来限制潜在损失
  • 仔细监控清算价格
  • 定期检查头寸风险和保证金比率
  • 考虑仅使用reduce订单进行风险管理
  • 由于风险分担,对交叉保证金持谨慎态度

速率限制

  • 遵守Binance API费率限制
  • 默认速率限制:
    • 每分钟1200个订单操作请求
    • 每秒100次市场数据请求
  • 对速率限制错误实施适当的错误处理

错误处理

常见错误场景

  • API凭据无效
  • 余额或保证金不足
  • 订单参数无效
  • 请求频率超限
  • 网络连接问题

期货特定错误

  • 保证金不足错误:保证金不足,无法操作
  • InvalidPositionMode错误:位置模式设置错误
  • OrderValidation错误:期货订单参数无效

错误处理示例:

try {
  await createFuturesOrder({
    symbol: 'BTCUSDT',
    side: 'BUY',
    type: 'LIMIT',
    quantity: '0.001',
    price: '40000',
    timeInForce: 'GTC'
  });
} catch (error) {
  if (error instanceof InsufficientMarginError) {
    console.error('Insufficient margin available');
  } else if (error instanceof InvalidPositionModeError) {
    console.error('Invalid position mode');
  } else if (error instanceof OrderValidationError) {
    console.error('Invalid order parameters');
  }
}

项目结构

.
├── src/
│   ├── index.ts                 # Server entry point
│   ├── services/
│   │   ├── binance.ts          # Binance API integration
│   │   ├── keystore.ts         # API key management
│   │   └── tools.ts            # Trading tools implementation
│   └── types/
│       ├── binance.ts          # Binance types
│       └── binance-connector.d.ts  # API client types
├── README.md
├── README_CN.md
├── package.json
├── pnpm-lock.yaml
└── tsconfig.json

发展

  1. 设置环境变量:

创建 .env 文件,并设置您的Binance API凭据:

BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_secret_key_here
  1. 安装依赖项:
pnpm install

构建服务器:

pnpm build

对于自动重建的开发:

pnpm watch

安装

通过Smithery安装

通过以下方式自动安装适用于Claude Desktop的币安交易服务器 史密瑟里:

npx -y @smithery/cli install mcp-server-cex-bn --client claude

手动安装

  1. 克隆存储库
  2. 安装依赖项:
pnpm install
  1. 在中配置您的Binance API凭据 .env
  2. 构建并启动服务器:
pnpm build
pnpm start

调试

由于MCP服务器通过stdio进行通信,调试可能具有挑战性。我们建议使用 MCP检查员,可作为包脚本使用:

pnpm inspector

检查器将提供一个URL,用于访问浏览器中的调试工具。

mcp服务器bn