Handle interruptions and rate limits.
Let the API recover. Keep your client in control.
Download the Python example · PY ↓
Step-by-step transcript
- 0:00
Handle interruptions and rate limits.
This tutorial shows how to handle MT5 interruptions and rate limits: let the API recover, restore your stream, and retry reads safely. We use real demo-account responses.
- 0:15
Check the existing session.
Open M T five dot M R P C dot pro. Keep your API key and saved terminal id. In ConnectState, this recorded response shows Connected, with isConnected, isAlive, and apiIsAlive all true. The terminal is healthy. A dropped browser connection or a failed request does not, by itself, mean you need a new terminal.
- 0:40
Let automatic recovery work.
MetaRPC monitors terminal health and automatically attempts to restore eligible active sessions when recovery is needed. It keeps the terminal id. Your application should pause affected work and allow recovery time, rather than repeatedly calling Reconnect or ConnectEx. Check state at intervals, then verify account access with AccountSummary before resuming. Recovery takes time; it is not an immediate response to every failed call.
- 1:10
Act on the reason, not every failure.
An intentional disconnect, an expired session, suspension, or invalid credentials needs the corresponding action. Read the reported reason. Update credentials or resolve the account issue when required. Reconnect and ConnectByToken are explicit recovery tools; they do not belong in a loop around every request. If an eligible session remains unavailable, keep affected work paused and contact support with the error code and time.
- 1:40
Restore your application’s socket.
Terminal recovery and your application’s WebSocket are separate connections. Once account reads work, close a failed socket and open OnSymbolTick again with the saved terminal id and API key. Restore your symbol subscriptions. Here, real E U R U S D tick messages show bid, ask and timestamps. Confirm fresh messages arrive, and avoid opening duplicate streams. A healthy existing stream can stay open.
- 2:10
Recognize a real rate-limit response.
Here is an actual AccountSummary refusal captured with a bounded read-only test. HTTP Status Code 200 means the API returned a response. The non-null error contains TERMINAL RATE LIMIT EXCEEDED and says the call was not made. The observed limit was three hundred non-trading calls per sliding minute, shared by callers using this terminal. Check the current request limits rather than hard-coding this number.
- 2:39
Wait for the time the server gives you.
In this actual response, error properties contains a key and value pair named RetryAfterSeconds, with a value of 37. Wait at least thirty-seven seconds, then add a small random delay before retrying the read. Pause other workers using the same terminal budget, too. If another refusal arrives, use its updated delay. For HTTP Status Code 429, honor the Retry After header.
- 3:08
Resume gradually.
After waiting, our actual account read succeeds: HTTP Status Code 200, no API error, and demo account data. Resume gradually instead of replaying a burst of queued reads. Cache repeated account information, coordinate request pacing across workers, and use OnSymbolTick for live quotes. A rate-limit refusal is a reason to wait, not a reason to reconnect the terminal.
- 3:36
Keep retry rules small and explicit.
The companion Python example is limited to AccountSummary. It retries explicit throttling or busy responses with a valid waiting time, adds jitter, and stops after three attempts. Other errors are returned to the caller. Keep credentials in environment variables. Use a separate, bounded policy for interruptions; do not copy this read helper around connection commands or trading methods.
- 4:03
Resolve an uncertain trade first.
If a trading request loses its reply, the broker may already have processed it. Automatic recovery does not tell you the result of that trade. After access returns, inspect OpenedOrders and DealsHistory. Use order and deal tickets when available, and compare symbol, direction, volume and time. Similar trades can look alike, so keep an unresolved request paused instead of sending it again automatically.
- 4:31
Finish cleanly.
Stop subscriptions and call Disconnect when you deliberately finish the session. Our demonstration stream was stopped and the terminal was disconnected; no trades were changed. Remember: let MetaRPC handle eligible terminal recovery, restore your own socket when needed, wait on rate limits, and resolve uncertain trading outcomes before submitting another trade.