import * as ccxt from 'ccxt' ; import * as moment from 'moment' ; import { logger } from '../../logger' ; export interface ExchangeConfig { name : string ; } export interface OHLCV { time : number ; open : number ; high : number ; low : number ; close : number ; volume : number ; } export class export class Exchange { private exchange : ccxt . Exchange ; private marketsInfo : ccxt . Market [ ] ; constructor ( public config ) { this . exchange = < ccxt . Exchange > new ( < any > ccxt ) [ this . config . name ] ( { apiKey : '' , secret : '' , timeout : 30000 , enableRateLimit : true , } ) ; } param { string } symbol returns { Promise < Market > } memberof public async getExchangeInfo ( symbol ) { if ( ! this . marketsInfo ) this . marketsInfo = await this . exchange . fetchMarkets ( ) ; const markets = this . marketsInfo . filter ( market => market . symbol === symbol ) ; if ( markets . length !== 1 ) return undefined ; return markets [ 0 ] ; } param { string } symbol param { { limit : number ; since ? : number } } opts returns { Promise < OHLCV [ ] > } memberof public async getCandles ( symbol , opts ) { if ( ! this . exchange . fetchOHLCV ) { throw new Error ( `[Exchange] ${ this . config . name } doesn't have fetchOHLCV method` ) ; } try { const limit = opts . limit || 1 ; const since = opts . since || moment ( ) . subtract ( limit , 'm' ) . toDate ( ) . getTime ( ) ; const candles = await this . exchange . fetchOHLCV ( symbol , '1m' , since , limit ) ; return candles . map ( ( [ T , O , H , L , C , V ] ) => < OHLCV > { time : T , open : O , high : H , low : L , close : C , volume : V , } ) ; } catch ( error ) { logger . error ( error ) ; throw new Error ( `[Exchange] ${ this . config . name } doesn't have fetchOHLCV method` ) ; } } }