Skip to main content
Glama

fetchOHLCV

Retrieve OHLCV candlestick data for cryptocurrency trading pairs from exchanges to analyze market trends and price movements.

Instructions

Fetch OHLCV candlestick data for a symbol on an exchange

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
exchangeIdYesExchange ID (e.g., 'binance', 'coinbase')
symbolYesTrading symbol (e.g., 'BTC/USDT')
timeframeNoTimeframe (e.g., '1m', '5m', '1h', '1d')1h
sinceNoTimestamp in ms to fetch data since (optional)
limitNoLimit the number of candles returned (optional)

Implementation Reference

  • Registers the 'fetchOHLCV' tool on the MCP server, including description, input schema, and inline handler function.
      server.tool(
        "fetchOHLCV",
        "Fetch OHLCV candlestick data 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')"),
          timeframe: z.string().default("1h").describe("Timeframe (e.g., '1m', '5m', '1h', '1d')"),
          since: z.number().optional().describe("Timestamp in ms to fetch data since (optional)"),
          limit: z.number().optional().describe("Limit the number of candles returned (optional)")
        },
        async ({ exchangeId, symbol, timeframe, since, limit }) => {
          try {
            // 공개 인스턴스 사용
            const exchange = ccxtServer.getPublicExchangeInstance(exchangeId);
    
            // 거래소가 OHLCV 데이터를 지원하는지 확인
            if (!exchange.has['fetchOHLCV']) {
              return {
                content: [
                  {
                    type: "text",
                    text: `Exchange ${exchangeId} does not support OHLCV data fetching`
                  }
                ],
                isError: true
              };
            }
            
            const ohlcv = await exchange.fetchOHLCV(symbol, timeframe, since, limit);
            
            return {
              content: [
                {
                  type: "text",
                  text: JSON.stringify(ohlcv, null, 2)
                }
              ]
            };
          } catch (error) {
            return {
              content: [
                {
                  type: "text",
                  text: `Error fetching OHLCV data: ${(error as Error).message}`
                }
              ],
              isError: true
            };
          }
        }
      );
    }
  • The handler function that implements the core logic: retrieves public CCXT exchange instance, checks support for fetchOHLCV, fetches the data, and returns JSON stringified OHLCV array or error.
      async ({ exchangeId, symbol, timeframe, since, limit }) => {
        try {
          // 공개 인스턴스 사용
          const exchange = ccxtServer.getPublicExchangeInstance(exchangeId);
    
          // 거래소가 OHLCV 데이터를 지원하는지 확인
          if (!exchange.has['fetchOHLCV']) {
            return {
              content: [
                {
                  type: "text",
                  text: `Exchange ${exchangeId} does not support OHLCV data fetching`
                }
              ],
              isError: true
            };
          }
          
          const ohlcv = await exchange.fetchOHLCV(symbol, timeframe, since, limit);
          
          return {
            content: [
              {
                type: "text",
                text: JSON.stringify(ohlcv, null, 2)
              }
            ]
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error fetching OHLCV data: ${(error as Error).message}`
              }
            ],
            isError: true
          };
        }
      }
    );
  • Zod schema for input validation: exchangeId (string), symbol (string), timeframe (string, default '1h'), since (number optional), limit (number optional).
    {
      exchangeId: z.string().describe("Exchange ID (e.g., 'binance', 'coinbase')"),
      symbol: z.string().describe("Trading symbol (e.g., 'BTC/USDT')"),
      timeframe: z.string().default("1h").describe("Timeframe (e.g., '1m', '5m', '1h', '1d')"),
      since: z.number().optional().describe("Timestamp in ms to fetch data since (optional)"),
      limit: z.number().optional().describe("Limit the number of candles returned (optional)")
    },
  • src/server.ts:372-372 (registration)
    Top-level call to registerMarketTools in CcxtMcpServer's registerTools method, which includes registration of fetchOHLCV.
    registerMarketTools(this.server, this);

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