import { BitfinexApi , CandleStick } from 'ccex-api' ; import { Bar , ErrorCallback , HistoryCallback , IBasicDataFeed , LibrarySymbolInfo , OnReadyCallback , ResolveCallback , SearchSymbolsCallback , SubscribeBarsCallback , ResolutionString , } from '../lib/charting_library/charting_library.min' ; const corsProxy = 'https://api.exchangecompare.com/' ; const pair = 'btc_usd' ; export class Datafeed implements IBasicDataFeed { private exchangeApi : BitfinexApi ; private initialLastCandle : CandleStick | null = null ; constructor ( ) { this . exchangeApi = new BitfinexApi ( { corsProxy } ) ; } searchSymbols ( userInput , exchange , symbolType , onResult ) { throw new Error ( 'Method not implemented.' ) ; } resolveSymbol ( symbolName , onResolve , onError ) { const symbolData = { name : pair , full_name : pair , exchange : 'bitfinex' , listed_exchange : pair , timezone : < TradingView . Timezone > 'Asia/Tokyo' , minmov : 1 , pricescale : 100 , session : '24x7' , has_intraday : true , has_no_volume : false , ticker : pair , description : 'bitfinex' , type : 'bitcoin' , supported_resolutions : [ '1' , '5' , '15' , '30' , '60' , '120' , 'D' ] , } ; setTimeout ( ( ) => { onResolve ( symbolData ) ; } ) ; } getBars ( symbolInfo , resolution , rangeStartDate , rangeEndDate , onResult , onError , isFirstCall ) { const minutesFoot = resolutionToMinutes ( resolution ) ; this . exchangeApi . fetchCandleStickRange$ ( pair , minutesFoot , rangeStartDate * 1000 , rangeEndDate * 1000 ) . subscribe ( ( candlesticks ) => { if ( isFirstCall && candlesticks && candlesticks . length ) { this . initialLastCandle = candlesticks [ candlesticks . length - 1 ] ; } const bars = candlesticks . map ( adaptCandlestickToBar ) ; const noData = isFirstCall || ( bars && bars . length > 0 ) ? false : true ; onResult ( bars , { noData } ) ; } ) ; } subscribeBars ( symbolInfo , resolution , onTick , listenerGuid , onResetCacheNeededCallback ) { console . log ( 'subscribeBars' ) ; const minutesFoot = resolutionToMinutes ( resolution ) ; if ( this . initialLastCandle ) { this . exchangeApi . lastCandle$ ( pair , this . initialLastCandle , minutesFoot ) . subscribe ( ( lastCandle ) => { const updatedLastBar = adaptCandlestickToBar ( lastCandle ) ; onTick ( updatedLastBar ) ; } ) ; } } unsubscribeBars ( listenerGuid ) { console . log ( 'unsubscribeBars' ) ; } onReady ( callback ) { setTimeout ( callback ) ; } } function resolutionToMinutes ( res ) { const resStringMinuteMap = { 'D' : 1440 , '1D' : 1440 , } ; const minutes = resStringMinuteMap [ res ] || Number ( res ) ; if ( ! minutes ) { throw new Error ( `Resolution is not recognized ${ res }` ) ; } return minutes ; } function adaptCandlestickToBar ( candlestick ) { return { ... candlestick , time : candlestick . timestamp } ; }