get_market_data
Retrieve comprehensive cryptocurrency market data for trading pairs on Binance spot or futures markets to analyze prices and trends.
Instructions
Get comprehensive market data for a trading pair
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Trading pair symbol (e.g., BTCUSDT) | |
| type | Yes | Market type |
Implementation Reference
- src/connectors/binance-rest.ts:38-133 (handler)Core handler function that fetches spot market data or comprehensive futures data (including ticker, open interest, funding rate, mark price, and recent liquidations) from Binance REST API endpoints using axios with exponential backoff retry logic.public async getMarketData(symbol: string, type: 'spot' | 'futures'): Promise<any> { try { logger.info(`Getting ${type} market data for ${symbol}`); if (type === 'spot') { const data = await this.executeWithRetry(() => this.axiosInstance.get(`${config.SPOT_REST_URL}/ticker/24hr`, { params: { symbol: symbol.toUpperCase() } }).then(response => response.data) ); logger.info('Successfully fetched spot market data'); return data; } // For futures, fetch all relevant data in parallel logger.info('Fetching futures data from multiple endpoints...'); try { const [ marketData, openInterest, fundingData, liquidations ] = await Promise.all([ // Basic market data this.executeWithRetry(() => this.axiosInstance.get(`${config.FUTURES_REST_URL}/ticker/24hr`, { params: { symbol: symbol.toUpperCase() } }).then(response => { logger.info('Successfully fetched futures ticker data'); return response.data; }) ), // Open interest this.executeWithRetry(() => this.axiosInstance.get(`${config.FUTURES_REST_URL}/openInterest`, { params: { symbol: symbol.toUpperCase() } }).then(response => { logger.info('Successfully fetched open interest data'); return response.data; }) ), // Premium index (funding rate) this.executeWithRetry(() => this.axiosInstance.get(`${config.FUTURES_REST_URL}/premiumIndex`, { params: { symbol: symbol.toUpperCase() } }).then(response => { logger.info('Successfully fetched funding rate data'); return response.data; }) ), // Recent liquidations this.executeWithRetry(() => this.axiosInstance.get(`${config.FUTURES_REST_URL}/forceOrders`, { params: { symbol: symbol.toUpperCase(), startTime: Date.now() - 24 * 60 * 60 * 1000, limit: 100 } }).then(response => { logger.info('Successfully fetched liquidations data'); return response.data; }) ) ]); logger.info('Successfully fetched all futures data, combining responses...'); // Combine all futures data with correct field mappings const combinedData = { ...marketData, openInterest: openInterest.openInterest, fundingRate: fundingData.lastFundingRate, markPrice: fundingData.markPrice, nextFundingTime: fundingData.nextFundingTime, liquidations24h: liquidations.length, liquidationVolume24h: liquidations.reduce((sum: number, order: any) => sum + parseFloat(order.executedQty), 0 ) }; logger.info('Successfully combined futures data'); return combinedData; } catch (error) { logger.error('Error in futures data Promise.all:', error); throw error; } } catch (error) { logger.error('Error fetching market data:', error); throw new APIError('Failed to fetch market data', error as Error); } }
- src/index.ts:47-65 (registration)MCP tool registration including name, description, and input schema definition for get_market_data.{ name: "get_market_data", description: "Get comprehensive market data for a trading pair", inputSchema: { type: "object", properties: { symbol: { type: "string", description: "Trading pair symbol (e.g., BTCUSDT)" }, type: { type: "string", enum: ["spot", "futures"], description: "Market type" } }, required: ["symbol", "type"] } },
- src/index.ts:171-183 (handler)Top-level MCP tool handler that validates input parameters using type guard and delegates to the BinanceRestConnector's getMarketData method, returning JSON-formatted response.case "get_market_data": { if (!isMarketDataParams(request.params.arguments)) { throw new Error('Invalid market data parameters'); } const { symbol, type } = request.params.arguments; const data = await restConnector.getMarketData(symbol, type); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; }
- src/types/api-types.ts:1-37 (schema)TypeScript interface and type guard for validating get_market_data input parameters.export interface MarketDataParams { symbol: string; type: 'spot' | 'futures'; } export interface KlineParams { symbol: string; type: 'spot' | 'futures'; interval: string; limit?: number; } export interface StreamParams { symbol: string; type: 'spot' | 'futures'; streams: string[]; } export interface FuturesDataParams { symbol: string; } export class APIError extends Error { constructor(message: string, public readonly cause?: Error) { super(message); this.name = 'APIError'; } } // Type guards export function isMarketDataParams(params: any): params is MarketDataParams { return ( typeof params === 'object' && typeof params.symbol === 'string' && (params.type === 'spot' || params.type === 'futures') ); }