Skip to main content
Glama
fetchOhlcvData.js1.03 kB
const ccxt = require("ccxt"); // Initialize ccxt client const exid = process.env.EXCHANGE_NAME || "binance" const exchange = new ccxt[exid]({ enableRateLimit: true, }); // Reusable function to fetch OHLCV data and return an Asset object async function fetchOhlcvData(symbol, timeframe, limit) { try { if (!exchange.has["fetchOHLCV"]) { throw new Error(`${exid} does not support fetchOHLCV`); } const ohlcv = await exchange.fetchOHLCV(symbol, timeframe, undefined, limit); return { dates: ohlcv.map((row) => new Date(row[0])), // Timestamps as Date objects openings: ohlcv.map((row) => row[1]), // Open prices highs: ohlcv.map((row) => row[2]), // High prices lows: ohlcv.map((row) => row[3]), // Low prices closings: ohlcv.map((row) => row[4]), // Close prices (required) volumes: ohlcv.map((row) => row[5]), // Volumes }; } catch (error) { throw new Error(`Failed to fetch OHLCV data: ${error.message}`); } } module.exports = fetchOhlcvData;

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/kukapay/crypto-indicators-mcp'

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