UNPKG

@fraserdarwent/xapi-node

Version:

This project is made possible to get data from Forex market, execute market or limit order with NodeJS/JS through WebSocket connection

180 lines 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Socket = void 0; const __1 = require("../.."); const SocketConnection_1 = require("./SocketConnection"); const Log_1 = require("../../utils/Log"); class Socket extends SocketConnection_1.SocketConnection { constructor(XAPI, password) { super(XAPI, password, 'wss://' + XAPI.hostName + '/' + XAPI.accountType); this.listen = { getAllSymbols: (callBack, key = null) => this.addListener('command_' + 'getAllSymbols', callBack, key), getCalendar: (callBack, key = null) => this.addListener('command_' + 'getCalendar', callBack, key), getChartLastRequest: (callBack, key = null) => this.addListener('command_' + 'getChartLastRequest', callBack, key), getChartRangeRequest: (callBack, key = null) => this.addListener('command_' + 'getChartRangeRequest', callBack, key), getCommissionDef: (callBack, key = null) => this.addListener('command_' + 'getCommissionDef', callBack, key), getCurrentUserData: (callBack, key = null) => this.addListener('command_' + 'getCurrentUserData', callBack, key), getIbsHistory: (callBack, key = null) => this.addListener('command_' + 'getIbsHistory', callBack, key), getMarginLevel: (callBack, key = null) => this.addListener('command_' + 'getMarginLevel', callBack, key), getMarginTrade: (callBack, key = null) => this.addListener('command_' + 'getMarginTrade', callBack, key), getNews: (callBack, key = null) => this.addListener('command_' + 'getNews', callBack, key), getProfitCalculation: (callBack, key = null) => this.addListener('command_' + 'getProfitCalculation', callBack, key), getServerTime: (callBack, key = null) => this.addListener('command_' + 'getServerTime', callBack, key), getStepRules: (callBack, key = null) => this.addListener('command_' + 'getStepRules', callBack, key), getSymbol: (callBack, key = null) => this.addListener('command_' + 'getSymbol', callBack, key), getTickPrices: (callBack, key = null) => this.addListener('command_' + 'getTickPrices', callBack, key), getTradeRecords: (callBack, key = null) => this.addListener('command_' + 'getTradeRecords', callBack, key), getTrades: (callBack, key = null) => this.addListener('command_' + 'getTrades', callBack, key), getTradesHistory: (callBack, key = null) => this.addListener('command_' + 'getTradesHistory', callBack, key), getTradingHours: (callBack, key = null) => this.addListener('command_' + 'getTradingHours', callBack, key), getVersion: (callBack, key = null) => this.addListener('command_' + 'getVersion', callBack, key), tradeTransaction: (callBack, key = null) => this.addListener('command_' + 'tradeTransaction', callBack, key), tradeTransactionStatus: (callBack, key = null) => this.addListener('command_' + 'tradeTransactionStatus', callBack, key), ping: (callBack, key = null) => this.addListener('command_' + 'ping', callBack, key), login: (callBack, key = null) => this.addListener('command_' + 'login', callBack, key) }; this.send = { getAllSymbols: () => this.sendCommand('getAllSymbols'), getCalendar: () => this.sendCommand('getCalendar'), getChartLastRequest: (period, start, symbol) => this.sendCommand('getChartLastRequest', { 'info': { period, start, symbol } }), getChartRangeRequest: (end, period, start, symbol, ticks = 0) => this.sendCommand('getChartRangeRequest', { 'info': { end, period, start, symbol, ticks } }), getCommissionDef: (symbol, volume) => this.sendCommand('getCommissionDef', { symbol, volume }), getCurrentUserData: () => this.sendCommand('getCurrentUserData'), getIbsHistory: (start, end) => this.sendCommand('getIbsHistory', { end, start }), getMarginLevel: () => this.sendCommand('getMarginLevel'), getMarginTrade: (symbol, volume) => this.sendCommand('getMarginTrade', { symbol, volume }), getNews: (start, end) => this.sendCommand('getNews', { start, end }), getProfitCalculation: (closePrice, cmd, openPrice, symbol, volume) => this.sendCommand('getProfitCalculation', { closePrice, cmd, openPrice, symbol, volume }), getServerTime: () => this.sendCommand('getServerTime', {}, null, true), getStepRules: () => this.sendCommand('getStepRules'), getSymbol: (symbol) => this.sendCommand('getSymbol', { symbol }), getTickPrices: (symbols, timestamp = 0, level = -1) => this.sendCommand('getTickPrices', { level, symbols, timestamp }), getTradeRecords: (orders) => this.sendCommand('getTradeRecords', { orders }), getTrades: (openedOnly = true) => this.sendCommand('getTrades', { openedOnly }), getTradesHistory: (start, end) => this.sendCommand('getTradesHistory', { end, start }), getTradingHours: (symbols) => this.sendCommand('getTradingHours', { symbols }), getVersion: () => this.sendCommand('getVersion'), tradeTransaction: (tradeTransInfo) => { const { customComment, expiration, cmd, offset, order, price, sl, symbol, tp, type, volume } = tradeTransInfo; return new Promise((resolve, reject) => { const position = type === __1.TYPE_FIELD.MODIFY ? this.XAPI.positions.find(p => p.position === order) : undefined; if (type === __1.TYPE_FIELD.MODIFY && position === undefined) { const error = (!this.XAPI.isSubscribeTrades) ? 'type === MODIFY in tradeTransaction will not work with missing parameters and subscribeTrades = false, ' + 'you should set subscribeTrades = true in login config' : 'type === MODIFY in tradeTransaction orderId = ' + order + ' not found,' + ' possible open orderIds: ' + this.XAPI.positions.map(p => p.position).join(','); if (cmd === undefined) { return Promise.reject(error); } else { Log_1.Log.error(error); } } const transactionId = this.createTransactionId(); return this.sendCommand('tradeTransaction', { 'tradeTransInfo': { cmd: position ? cmd || position.cmd : cmd, customComment: (customComment === undefined || customComment === null || customComment.length === 0) ? 'x' + transactionId : 'x' + transactionId + '_' + customComment, expiration: (expiration instanceof Date) ? expiration.getTime() : (position ? expiration || position.expiration : expiration), offset: position ? offset || position.offset : offset, order, price: position ? price || position.open_price : price, sl: position ? sl || position.sl : sl, symbol: position ? symbol || position.symbol : symbol, tp: position ? tp || position.tp : tp, type, volume: volume === undefined ? (position ? parseFloat(position.volume.toFixed(2)) : undefined) : parseFloat(volume.toFixed(2)) } }, transactionId, true).then(({ returnData, time }) => { if (this.XAPI.isSubscribeTrades) { const { data } = this.XAPI.orders[returnData.order] || {}; if (data === undefined || data === null) { this.XAPI.orders[returnData.order] = { order: returnData.order, resolve, reject, data: null, time }; } else { if (data.requestStatus === __1.REQUEST_STATUS_FIELD.ACCEPTED) { resolve(data); } else { reject(data); } delete this.XAPI.orders[returnData.order]; } } else { resolve({ customComment: null, message: null, order: returnData.order, requestStatus: null }); } }).catch(reject); }); }, tradeTransactionStatus: (order) => this.sendCommand('tradeTransactionStatus', { order }) }; } } exports.Socket = Socket; //# sourceMappingURL=Socket.js.map