fme-quotes-oanda
Version:
This takes the oanda interface and streams quotes using the fme-quote format
154 lines (153 loc) • 6.76 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fme_logger_1 = require("fme-logger");
var L = new fme_logger_1.Log("onada-quotes");
L.setLevel("debug");
const OandaAdapter = require("oanda-adapter");
const fme_quotes_models_1 = require("fme-quotes-models");
const _m = require("moment-timezone");
class OandaController {
constructor(environment, accessToken, username, accountId) {
this.environment = environment;
this.accessToken = accessToken;
this.username = username;
this.accountId = accountId;
this.subscribers = [];
this.instruments = [];
this.streamStarted = false;
this.availableSymbols = [];
this.addSymbol = (symbol) => {
// this.client.subscribePrice(this.accountId,symbol,this.streamOut);
};
this.connect = () => {
L.info("connect");
this.client = new OandaAdapter({
environment: this.environment,
accessToken: this.accessToken,
username: this.username
});
};
this.convertSymbol = (symbolIn) => {
var symbol = this.availableSymbols.find((a) => { return a.vendorSymbol === symbolIn; });
if (symbol) {
return symbol.symbol;
}
L.error("convertSymbol", symbolIn, this.availableSymbols);
return "N/F";
};
this.convertSymbol2FmeSymbol = (symbolIn) => {
var symbolOut = new fme_quotes_models_1.FmeSymbol();
symbolOut.symbol = symbolIn.displayName.replace("_", "-");
symbolOut.name = symbolIn.instrument;
symbolOut.decimalPoints = this.numberOfDecimals(symbolIn.pip);
symbolOut.id = symbolIn.instrument;
symbolOut.intervals = [];
symbolOut.isStreaming = true;
symbolOut.source = "gdax";
symbolOut.vendorSymbol = symbolIn.instrument;
return symbolOut;
};
this.convertTicker = (quote) => {
var fmeQuoteRt = new fme_quotes_models_1.FmeQuoteRt();
fmeQuoteRt.symbol = quote.instrument.replace("_", "-");
fmeQuoteRt.bid = quote.bid;
fmeQuoteRt.ask = quote.ask;
fmeQuoteRt.last = quote.ask;
fmeQuoteRt.timestamp = _m(quote.time).toDate();
fmeQuoteRt.source = "OANDA";
fmeQuoteRt.decimalPoints = this.getDecimalPoints(quote.instrument);
return fmeQuoteRt;
};
this.delay = (seconds) => __awaiter(this, void 0, void 0, function* () {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
return true;
}), seconds * 1000);
});
this.getDecimalPoints = (instrument) => {
var dp = this.availableSymbols.find((a) => { return a.vendorSymbol == instrument; });
if (dp)
return dp.decimalPoints;
else
return 2;
};
this.getSymbols = () => __awaiter(this, void 0, void 0, function* () {
L.debug("Processing getSymbols");
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
L.debug("Trying getInstruments", this.client);
this.client.getInstruments(this.accountId, (error, data) => {
this.instruments = data;
for (var i in this.instruments) {
var symbol = this.instruments[i];
this.availableSymbols.push(this.convertSymbol2FmeSymbol(symbol));
}
resolve(this.instruments.length);
this.startStream(null);
});
}
catch (err) {
L.error("getSymbols", err);
reject(err);
return;
}
}));
});
this.setDecimalPoints = () => {
for (var i in this.instruments) {
var instrument = this.instruments[i];
instrument.decimalPoints = this.numberOfDecimals(instrument.pip);
}
};
this.subscribe = (clientName, clientFunction) => {
var idx = this.subscribers.findIndex((a) => { return a.name == clientName; });
if (idx == -1) {
L.debug("New subscriber", clientName);
this.subscribers.push({ name: clientName, callBack: clientFunction });
}
else {
L.debug("Updating subscriber", clientName);
this.subscribers[idx] = { name: clientName, callBack: clientFunction };
}
};
this.streamOut = (data) => {
var q = this.convertTicker(data);
for (var i in this.subscribers) {
this.subscribers[i].callBack(q);
}
};
this.startStream = (symbols) => __awaiter(this, void 0, void 0, function* () {
if (this.streamStarted)
return;
this.streamStarted = true;
for (var i in this.instruments) {
this.client.subscribePrice(this.accountId, this.instruments[i].instrument, this.streamOut);
}
});
this.stopStream = () => {
};
this.unSubscribe = (clientName) => {
var idx = this.subscribers.findIndex((a) => { return a.name == clientName; });
if (idx == -1) {
L.error("Unsubscribe - Client does not exist", clientName);
}
else {
this.subscribers.slice(idx, 1);
}
};
}
numberOfDecimals(number) {
var index = number.indexOf(".");
if (index == -1)
return 0;
return number.length - index - 1;
}
}
exports.OandaController = OandaController;