UNPKG

fme-poloniex

Version:

API for fetching data from the Poloniex API

80 lines (59 loc) 2.32 kB
import {Log} from "fme-logger"; var L = new Log("poloniex"); import * as _m from "moment-timezone"; import {FmeQuote} from "fme-quotes-models"; import * as Poloniex from "poloniex-api-node"; type PoloniexQuote = { "date": number, //unix time stamp "high": number, "low": number, "open": number, "close": number, "volume": number, "quoteVolume": number, "weightedAverage": number } export class PoloniexQuotes { version = "1.0.0"; poloniex:any; constructor() { this.poloniex = new Poloniex(); } getQuotes = async (symbol:string, minutes:number=5, startTime=_m().startOf("day").toDate(),endTime=_m().toDate()) => { var seconds = minutes*60; var start = _m(startTime).unix(); var end = _m(endTime).unix(); var pQuotes = [] as PoloniexQuote[]; L.debug("Calling poloniex for chartdate with data",symbol, seconds, start,end); try { pQuotes = await this.poloniex.returnChartData(symbol, seconds, start, end) as PoloniexQuote[]; } catch (err) { L.error("GetQuotes returend error",err,symbol,minutes,startTime,endTime); } L.debug("returnChartDate returned",pQuotes.length); var fQuotes = [] as FmeQuote[]; for (var i in pQuotes) { fQuotes.push(this.convert(symbol,minutes,pQuotes[i])); } return fQuotes; } convert = (symbol:string, minutes:number, pQuote:PoloniexQuote) => { var fQuote = new FmeQuote(); fQuote.YYYYMMDD = _m.unix(pQuote.date).format("YYYYMMDD"); fQuote.timeHHMM = _m.unix(pQuote.date).format("HHmm"); fQuote.time = _m.unix(pQuote.date).hours()*60+ _m(pQuote.date).minutes(); fQuote.timestamp = _m.unix(pQuote.date).toDate(); fQuote.symbol = symbol; fQuote.open = pQuote.open; fQuote.high = pQuote.high; fQuote.low = pQuote.low; fQuote.close = pQuote.close; fQuote.avg = pQuote.weightedAverage; fQuote.volume = pQuote.volume; fQuote.source = "Poloniex"; fQuote.decimalPoints = 2; fQuote.interval = minutes; fQuote.type = "minutes"; return fQuote; } }