get_candlesticks
Retrieve historical candlestick data for cryptocurrency trading instruments from OKX exchange to analyze price movements and market trends over specified time intervals.
Instructions
Get candlestick data for an OKX instrument
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instrument | Yes | Instrument ID (e.g. BTC-USDT) | |
| bar | No | Time interval (e.g. 1m, 5m, 1H, 1D) | 1m |
| limit | No | Number of candlesticks (max 100) |
Implementation Reference
- src/index.ts:182-224 (handler)Handler implementation for the 'get_candlesticks' tool. Fetches candlestick data from OKX API using axios, processes the response, and returns formatted JSON with timestamps.} else { // get_candlesticks console.error(`[API] Fetching candlesticks for instrument: ${args.instrument}, bar: ${args.bar || '1m'}, limit: ${args.limit || 100}`); const response = await this.axiosInstance.get<OKXCandlesticksResponse>( '/market/candles', { params: { instId: args.instrument, bar: args.bar || '1m', limit: args.limit || 100 }, } ); if (response.data.code !== '0') { throw new Error(`OKX API error: ${response.data.msg}`); } if (!response.data.data || response.data.data.length === 0) { throw new Error('No data returned from OKX API'); } return { content: [ { type: 'text', text: JSON.stringify( response.data.data.map(([time, open, high, low, close, vol, volCcy]) => ({ timestamp: new Date(parseInt(time)).toISOString(), open, high, low, close, volume: vol, volumeCurrency: volCcy })), null, 2 ), }, ], }; }
- src/index.ts:98-121 (schema)Input schema definition for the 'get_candlesticks' tool, specifying parameters for instrument, bar interval, and limit.{ name: 'get_candlesticks', description: 'Get candlestick data for an OKX instrument', inputSchema: { type: 'object', properties: { instrument: { type: 'string', description: 'Instrument ID (e.g. BTC-USDT)', }, bar: { type: 'string', description: 'Time interval (e.g. 1m, 5m, 1H, 1D)', default: '1m' }, limit: { type: 'number', description: 'Number of candlesticks (max 100)', default: 100 } }, required: ['instrument'], }, },
- src/index.ts:82-123 (registration)Registration of available tools in ListToolsRequestSchema handler, including 'get_candlesticks'.this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'get_price', description: 'Get latest price for an OKX instrument', inputSchema: { type: 'object', properties: { instrument: { type: 'string', description: 'Instrument ID (e.g. BTC-USDT)', }, }, required: ['instrument'], }, }, { name: 'get_candlesticks', description: 'Get candlestick data for an OKX instrument', inputSchema: { type: 'object', properties: { instrument: { type: 'string', description: 'Instrument ID (e.g. BTC-USDT)', }, bar: { type: 'string', description: 'Time interval (e.g. 1m, 5m, 1H, 1D)', default: '1m' }, limit: { type: 'number', description: 'Number of candlesticks (max 100)', default: 100 } }, required: ['instrument'], }, }, ], }));
- src/index.ts:30-42 (helper)TypeScript interface defining the structure of OKX candlesticks API response, used in the handler.interface OKXCandlesticksResponse { code: string; msg: string; data: Array<[ time: string, // Open time open: string, // Open price high: string, // Highest price low: string, // Lowest price close: string, // Close price vol: string, // Trading volume volCcy: string // Trading volume in currency ]>; }