validate_order_quantity
Ensure orders comply with Bybit trading rules by validating and formatting order quantities based on category, symbol, and target amount for accurate trade execution.
Instructions
Validate order quantity against Bybit trading rules and get properly formatted quantity
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| category | Yes | Category (spot, linear, inverse, etc.) | |
| currentPrice | No | Current price (optional, will fetch if not provided) | |
| symbol | Yes | Symbol (e.g., BTCUSDT) | |
| targetAmount | Yes | Target amount in quote currency (e.g., 80 for $80 worth) |
Implementation Reference
- src/bybit-service.ts:966-1052 (handler)Core handler function that validates order quantity: fetches ticker and instrument info, calculates quantity from target amount, adjusts to exchange lot size rules (min, max, step), formats correctly, returns validated qty, cost, and adjustments.async validateOrderQuantity( category: string, symbol: string, targetAmount: number, currentPrice?: number ): Promise<{ isValid: boolean; validatedQty: string; estimatedCost: string; adjustments: string[]; error?: string; }> { try { // Get current price if not provided let price = currentPrice; if (!price) { const ticker = await this.getTickers(category, symbol); if ('error' in ticker) { return { isValid: false, validatedQty: '0', estimatedCost: '0', adjustments: [], error: `Failed to get price: ${ticker.error}` }; } price = parseFloat(ticker.result.list[0].lastPrice); } // Get instrument info const instrumentInfo = await this.getInstrumentsInfo(category, symbol); if ('error' in instrumentInfo) { return { isValid: false, validatedQty: '0', estimatedCost: '0', adjustments: [], error: `Failed to get instrument info: ${instrumentInfo.error}` }; } const instrument = instrumentInfo.result.list[0]; const lotSizeFilter = instrument.lotSizeFilter; const minOrderQty = parseFloat(lotSizeFilter.minOrderQty); const maxOrderQty = parseFloat(lotSizeFilter.maxOrderQty); const qtyStep = parseFloat(lotSizeFilter.qtyStep); const adjustments: string[] = []; let rawOrderQty = targetAmount / price; // Round to the nearest valid quantity step let orderQty = Math.round(rawOrderQty / qtyStep) * qtyStep; // Ensure it meets minimum requirements if (orderQty < minOrderQty) { orderQty = minOrderQty; adjustments.push(`Adjusted to minimum quantity: ${orderQty}`); } // Ensure it doesn't exceed maximum if (orderQty > maxOrderQty) { orderQty = maxOrderQty; adjustments.push(`Adjusted to maximum quantity: ${orderQty}`); } // Format to the correct decimal places based on qtyStep const decimalPlaces = qtyStep.toString().split('.')[1]?.length || 0; const validatedQty = orderQty.toFixed(decimalPlaces); const estimatedCost = (orderQty * price).toFixed(2); return { isValid: true, validatedQty, estimatedCost, adjustments, }; } catch (error: any) { return { isValid: false, validatedQty: '0', estimatedCost: '0', adjustments: [], error: error.message }; } }
- src/index.ts:486-507 (schema)JSON Schema defining the input parameters for the validate_order_quantity tool.inputSchema: { type: 'object', properties: { category: { type: 'string', description: 'Category (spot, linear, inverse, etc.)', }, symbol: { type: 'string', description: 'Symbol (e.g., BTCUSDT)', }, targetAmount: { type: 'number', description: 'Target amount in quote currency (e.g., 80 for $80 worth)', }, currentPrice: { type: 'number', description: 'Current price (optional, will fetch if not provided)', }, }, required: ['category', 'symbol', 'targetAmount'], },
- src/index.ts:955-970 (registration)Tool handler registration in the MCP switch statement that dispatches calls to the BybitService.validateOrderQuantity method.case 'validate_order_quantity': { const result = await this.bybitService.validateOrderQuantity( typedArgs.category, typedArgs.symbol, typedArgs.targetAmount, typedArgs.currentPrice ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/index.ts:483-508 (registration)Tool registration in the ListTools response, including name, description, and schema.{ name: 'validate_order_quantity', description: 'Validate order quantity against Bybit trading rules and get properly formatted quantity', inputSchema: { type: 'object', properties: { category: { type: 'string', description: 'Category (spot, linear, inverse, etc.)', }, symbol: { type: 'string', description: 'Symbol (e.g., BTCUSDT)', }, targetAmount: { type: 'number', description: 'Target amount in quote currency (e.g., 80 for $80 worth)', }, currentPrice: { type: 'number', description: 'Current price (optional, will fetch if not provided)', }, }, required: ['category', 'symbol', 'targetAmount'], }, },