@neabyte/chart-to-image
Version:
Convert trading charts to images using Node.js canvas with advanced features: 6 chart types, VWAP/EMA/SMA indicators, custom colors, themes, hide elements, scaling, and PNG/JPEG export formats.
63 lines (62 loc) • 1.96 kB
JavaScript
import ccxt from 'ccxt';
export class DataProvider {
exchange;
config;
constructor(config) {
this.config = config;
this.exchange = this.createExchange();
}
createExchange() {
const exchangeClass = ccxt[this.config.name];
if (!exchangeClass) {
throw new Error(`Unsupported exchange: ${this.config.name}`);
}
const exchange = new exchangeClass({
apiKey: this.config.apiKey,
secret: this.config.secret,
sandbox: this.config.sandbox || false,
enableRateLimit: true
});
return exchange;
}
async fetchOHLCV(symbol, timeframe, limit = 100) {
try {
await this.exchange.loadMarkets();
const ohlcv = await this.exchange.fetchOHLCV(symbol, timeframe, undefined, limit);
return ohlcv.map((candle) => {
const candleArray = candle;
return {
timestamp: candleArray[0],
open: candleArray[1],
high: candleArray[2],
low: candleArray[3],
close: candleArray[4],
volume: candleArray[5]
};
});
}
catch (error) {
throw new Error(`Failed to fetch data: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
getExchangeInfo() {
return {
name: this.config.name,
sandbox: this.config.sandbox,
markets: this.exchange.markets,
timeframes: this.exchange.timeframes
};
}
async isSymbolSupported(symbol) {
try {
await this.exchange.loadMarkets();
return symbol in this.exchange.markets;
}
catch {
return false;
}
}
getSupportedTimeframes() {
return Object.keys(this.exchange.timeframes || {});
}
}