metaapi / metaapi-javascript-sdk

Browser and node.js javascript SDK for MetaApi, a professional cloud forex trading API for MetaTrader platform which supports both MetaTrader MetaTrader 5 and MetaTrader 4. Free usage tier available.
https://metaapi.cloud
Other
23 stars 1 forks source link

Failed to subscribe TimeoutError #7

Closed superhero closed 1 month ago

superhero commented 1 month ago

I have the same error as stated in ticket #2 , and my account is deployed and connected... I know so, because the timeout shows sporadically. Meaning, sometimes it's a timeout, sometimes it's not.

I could more or less ignore the timeout error I think, and it would work just fine, however... the error message eats up the whole log, so it's impossible to follow what's going on.

idk if it's a bug a feature request or documentation request, if so, then; how to catch these timeouts and log them according to once own preference?

...or better yet, how can I solve the problem with sporadic timeouts. Currently I'm testing this on ftmo demo server through your london gateway. Maybe it's a common problem because it's their demo server?

Code example:

  const metaApi = new MetaApi(metaApiToken)
  const ftmoAccount = await metaApi.metatraderAccountApi.getAccount(ftmoAccountId)
  const ftmoConnection = ftmoAccount.getStreamingConnection()
  const terminalState = ftmoConnection.terminalState
  const historyStorage = ftmoConnection.historyStorage
  await ftmoConnection.connect()
  await ftmoConnection.waitSynchronized()

  console.log('sleep 10 seconds before we start subscribing to market data')
  await new Promise((resolve) => setTimeout(resolve, 10000))
  await ftmoConnection.subscribeToMarketData('EURUSD')

  console.log('sleep 10 seconds before we start reading the price')
  await new Promise((resolve) => setTimeout(resolve, 10000))
  console.log(terminalState.price('EURUSD'))

  console.log('sleep 10 seconds before we close all positions of market')
  await new Promise((resolve) => setTimeout(resolve, 10000))
  console.log(await ftmoConnection.closePositionsBySymbol('EURUSD'))

  console.log('sleep 10 seconds before we create a market buy order')
  await new Promise((resolve) => setTimeout(resolve, 10000))
  const position = await ftmoConnection.createMarketBuyOrder('EURUSD', size, stopLoss, takeProfit)
  console.log(position)

  console.log('sleep 10 seconds before we close the position')
  await new Promise((resolve) => setTimeout(resolve, 10000))
  console.log(await ftmoConnection.closePosition(position.positionId))

and output:

[2024-07-23T08:41:13.797Z] Connecting MetaApi websocket client to the MetaApi server via https://mt-client-api-v1.london-a.agiliumtrade.ai shared server.
[2024-07-23T08:41:13.805Z] Connecting MetaApi websocket client to the MetaApi server via https://mt-client-api-v1.london-b.agiliumtrade.ai shared server.
(node:79839) [UNDICI-WS] Warning: WebSockets are experimental, expect them to change at any time.
(Use `node --trace-warnings ...` to show where the warning was created)
[2024-07-23T08:41:14.095Z] london:0: MetaApi websocket client connected to the MetaApi server
[2024-07-23T08:41:14.130Z] london:1: MetaApi websocket client connected to the MetaApi server
sleep 10 seconds before we start subscribing to market data
[2024-07-23T08:41:16.138Z] ***:london:1:ps-mpa-b-64:onSynchronizationStarted: event is taking more than 1 second to process
[2024-07-23T08:41:17.070Z] ***:london:1:ps-mpa-b-64:onSynchronizationStarted: finished in 1 seconds
sleep 10 seconds before we start reading the price
{
  symbol: 'EURUSD',
  bid: 1.08792,
  ask: 1.08795,
  time: 2024-07-23T08:41:35.477Z,
  brokerTime: '2024-07-23 11:41:35.477',
  profitTickValue: 1,
  lossTickValue: 1,
  accountCurrencyExchangeRate: 1,
  timestamps: {
    eventGenerated: 2024-07-23T08:41:35.477Z,
    serverProcessingStarted: 2024-07-23T08:41:35.934Z,
    serverProcessingFinished: 2024-07-23T08:41:36.038Z,
    clientProcessingFinished: 2024-07-23T08:41:36.058Z
  },
  equity: 102083.3
}
sleep 10 seconds before we close all positions of market
{
  numericCode: 10009,
  stringCode: 'TRADE_RETCODE_DONE',
  message: 'Request completed',
  tradeStartTime: 2024-07-23T08:41:46.329Z
}
sleep 10 seconds before we create a market buy order
{
  numericCode: 10009,
  stringCode: 'TRADE_RETCODE_DONE',
  message: 'Request completed',
  orderId: '167093742',
  positionId: '167093742',
  tradeExecutionTime: 2024-07-23T08:41:57.253Z,
  tradeStartTime: 2024-07-23T08:41:56.482Z
}
sleep 10 seconds before we close the position
{
  numericCode: 10009,
  stringCode: 'TRADE_RETCODE_DONE',
  message: 'Request completed',
  orderId: '0',
  positionId: '167093742',
  tradeExecutionTime: 2024-07-23T08:42:07.764Z,
  tradeStartTime: 2024-07-23T08:42:06.999Z
}
[2024-07-23T08:43:12.716Z] ***:1: Failed to subscribe TimeoutError: It seems like the account *** is not connected to broker yet or SDK settings you use does not match the account region. Please make sure account is connected to broker before retrying the request. Please make sure you pass a region option to MetaApi constructor for Java SDK. Please make sure you do not pass region option to MetaApi constructor for javascript and python SDKs.
    at MetaApiWebsocketClient._convertError (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/metaapi.cloud-sdk/dist/clients/metaApi/metaApiWebsocket.client.js:1432:20)
    at Socket.<anonymous> (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/metaapi.cloud-sdk/dist/clients/metaApi/metaApiWebsocket.client.js:357:40)
    at Emitter.emit (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/component-emitter/index.js:145:20)
    at Socket.onevent (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/socket.io-client/lib/socket.js:278:10)
    at Socket.onpacket (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/socket.io-client/lib/socket.js:236:12)
    at Manager.<anonymous> (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/component-bind/index.js:21:15)
    at Emitter.emit (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/component-emitter/index.js:145:20)
    at Manager.ondecoded (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/socket.io-client/lib/manager.js:349:8)
    at Decoder.<anonymous> (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/component-bind/index.js:21:15)
    at Emitter.emit (/Users/superhero/Workspace/sd/m3-mt-market-subscriber-gcr/node_modules/component-emitter/index.js:145:20)

And then the message is repeated:

[2024-07-23T08:43:20.862Z]
[2024-07-23T08:43:31.990Z]
[2024-07-23T08:43:49.127Z]
[2024-07-23T08:44:18.282Z]
[2024-07-23T08:53:46.951Z]
[2024-07-23T08:53:55.087Z]
[2024-07-23T08:54:13.585Z]
[2024-07-23T08:54:30.762Z]
[2024-07-23T08:54:59.908Z]
[2024-07-23T08:55:41.072Z]

In between these timeouts, I'm still able to read the price changes, and open and close positions, perhaps there is some gap I fail to notice, but looks to me like it works.

if it's a matter of paying for the service to solve the issue, that's not an issue, I do expect to pay to have more than 2 accounts in the future. I'm currently just testing the integration, which I am impressed with, apart from above stated problem....

edzebo commented 1 month ago

I also encountered identical issue around 2 months ago and created a support ticket. Haven't been able to resolve it and ticket got closed. Just got back to check new version release and I'm seeing this is still ongoing.

This is also blocker for my implementation, and paying for the service is not a problem. Even though I don't need more than 2 accounts.

metaapi commented 1 month ago

The error above happens when the connection to the broker is lost. It is OK if it happens for a short period of time and rarely as the uptime of the broker connection in the MT terminal itself is not perfect both for MT4 and MT5 platforms. If it happens frequently or for prolonged periods of time for your trading account we recommend you to contact support to check if uptime of your trading account can be improved.

I'll be closing this ticket as this is not a bug in the SDK itself.

superhero commented 1 month ago

This is a bug with metaapi, or a required feature request with the sdk, depending on how you see it... I would classify it as a bug as there is no such issues when I set up my own infrastructure, and the timeouts can not be caught and handled by the developer... Instead the output is spammed to the stdout to an extent that it's unusable.

If you do not reopen this and fix the problem, then I will also stop working with the solution you offering and simply work with my own infrastructure solution, as this is clearly a problem with the sdk that metaapi should solve for anyone to be able to work with the sdk.

No matter if this problem is resolved by talking to the broker or any other support, it doesn't change the fact that we as developers need to be able to handle the timeouts, or any other error message from the server.