fetchTrades
Retrieve recent cryptocurrency trades from exchanges to analyze market activity and trading patterns for specific trading pairs.
Instructions
Fetch recent trades for a symbol on an exchange
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| exchangeId | Yes | Exchange ID (e.g., 'binance', 'coinbase') | |
| symbol | Yes | Trading symbol (e.g., 'BTC/USDT') | |
| since | No | Timestamp in ms to fetch trades since (optional) | |
| limit | No | Limit the number of trades returned (optional) |
Implementation Reference
- src/tools/market-tools.ts:167-192 (handler)The handler function for the 'fetchTrades' tool. It retrieves a public CCXT exchange instance, calls fetchTrades with the provided parameters, and returns the trades as JSON or an error message.async ({ exchangeId, symbol, since, limit }) => { try { // 공개 인스턴스 사용 const exchange = ccxtServer.getPublicExchangeInstance(exchangeId); const trades = await exchange.fetchTrades(symbol, since, limit); return { content: [ { type: "text", text: JSON.stringify(trades, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error fetching trades: ${(error as Error).message}` } ], isError: true }; } }
- src/tools/market-tools.ts:161-166 (schema)Input schema for the 'fetchTrades' tool using Zod, defining parameters: exchangeId (required), symbol (required), since (optional number), limit (optional number).{ exchangeId: z.string().describe("Exchange ID (e.g., 'binance', 'coinbase')"), symbol: z.string().describe("Trading symbol (e.g., 'BTC/USDT')"), since: z.number().optional().describe("Timestamp in ms to fetch trades since (optional)"), limit: z.number().optional().describe("Limit the number of trades returned (optional)") },
- src/tools/market-tools.ts:158-193 (registration)Registration of the 'fetchTrades' tool on the MCP server, including name, description, input schema, and inline handler function.server.tool( "fetchTrades", "Fetch recent trades for a symbol on an exchange", { exchangeId: z.string().describe("Exchange ID (e.g., 'binance', 'coinbase')"), symbol: z.string().describe("Trading symbol (e.g., 'BTC/USDT')"), since: z.number().optional().describe("Timestamp in ms to fetch trades since (optional)"), limit: z.number().optional().describe("Limit the number of trades returned (optional)") }, async ({ exchangeId, symbol, since, limit }) => { try { // 공개 인스턴스 사용 const exchange = ccxtServer.getPublicExchangeInstance(exchangeId); const trades = await exchange.fetchTrades(symbol, since, limit); return { content: [ { type: "text", text: JSON.stringify(trades, null, 2) } ] }; } catch (error) { return { content: [ { type: "text", text: `Error fetching trades: ${(error as Error).message}` } ], isError: true }; } } );
- src/server.ts:372-372 (registration)Call to registerMarketTools which includes the registration of fetchTrades among other market tools.registerMarketTools(this.server, this);
- src/server.ts:321-356 (helper)Helper method used by fetchTrades to obtain a public (unauthenticated) CCXT exchange instance for the specified exchange.getPublicExchangeInstance( exchangeId: string, marketType: "spot" | "futures" = "spot", ): Exchange { const instanceKey = `${exchangeId}-${marketType}`; if (!this.publicExchangeInstances[instanceKey]) { if (!ccxt.exchanges.includes(exchangeId)) { console.error( `Exchange ID '${exchangeId}' not found in ccxt.exchanges for public instance.`, ); throw new Error(`Unsupported exchange for public data: ${exchangeId}`); } const exchangeOptions = { options: { defaultType: marketType, }, }; try { // @ts-ignore - CCXT dynamic instantiation without credentials this.publicExchangeInstances[instanceKey] = new ccxt[exchangeId]( exchangeOptions, ); } catch (error) { console.error( `Failed to create public CCXT instance for ${exchangeId} (${marketType}):`, error, ); throw error; // Re-throw the error after logging } } return this.publicExchangeInstances[instanceKey]; }