UNPKG

tradingview-api-adapter

Version:

API Adapter for real-time market data as quoted prices and symbol ticker details from Tradingview

92 lines 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QuoteSessionAdapter = void 0; const events_1 = require("events"); class QuoteSessionAdapter extends events_1.EventEmitter { constructor($bridge, $isMultiChannel = false) { super(); this.$bridge = $bridge; this.$isMultiChannel = $isMultiChannel; this.firstLoad = true; this.$bridge.listener(params => this.handleSessionListener(params)); this.launchSession(); } setFields(fieldList) { if (typeof fieldList === 'string') { this.$bridge.send("quote_set_fields", [fieldList]); } else { this.$bridge.send("quote_set_fields", Array.from(fieldList)); } } addPairs(pairList) { if (typeof pairList === 'string') { this.$bridge.send('quote_add_symbols', [pairList]); } else { this.$bridge.send('quote_add_symbols', Array.from(pairList)); } // this.$bridge.send('quote_add_symbols', [`={"adjustment":"splits","symbol":"${(pairList[0])}"}`]) // this.$client.send("quote_fast_symbols", [this.sessionID, `={"adjustment":"splits","symbol":"${(pairs[0])}"}`]) } removePairs(pairList) { if (typeof pairList === 'string') { this.$bridge.send('quote_remove_symbols', [pairList]); } else { this.$bridge.send('quote_remove_symbols', Array.from(pairList)); } } /** End section of the protected code **/ /** Section of the system code **/ launchSession() { this.$bridge.send("quote_create_session", []); } handleSessionListener(sessionData) { let quotedData = {}; /*!!!!currently implemented check for a session with a single quote*/ sessionData.params.forEach(param => { switch (param.s) { case 'error': console.error("Error", param.errmsg); //here I will need to add an error throw event break; case 'ok': if (this.$isMultiChannel) { const separatedPair = param.n.split(':'); if (separatedPair[0] in quotedData) { // @ts-ignore if (separatedPair[1] in quotedData[separatedPair[0]]) { // @ts-ignore Object.assign(quotedData[separatedPair[0]][separatedPair[1]], param.v); } else { // @ts-ignore quotedData[separatedPair[0]][separatedPair[1]] = param.v; } } else { // @ts-ignore quotedData[separatedPair[0]] = {}; // @ts-ignore quotedData[separatedPair[0]][separatedPair[1]] = param.v; } } else { Object.assign(quotedData, param.v); } break; default: console.log('From default', param); } }); this.emit('shaped_session_data', quotedData, { firstLoad: this.firstLoad, reload: !!sessionData.uploaded && !this.firstLoad // потом нужно будет доработать флаги (учесть чувствительность полей котировки) }); if (this.firstLoad && !!sessionData.uploaded) this.firstLoad = false; } } exports.QuoteSessionAdapter = QuoteSessionAdapter; //# sourceMappingURL=QuoteSessionAdapter.js.map