Skip to main content
Glama

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
NameRequiredDescriptionDefault
exchangeIdYesExchange ID (e.g., 'binance', 'coinbase')
symbolYesTrading symbol (e.g., 'BTC/USDT')
sinceNoTimestamp in ms to fetch trades since (optional)
limitNoLimit the number of trades returned (optional)

Implementation Reference

  • 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
        };
      }
    }
  • 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)")
    },
  • 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);
  • 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];
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/lazy-dinosaur/ccxt-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server