place_order
Execute buy or sell orders on Binance exchange using market or limit order types with specified trading pairs and quantities.
Instructions
下单交易 - 支持主网和测试网(主网将使用真实资金)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| price | No | 价格,LIMIT订单必需 | |
| quantity | Yes | 数量 | |
| side | Yes | 买卖方向 | |
| symbol | Yes | 交易对符号,如 BTCUSDT | |
| type | Yes | 订单类型 |
Implementation Reference
- src/tools/trading.ts:53-104 (handler)The core handler function for the 'place_order' tool. It validates the input using PlaceOrderSchema, constructs the order parameters for the Binance API, executes the order, handles errors with handleBinanceError, and returns a detailed order result object.handler: async (binanceClient: any, args: unknown) => { const networkMode = validateAndWarnMainnet(); const input = validateInput(PlaceOrderSchema, args); validateSymbol(input.symbol); validateQuantity(input.quantity); if (input.type === 'LIMIT' && !input.price) { throw new Error('Price is required for LIMIT orders'); } if (input.price) { validatePrice(input.price); } try { const orderParams: any = { symbol: input.symbol, side: input.side, type: input.type, quantity: input.quantity, }; if (input.type === 'LIMIT' && input.price) { orderParams.price = input.price; orderParams.timeInForce = 'GTC'; } const orderResult = await binanceClient.order(orderParams); return { symbol: orderResult.symbol, orderId: orderResult.orderId, orderListId: orderResult.orderListId, clientOrderId: orderResult.clientOrderId, transactTime: orderResult.transactTime, price: orderResult.price, origQty: orderResult.origQty, executedQty: orderResult.executedQty, cummulativeQuoteQty: orderResult.cummulativeQuoteQty, status: orderResult.status, timeInForce: orderResult.timeInForce, type: orderResult.type, side: orderResult.side, fills: orderResult.fills || [], timestamp: Date.now(), network: networkMode, }; } catch (error) { handleBinanceError(error); } },
- src/types/mcp.ts:33-39 (schema)Zod schema definition for PlaceOrder input validation, defining properties for symbol, side, type, quantity, and optional price with descriptions.export const PlaceOrderSchema = z.object({ symbol: z.string().describe('交易对符号'), side: z.enum(['BUY', 'SELL']).describe('买卖方向'), type: z.enum(['MARKET', 'LIMIT']).describe('订单类型'), quantity: z.string().describe('数量'), price: z.string().optional().describe('价格,LIMIT单必需'), });
- src/tools/trading.ts:22-105 (registration)Registration of the 'place_order' tool in the tradingTools array export. Includes name, description, JSON inputSchema matching the Zod schema, and references the handler function. This array is imported and used in server.ts for MCP tool setup.{ name: 'place_order', description: '下单交易 - 支持主网和测试网(主网将使用真实资金)', inputSchema: { type: 'object', properties: { symbol: { type: 'string', description: '交易对符号,如 BTCUSDT', }, side: { type: 'string', enum: ['BUY', 'SELL'], description: '买卖方向', }, type: { type: 'string', enum: ['MARKET', 'LIMIT'], description: '订单类型', }, quantity: { type: 'string', description: '数量', }, price: { type: 'string', description: '价格,LIMIT订单必需', }, }, required: ['symbol', 'side', 'type', 'quantity'], }, handler: async (binanceClient: any, args: unknown) => { const networkMode = validateAndWarnMainnet(); const input = validateInput(PlaceOrderSchema, args); validateSymbol(input.symbol); validateQuantity(input.quantity); if (input.type === 'LIMIT' && !input.price) { throw new Error('Price is required for LIMIT orders'); } if (input.price) { validatePrice(input.price); } try { const orderParams: any = { symbol: input.symbol, side: input.side, type: input.type, quantity: input.quantity, }; if (input.type === 'LIMIT' && input.price) { orderParams.price = input.price; orderParams.timeInForce = 'GTC'; } const orderResult = await binanceClient.order(orderParams); return { symbol: orderResult.symbol, orderId: orderResult.orderId, orderListId: orderResult.orderListId, clientOrderId: orderResult.clientOrderId, transactTime: orderResult.transactTime, price: orderResult.price, origQty: orderResult.origQty, executedQty: orderResult.executedQty, cummulativeQuoteQty: orderResult.cummulativeQuoteQty, status: orderResult.status, timeInForce: orderResult.timeInForce, type: orderResult.type, side: orderResult.side, fills: orderResult.fills || [], timestamp: Date.now(), network: networkMode, }; } catch (error) { handleBinanceError(error); } }, },