@neabyte/candlestick-cli
Version:
Beautiful terminal candlestick charts with real-time trading data. Create stunning ASCII art charts directly in your terminal with support for live market data, custom colors, and professional trading visualization.
1 lines • 2.91 kB
JavaScript
import*as ccxt from"ccxt";import{MarketDataError}from"../types/errors.js";export class CCXTProvider{exchange;constructor(){this.exchange=new ccxt.binance({enableRateLimit:!0,options:{defaultType:"future"}})}async fetchOHLCV(e="BTC/USDT",t="1h",r=1e3){try{this.validateInputs(e,t,r),await this.exchange.loadMarkets();const a=await this.exchange.fetchOHLCV(e,t,void 0,r);this.validateOHLCVData(a,e,t);const o=this.processOHLCVData(a,e,t);return this.convertToCandles(o)}catch(r){this.handleFetchError(r,e,t)}}validateInputs(e,t,r){if(!e||"string"!=typeof e)throw new MarketDataError("Invalid symbol provided",e,t);if(!t||"string"!=typeof t)throw new MarketDataError("Invalid timeframe provided",e,t);if("number"!=typeof r||r<=0||r>1e4)throw new MarketDataError(`Invalid limit provided: ${r}. Must be between 1 and 10000`,e,t)}validateOHLCVData(e,t,r){if(!Array.isArray(e)||0===e.length)throw new MarketDataError("No market data received from exchange",t,r)}processOHLCVData(e,t,r){return e.map((e,a)=>{const[o,i,n,s,c,h]=e,l={timestamp:Number(o)||0,open:Number(i)||0,high:Number(n)||0,low:Number(s)||0,close:Number(c)||0,volume:Number(h)||0};return this.validateOHLCData(l,a,t,r),l})}validateOHLCData(e,t,r,a){if(e.high<Math.max(e.open,e.close))throw new MarketDataError(`Invalid OHLC data at index ${t}: high < max(open, close)`,r,a);if(e.low>Math.min(e.open,e.close))throw new MarketDataError(`Invalid OHLC data at index ${t}: low > min(open, close)`,r,a)}convertToCandles(e){return e.map(e=>({open:e.open,high:e.high,low:e.low,close:e.close,volume:e.volume,timestamp:e.timestamp,type:e.open<e.close?1:0}))}handleFetchError(e,t,r){if(e instanceof MarketDataError)throw e;throw e instanceof Error&&this.handleSpecificError(e,t,r),new MarketDataError(`Failed to fetch market data: ${e instanceof Error?e.message:"Unknown error"}`,t,r)}handleSpecificError(e,t,r){if(e.message.includes("symbol"))throw new MarketDataError(`Invalid symbol: ${t}`,t,r);if(e.message.includes("timeframe"))throw new MarketDataError(`Invalid timeframe: ${r}`,t,r);if(e.message.includes("rate limit"))throw new MarketDataError("Rate limit exceeded, please try again later",t,r);if(e.message.includes("network"))throw new MarketDataError("Network error, please check your connection",t,r);throw new MarketDataError(`Failed to fetch market data: ${e.message}`,t,r)}async fetch4H(e="BTC/USDT",t=500){return this.fetchOHLCV(e,"4h",t)}async fetch1D(e="BTC/USDT",t=200){return this.fetchOHLCV(e,"1d",t)}async getLatestPrice(e="BTC/USDT"){try{return(await this.exchange.fetchTicker(e)).last||0}catch(e){throw globalThis.console.error("Error fetching latest price:",e),e}}async getMarketInfo(e="BTC/USDT"){try{await this.exchange.loadMarkets();const t=this.exchange.market(e);return{symbol:t.symbol,base:t.base,quote:t.quote,precision:t.precision,limits:t.limits}}catch(e){throw globalThis.console.error("Error fetching market info:",e),e}}}