metaapi.cloud-sdk
Version:
SDK for MetaApi, a professional cloud forex API which includes MetaTrader REST API and MetaTrader websocket API. Supports both MetaTrader 5 (MT5) and MetaTrader 4 (MT4). CopyFactory copy trading API included. (https://metaapi.cloud)
666 lines (665 loc) • 106 kB
JavaScript
'use strict';
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _async_to_generator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
import TerminalState from './terminalState';
import MemoryHistoryStorage from './memoryHistoryStorage';
import TimeoutError from '../clients/timeoutError';
import randomstring from 'randomstring';
import ConnectionHealthMonitor from './connectionHealthMonitor';
import { ValidationError } from '../clients/errorHandler';
import OptionsValidator from '../clients/optionsValidator';
import LoggerManager from '../logger';
import MetaApiConnection from './metaApiConnection';
let StreamingMetaApiConnection = class StreamingMetaApiConnection extends MetaApiConnection {
/**
* Opens the connection. Can only be called the first time, next calls will be ignored.
* @param {string} instanceId connection instance id
* @return {Promise} promise resolving when the connection is opened
*/ connect(instanceId) {
var _this = this;
return _async_to_generator(function*() {
if (!_this._openedInstances.includes(instanceId)) {
_this._openedInstances.push(instanceId);
}
if (!_this._opened) {
_this._logger.trace(`${_this._account.id}: Opening connection`);
_this._opened = true;
try {
yield _this.initialize();
yield _this.subscribe();
} catch (err) {
yield _this.close();
throw err;
}
}
})();
}
/**
* Clears the order and transaction history of a specified application and removes application
* @return {Promise} promise resolving when the history is cleared and application is removed
*/ removeApplication() {
this._checkIsConnectionActive();
this._historyStorage.clear();
return this._websocketClient.removeApplication(this._account.id);
}
/**
* Requests the terminal to start synchronization process
* (see https://metaapi.cloud/docs/client/websocket/synchronizing/synchronize/)
* @param {String} instanceIndex instance index
* @returns {Promise} promise which resolves when synchronization started
*/ synchronize(instanceIndex) {
var _this = this;
return _async_to_generator(function*() {
_this._checkIsConnectionActive();
const region = _this.getRegion(instanceIndex);
const instance = _this.getInstanceNumber(instanceIndex);
const host = _this.getHostName(instanceIndex);
let startingHistoryOrderTime = new Date(Math.max((_this._historyStartTime || new Date(0)).getTime(), (yield _this._historyStorage.lastHistoryOrderTime(instance)).getTime()));
let startingDealTime = new Date(Math.max((_this._historyStartTime || new Date(0)).getTime(), (yield _this._historyStorage.lastDealTime(instance)).getTime()));
let synchronizationId = randomstring.generate(32);
_this._getState(instanceIndex).lastSynchronizationId = synchronizationId;
const accountId = _this._account.accountRegions[region];
_this._logger.debug(`${_this._account.id}:${instanceIndex}: initiating synchronization ${synchronizationId}`);
return _this._websocketClient.synchronize(accountId, instance, host, synchronizationId, startingHistoryOrderTime, startingDealTime, _this.terminalState.getHashes());
})();
}
/**
* Initializes meta api connection
* @return {Promise} promise which resolves when meta api connection is initialized
*/ initialize() {
var _this = this;
return _async_to_generator(function*() {
_this._checkIsConnectionActive();
yield _this._historyStorage.initialize(_this._account.id, _this._connectionRegistry.application);
_this._websocketClient.addAccountCache(_this._account.id, _this._account.accountRegions);
})();
}
/**
* Initiates subscription to MetaTrader terminal
* @returns {Promise} promise which resolves when subscription is initiated
*/ subscribe() {
var _this = this;
return _async_to_generator(function*() {
_this._checkIsConnectionActive();
const accountRegions = _this._account.accountRegions;
Object.entries(accountRegions).forEach(([region, replicaId])=>{
if (!_this._options.region || _this._options.region === region) {
_this._websocketClient.ensureSubscribe(replicaId, 0);
_this._websocketClient.ensureSubscribe(replicaId, 1);
}
});
})();
}
/**
* Subscribes on market data of specified symbol (see
* https://metaapi.cloud/docs/client/websocket/marketDataStreaming/subscribeToMarketData/).
* @param {String} symbol symbol (e.g. currency pair or an index)
* @param {Array<MarketDataSubscription>} subscriptions array of market data subscription to create or update. Please
* note that this feature is not fully implemented on server-side yet
* @param {number} [timeoutInSeconds] timeout to wait for prices in seconds, default is 30
* @param {boolean} [waitForQuote] if set to false, the method will resolve without waiting for the first quote to
* arrive. Default is to wait for quote if quotes subscription is requested.
* @returns {Promise} promise which resolves when subscription request was processed
*/ subscribeToMarketData(symbol, subscriptions, timeoutInSeconds, waitForQuote = true) {
var _this = this;
return _async_to_generator(function*() {
_this._checkIsConnectionActive();
if (!_this._terminalState.specification(symbol)) {
throw new ValidationError(`${_this._account.id}: Cannot subscribe to market data for symbol ${symbol} because ` + 'symbol does not exist');
} else {
subscriptions = subscriptions || [
{
type: 'quotes'
}
];
if (_this._subscriptions[symbol]) {
const prevSubscriptions = _this._subscriptions[symbol].subscriptions;
subscriptions.forEach((subscription)=>{
const index = subscription.type === 'candles' ? prevSubscriptions.findIndex((item)=>item.type === subscription.type && item.timeframe === subscription.timeframe) : prevSubscriptions.findIndex((item)=>item.type === subscription.type);
if (index === -1) {
prevSubscriptions.push(subscription);
} else {
prevSubscriptions[index] = subscription;
}
});
} else {
_this._subscriptions[symbol] = {
subscriptions
};
}
yield _this._websocketClient.subscribeToMarketData(_this._account.id, symbol, subscriptions, _this._account.reliability);
if (waitForQuote !== false && subscriptions.find((s)=>s.type === 'quotes')) {
return _this.terminalState.waitForPrice(symbol, timeoutInSeconds);
}
}
})();
}
/**
* Unsubscribes from market data of specified symbol (see
* https://metaapi.cloud/docs/client/websocket/marketDataStreaming/unsubscribeFromMarketData/).
* @param {String} symbol symbol (e.g. currency pair or an index)
* @param {Array<MarketDataUnsubscription>} unsubscriptions array of subscriptions to cancel
* @returns {Promise} promise which resolves when unsubscription request was processed
*/ unsubscribeFromMarketData(symbol, unsubscriptions) {
this._checkIsConnectionActive();
if (!unsubscriptions) {
delete this._subscriptions[symbol];
} else if (this._subscriptions[symbol]) {
this._subscriptions[symbol].subscriptions = this._subscriptions[symbol].subscriptions.filter((subscription)=>{
return !unsubscriptions.find((unsubscription)=>subscription.type === unsubscription.type && (!unsubscription.timeframe || subscription.timeframe === unsubscription.timeframe));
});
if (!this._subscriptions[symbol].subscriptions.length) {
delete this._subscriptions[symbol];
}
}
return this._websocketClient.unsubscribeFromMarketData(this._account.id, symbol, unsubscriptions, this._account.reliability);
}
/**
* Invoked when subscription downgrade has occurred
* @param {String} instanceIndex index of an account instance connected
* @param {string} symbol symbol to update subscriptions for
* @param {Array<MarketDataSubscription>} updates array of market data subscription to update
* @param {Array<MarketDataUnsubscription>} unsubscriptions array of subscriptions to cancel
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ // eslint-disable-next-line complexity
onSubscriptionDowngraded(instanceIndex, symbol, updates, unsubscriptions) {
var _this = this;
return _async_to_generator(function*() {
if (unsubscriptions === null || unsubscriptions === void 0 ? void 0 : unsubscriptions.length) {
_this.unsubscribeFromMarketData(symbol, unsubscriptions).catch((err)=>{
let method = err.name !== 'ValidationError' ? 'error' : 'trace';
_this._logger[method](`${_this._account.id}: failed do unsubscribe from market data on subscription downgraded`, err);
});
}
if (updates === null || updates === void 0 ? void 0 : updates.length) {
_this.subscribeToMarketData(symbol, updates).catch((err)=>{
_this._logger.error(`${_this._account.id}: failed do subscribe from market data on subscription downgraded`, err);
});
}
})();
}
/**
* Returns list of the symbols connection is subscribed to
* @returns {Array<String>} list of the symbols connection is subscribed to
*/ get subscribedSymbols() {
return Object.keys(this._subscriptions);
}
/**
* Returns subscriptions for a symbol
* @param {string} symbol symbol to retrieve subscriptions for
* @returns {Array<MarketDataSubscription>} list of market data subscriptions for the symbol
*/ subscriptions(symbol) {
this._checkIsConnectionActive();
return (this._subscriptions[symbol] || {}).subscriptions;
}
/**
* Returns local copy of terminal state
* @returns {TerminalState} local copy of terminal state
*/ get terminalState() {
return this._terminalState;
}
/**
* Returns local history storage
* @returns {HistoryStorage} local history storage
*/ get historyStorage() {
return this._historyStorage;
}
/**
* Invoked when connection to MetaTrader terminal established
* @param {String} instanceIndex index of an account instance connected
* @param {Number} replicas number of account replicas launched
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ onConnected(instanceIndex, replicas) {
var _this = this;
return _async_to_generator(function*() {
let key = randomstring.generate(32);
let state = _this._getState(instanceIndex);
const region = _this.getRegion(instanceIndex);
_this.cancelRefresh(region);
yield _this._terminalHashManager.refreshIgnoredFieldLists(region);
state.shouldSynchronize = key;
state.synchronizationRetryIntervalInSeconds = 1;
state.synchronized = false;
_this._ensureSynchronized(instanceIndex, key);
_this._logger.debug(`${_this._account.id}:${instanceIndex}: connected to broker`);
})();
}
/**
* Invoked when connection to MetaTrader terminal terminated
* @param {String} instanceIndex index of an account instance connected
*/ onDisconnected(instanceIndex) {
var _this = this;
return _async_to_generator(function*() {
let state = _this._getState(instanceIndex);
state.lastDisconnectedSynchronizationId = state.lastSynchronizationId;
state.lastSynchronizationId = undefined;
state.shouldSynchronize = undefined;
state.synchronized = false;
state.disconnected = true;
const instanceNumber = _this.getInstanceNumber(instanceIndex);
const region = _this.getRegion(instanceIndex);
const instance = `${region}:${instanceNumber}`;
delete _this._refreshMarketDataSubscriptionSessions[instance];
clearTimeout(_this._refreshMarketDataSubscriptionTimeouts[instance]);
delete _this._refreshMarketDataSubscriptionTimeouts[instance];
clearTimeout(state.synchronizationTimeout);
delete state.synchronizationTimeout;
clearTimeout(state.ensureSynchronizeTimeout);
delete state.ensureSynchronizeTimeout;
_this._logger.debug(`${_this._account.id}:${instanceIndex}: disconnected from broker`);
})();
}
/**
* Invoked when a symbol specifications were updated
* @param {String} instanceIndex index of account instance connected
* @param {Array<MetatraderSymbolSpecification>} specifications updated specifications
* @param {Array<String>} removedSymbols removed symbols
*/ onSymbolSpecificationsUpdated(instanceIndex, specifications, removedSymbols) {
this._scheduleSynchronizationTimeout(instanceIndex);
}
/**
* Invoked when position synchronization finished to indicate progress of an initial terminal state synchronization
* @param {string} instanceIndex index of an account instance connected
* @param {String} synchronizationId synchronization request id
*/ onPositionsSynchronized(instanceIndex, synchronizationId) {
this._scheduleSynchronizationTimeout(instanceIndex);
}
/**
* Invoked when pending order synchronization fnished to indicate progress of an initial terminal state
* synchronization
* @param {string} instanceIndex index of an account instance connected
* @param {String} synchronizationId synchronization request id
*/ onPendingOrdersSynchronized(instanceIndex, synchronizationId) {
this._scheduleSynchronizationTimeout(instanceIndex);
}
/**
* Invoked when a synchronization of history deals on a MetaTrader account have finished to indicate progress of an
* initial terminal state synchronization
* @param {String} instanceIndex index of an account instance connected
* @param {String} synchronizationId synchronization request id
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ onDealsSynchronized(instanceIndex, synchronizationId) {
var _this = this;
return _async_to_generator(function*() {
let state = _this._getState(instanceIndex);
state.dealsSynchronized[synchronizationId] = true;
_this._scheduleSynchronizationTimeout(instanceIndex);
_this._logger.debug(`${_this._account.id}:${instanceIndex}: finished synchronization ${synchronizationId}`);
})();
}
/**
* Invoked when a synchronization of history orders on a MetaTrader account have finished to indicate progress of an
* initial terminal state synchronization
* @param {String} instanceIndex index of an account instance connected
* @param {String} synchronizationId synchronization request id
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ onHistoryOrdersSynchronized(instanceIndex, synchronizationId) {
var _this = this;
return _async_to_generator(function*() {
let state = _this._getState(instanceIndex);
state.ordersSynchronized[synchronizationId] = true;
_this._scheduleSynchronizationTimeout(instanceIndex);
})();
}
/**
* Invoked when connection to MetaApi websocket API restored after a disconnect
* @param {String} region reconnected region
* @param {Number} instanceNumber reconnected instance number
* @return {Promise} promise which resolves when connection to MetaApi websocket API restored after a disconnect
*/ onReconnected(region, instanceNumber) {
var _this = this;
return _async_to_generator(function*() {
const instanceTemplate = `${region}:${instanceNumber}`;
Object.keys(_this._stateByInstanceIndex).filter((key)=>key.startsWith(`${instanceTemplate}:`)).forEach((key)=>{
delete _this._stateByInstanceIndex[key];
});
delete _this._refreshMarketDataSubscriptionSessions[instanceTemplate];
clearTimeout(_this._refreshMarketDataSubscriptionTimeouts[instanceTemplate]);
delete _this._refreshMarketDataSubscriptionTimeouts[instanceTemplate];
})();
}
/**
* Invoked when a stream for an instance index is closed
* @param {String} instanceIndex index of an account instance connected
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ onStreamClosed(instanceIndex) {
var _this = this;
return _async_to_generator(function*() {
delete _this._stateByInstanceIndex[instanceIndex];
})();
}
/**
* Invoked when MetaTrader terminal state synchronization is started
* @param {string} instanceIndex index of an account instance connected
* @param {string} specificationsHash specifications hash
* @param {string} positionsHash positions hash
* @param {string} ordersHash orders hash
* @param {string} synchronizationId synchronization id
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ onSynchronizationStarted(instanceIndex, specificationsHash, positionsHash, ordersHash, synchronizationId) {
var _this = this;
return _async_to_generator(function*() {
_this._logger.debug(`${_this._account.id}:${instanceIndex}: starting synchronization ${synchronizationId}`);
const instanceNumber = _this.getInstanceNumber(instanceIndex);
const region = _this.getRegion(instanceIndex);
const instance = `${region}:${instanceNumber}`;
const accountId = _this._account.accountRegions[region];
delete _this._refreshMarketDataSubscriptionSessions[instance];
let sessionId = randomstring.generate(32);
_this._refreshMarketDataSubscriptionSessions[instance] = sessionId;
clearTimeout(_this._refreshMarketDataSubscriptionTimeouts[instance]);
delete _this._refreshMarketDataSubscriptionTimeouts[instance];
yield _this._refreshMarketDataSubscriptions(accountId, instanceNumber, sessionId);
_this._scheduleSynchronizationTimeout(instanceIndex);
let state = _this._getState(instanceIndex);
if (state && !_this._closed) {
state.lastSynchronizationId = synchronizationId;
}
})();
}
/**
* Invoked when account region has been unsubscribed
* @param {String} region account region unsubscribed
* @return {Promise} promise which resolves when the asynchronous event is processed
*/ onUnsubscribeRegion(region) {
Object.keys(this._refreshMarketDataSubscriptionTimeouts).filter((instance)=>instance.startsWith(`${region}:`)).forEach((instance)=>{
clearTimeout(this._refreshMarketDataSubscriptionTimeouts[instance]);
delete this._refreshMarketDataSubscriptionTimeouts[instance];
delete this._refreshMarketDataSubscriptionSessions[instance];
});
Object.keys(this._stateByInstanceIndex).filter((instance)=>instance.startsWith(`${region}:`)).forEach((instance)=>delete this._stateByInstanceIndex[instance]);
}
/**
* Returns flag indicating status of state synchronization with MetaTrader terminal
* @param {String} instanceIndex index of an account instance connected
* @param {String} synchronizationId optional synchronization request id, last synchronization request id will be used
* by default
* @return {Promise<Boolean>} promise resolving with a flag indicating status of state synchronization with MetaTrader
* terminal
*/ isSynchronized(instanceIndex, synchronizationId) {
var _this = this;
return _async_to_generator(function*() {
return Object.values(_this._stateByInstanceIndex).reduce((acc, s)=>{
if (instanceIndex !== undefined && s.instanceIndex !== instanceIndex) {
return acc;
}
const checkSynchronizationId = synchronizationId || s.lastSynchronizationId;
let synchronized = !!s.ordersSynchronized[checkSynchronizationId] && !!s.dealsSynchronized[checkSynchronizationId];
return acc || synchronized;
}, false);
})();
}
/**
* @typedef {Object} SynchronizationOptions
* @property {String} [applicationPattern] application regular expression pattern, default is .*
* @property {String} [synchronizationId] synchronization id, last synchronization request id will be used by
* default
* @property {Number} [instanceIndex] index of an account instance to ensure synchronization on, default is to wait
* for the first instance to synchronize
* @property {Number} [timeoutInSeconds] wait timeout in seconds, default is 5m
* @property {Number} [intervalInMilliseconds] interval between account reloads while waiting for a change, default is 1s
*/ /**
* Waits until synchronization to MetaTrader terminal is completed
* @param {SynchronizationOptions} opts synchronization options
* @return {Promise} promise which resolves when synchronization to MetaTrader terminal is completed
* @throws {TimeoutError} if application failed to synchronize with the teminal within timeout allowed
*/ // eslint-disable-next-line complexity
waitSynchronized(opts) {
var _this = this;
return _async_to_generator(function*() {
_this._checkIsConnectionActive();
opts = opts || {};
let instanceIndex = opts.instanceIndex;
let synchronizationId = opts.synchronizationId;
let timeoutInSeconds = opts.timeoutInSeconds || 300;
let intervalInMilliseconds = opts.intervalInMilliseconds || 1000;
let applicationPattern = opts.applicationPattern || (_this._account.application === 'CopyFactory' ? 'CopyFactory.*|RPC' : 'RPC');
let startTime = Date.now();
let synchronized;
while(!(synchronized = yield _this.isSynchronized(instanceIndex, synchronizationId)) && startTime + timeoutInSeconds * 1000 > Date.now()){
yield new Promise((res)=>setTimeout(res, intervalInMilliseconds));
}
let state;
if (instanceIndex === undefined) {
for (let s of Object.values(_this._stateByInstanceIndex)){
if (yield _this.isSynchronized(s.instanceIndex, synchronizationId)) {
state = s;
instanceIndex = s.instanceIndex;
}
}
} else {
state = Object.values(_this._stateByInstanceIndex).find((s)=>s.instanceIndex === instanceIndex);
}
if (!synchronized) {
throw new TimeoutError('Timed out waiting for MetaApi to synchronize to MetaTrader account ' + _this._account.id + ', synchronization id ' + (synchronizationId || state && state.lastSynchronizationId || state && state.lastDisconnectedSynchronizationId));
}
let timeLeftInSeconds = Math.max(0, timeoutInSeconds - (Date.now() - startTime) / 1000);
const region = _this.getRegion(state.instanceIndex);
const accountId = _this._account.accountRegions[region];
yield _this._websocketClient.waitSynchronized(accountId, _this.getInstanceNumber(instanceIndex), applicationPattern, timeLeftInSeconds);
})();
}
/**
* Closes the connection. The instance of the class should no longer be used after this method is invoked.
* @param {string} instanceId connection instance id
*/ close(instanceId) {
var _this = this;
return _async_to_generator(function*() {
if (_this._opened) {
_this._openedInstances = _this._openedInstances.filter((id)=>id !== instanceId);
if (!_this._openedInstances.length && !_this._closed) {
clearInterval(_this._refreshJob);
_this._logger.debug(`${_this._account.id}: Closing connection`);
Object.values(_this._stateByInstanceIndex).forEach((state)=>clearTimeout(state.synchronizationTimeout));
_this._stateByInstanceIndex = {};
yield _this._connectionRegistry.removeStreaming(_this._account);
_this._terminalState.close();
const accountRegions = _this._account.accountRegions;
_this._websocketClient.removeSynchronizationListener(_this._account.id, _this);
_this._websocketClient.removeSynchronizationListener(_this._account.id, _this._terminalState);
_this._websocketClient.removeSynchronizationListener(_this._account.id, _this._historyStorage);
_this._websocketClient.removeSynchronizationListener(_this._account.id, _this._healthMonitor);
_this._websocketClient.removeReconnectListener(_this);
_this._healthMonitor.stop();
_this._refreshMarketDataSubscriptionSessions = {};
Object.values(_this._refreshMarketDataSubscriptionTimeouts).forEach((timeout)=>clearTimeout(timeout));
_this._refreshMarketDataSubscriptionTimeouts = {};
Object.values(accountRegions).forEach((replicaId)=>_this._websocketClient.removeAccountCache(replicaId));
_this._closed = true;
_this._logger.trace(`${_this._account.id}: Closed connection`);
}
}
})();
}
/**
* Returns synchronization status
* @return {boolean} synchronization status
*/ get synchronized() {
return Object.values(this._stateByInstanceIndex).reduce((acc, s)=>acc || s.synchronized, false);
}
/**
* Returns MetaApi account
* @return {MetatraderAccount} MetaApi account
*/ get account() {
return this._account;
}
/**
* Returns connection health monitor instance
* @return {ConnectionHealthMonitor} connection health monitor instance
*/ get healthMonitor() {
return this._healthMonitor;
}
_refreshMarketDataSubscriptions(accountId, instanceNumber, session) {
var _this = this;
return _async_to_generator(function*() {
const region = _this._websocketClient.getAccountRegion(accountId);
const instance = `${region}:${instanceNumber}`;
try {
if (_this._refreshMarketDataSubscriptionSessions[instance] === session) {
const subscriptionsList = [];
Object.keys(_this._subscriptions).forEach((key)=>{
const subscriptions = _this.subscriptions(key);
const subscriptionsItem = {
symbol: key
};
if (subscriptions) {
subscriptionsItem.subscriptions = subscriptions;
}
subscriptionsList.push(subscriptionsItem);
});
yield _this._websocketClient.refreshMarketDataSubscriptions(accountId, instanceNumber, subscriptionsList);
}
} catch (err) {
_this._logger.error(`Error refreshing market data subscriptions job for account ${_this._account.id} ` + `${instanceNumber}`, err);
} finally{
if (_this._refreshMarketDataSubscriptionSessions[instance] === session) {
let refreshInterval = (Math.random() * (_this._maxSubscriptionRefreshInterval - _this._minSubscriptionRefreshInterval) + _this._minSubscriptionRefreshInterval) * 1000;
_this._refreshMarketDataSubscriptionTimeouts[instance] = setTimeout(()=>_this._refreshMarketDataSubscriptions(accountId, instanceNumber, session), refreshInterval);
}
}
})();
}
_generateStopOptions(stopLoss, takeProfit) {
let trade = {};
if (typeof stopLoss === 'number') {
trade.stopLoss = stopLoss;
} else if (stopLoss) {
trade.stopLoss = stopLoss.value;
trade.stopLossUnits = stopLoss.units;
}
if (typeof takeProfit === 'number') {
trade.takeProfit = takeProfit;
} else if (takeProfit) {
trade.takeProfit = takeProfit.value;
trade.takeProfitUnits = takeProfit.units;
}
return trade;
}
_ensureSynchronized(instanceIndex, key) {
var _this = this;
return _async_to_generator(function*() {
let state = _this._getState(instanceIndex);
if (state && state.shouldSynchronize && !_this._closed) {
try {
const synchronizationResult = yield _this.synchronize(instanceIndex);
if (synchronizationResult) {
state.synchronized = true;
state.synchronizationRetryIntervalInSeconds = 1;
delete state.ensureSynchronizeTimeout;
}
_this._scheduleSynchronizationTimeout(instanceIndex);
} catch (err) {
const level = _this._latencyService.getSynchronizedAccountInstances(_this._account.id).length ? 'debug' : 'error';
_this._logger[level]('MetaApi websocket client for account ' + _this._account.id + ':' + instanceIndex + ' failed to synchronize', err);
if (state.shouldSynchronize === key) {
clearTimeout(state.ensureSynchronizeTimeout);
state.ensureSynchronizeTimeout = setTimeout(_this._ensureSynchronized.bind(_this, instanceIndex, key), state.synchronizationRetryIntervalInSeconds * 1000);
state.synchronizationRetryIntervalInSeconds = Math.min(state.synchronizationRetryIntervalInSeconds * 2, 300);
}
}
}
})();
}
_getState(instanceIndex) {
if (!this._stateByInstanceIndex['' + instanceIndex]) {
this._stateByInstanceIndex['' + instanceIndex] = {
instanceIndex,
ordersSynchronized: {},
dealsSynchronized: {},
shouldSynchronize: undefined,
synchronizationRetryIntervalInSeconds: 1,
synchronized: false,
lastDisconnectedSynchronizationId: undefined,
lastSynchronizationId: undefined,
disconnected: false
};
}
return this._stateByInstanceIndex['' + instanceIndex];
}
_scheduleSynchronizationTimeout(instanceIndex) {
let state = this._getState(instanceIndex);
if (state && !this._closed) {
clearTimeout(state.synchronizationTimeout);
state.synchronizationTimeout = setTimeout(()=>this._checkSynchronizationTimedOut(instanceIndex), 2 * 60 * 1000);
this._logger.debug(`${this._account.id}:${instanceIndex}: scheduled synchronization timeout`);
}
}
_checkSynchronizationTimedOut(instanceIndex) {
this._logger.debug(`${this._account.id}:${instanceIndex}: checking if synchronization timed out out`);
let state = this._getState(instanceIndex);
if (state && !this._closed) {
let synchronizationId = state.lastSynchronizationId;
let synchronized = !!state.dealsSynchronized[synchronizationId];
if (!synchronized && synchronizationId && state.shouldSynchronize) {
this._logger.warn(`${this._account.id}:${instanceIndex}: resynchronized since latest synchronization ` + `${synchronizationId} did not finish in time`);
this._ensureSynchronized(instanceIndex, state.shouldSynchronize);
}
}
}
/**
* Constructs MetaApi MetaTrader streaming Api connection
* @param {MetaApiOpts} options metaapi options
* @param {MetaApiWebsocketClient} websocketClient MetaApi websocket client
* @param {TerminalHashManager} terminalHashManager terminal hash manager
* @param {MetatraderAccount} account MetaTrader account id to connect to
* @param {HistoryStorage} historyStorage terminal history storage. By default an instance of MemoryHistoryStorage
* will be used.
* @param {ConnectionRegistry} connectionRegistry metatrader account connection registry
* @param {Date} [historyStartTime] history start sync time
* @param {RefreshSubscriptionsOpts} [refreshSubscriptionsOpts] subscriptions refresh options
*/ constructor(options, websocketClient, terminalHashManager, account, historyStorage, connectionRegistry, historyStartTime, refreshSubscriptionsOpts){
super(options, websocketClient, account);
refreshSubscriptionsOpts = refreshSubscriptionsOpts || {};
const validator = new OptionsValidator();
this._minSubscriptionRefreshInterval = validator.validateNonZero(refreshSubscriptionsOpts.minDelayInSeconds, 1, 'refreshSubscriptionsOpts.minDelayInSeconds');
this._maxSubscriptionRefreshInterval = validator.validateNonZero(refreshSubscriptionsOpts.maxDelayInSeconds, 600, 'refreshSubscriptionsOpts.maxDelayInSeconds');
this._connectionRegistry = connectionRegistry;
this._historyStartTime = historyStartTime;
this._terminalHashManager = terminalHashManager;
this._terminalState = new TerminalState(account, terminalHashManager, this._websocketClient);
this._historyStorage = historyStorage || new MemoryHistoryStorage();
this._healthMonitor = new ConnectionHealthMonitor(this);
this._websocketClient.addSynchronizationListener(account.id, this);
this._websocketClient.addSynchronizationListener(account.id, this._terminalState);
this._websocketClient.addSynchronizationListener(account.id, this._historyStorage);
this._websocketClient.addSynchronizationListener(account.id, this._healthMonitor);
Object.values(account.accountRegions).forEach((replicaId)=>this._websocketClient.addReconnectListener(this, replicaId));
this._subscriptions = {};
this._stateByInstanceIndex = {};
this._refreshMarketDataSubscriptionSessions = {};
this._refreshMarketDataSubscriptionTimeouts = {};
this._openedInstances = [];
this._logger = LoggerManager.getLogger('MetaApiConnection');
}
};
/**
* Exposes MetaApi MetaTrader streaming API connection to consumers
*/ export { StreamingMetaApiConnection as default };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyIndXNlIHN0cmljdCc7XG5cbmltcG9ydCBUZXJtaW5hbFN0YXRlIGZyb20gJy4vdGVybWluYWxTdGF0ZSc7XG5pbXBvcnQgTWVtb3J5SGlzdG9yeVN0b3JhZ2UgZnJvbSAnLi9tZW1vcnlIaXN0b3J5U3RvcmFnZSc7XG5pbXBvcnQgVGltZW91dEVycm9yIGZyb20gJy4uL2NsaWVudHMvdGltZW91dEVycm9yJztcbmltcG9ydCByYW5kb21zdHJpbmcgZnJvbSAncmFuZG9tc3RyaW5nJztcbmltcG9ydCBDb25uZWN0aW9uSGVhbHRoTW9uaXRvciBmcm9tICcuL2Nvbm5lY3Rpb25IZWFsdGhNb25pdG9yJztcbmltcG9ydCB7VmFsaWRhdGlvbkVycm9yfSBmcm9tICcuLi9jbGllbnRzL2Vycm9ySGFuZGxlcic7XG5pbXBvcnQgT3B0aW9uc1ZhbGlkYXRvciBmcm9tICcuLi9jbGllbnRzL29wdGlvbnNWYWxpZGF0b3InO1xuaW1wb3J0IExvZ2dlck1hbmFnZXIgZnJvbSAnLi4vbG9nZ2VyJztcbmltcG9ydCBNZXRhQXBpQ29ubmVjdGlvbiBmcm9tICcuL21ldGFBcGlDb25uZWN0aW9uJztcblxuLyoqXG4gKiBFeHBvc2VzIE1ldGFBcGkgTWV0YVRyYWRlciBzdHJlYW1pbmcgQVBJIGNvbm5lY3Rpb24gdG8gY29uc3VtZXJzXG4gKi9cbmV4cG9ydCB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