Building on the MetaTrader 5 Manager API: five lessons from a real-time risk monitor

By Daniel Chen · · 4 min read

For a live prop-trading platform I built a C#/.NET service on MetaQuotes' MetaTrader 5 Manager API, plus a monitor that checks every funded account against its loss rules every few seconds. Here is what I would tell anyone starting the same job.

1. Know which API you are using for what

MT5 gives brokers two main doors. The Manager API is a native SDK: a manager login connects to the trade server and keeps a live local copy of users and positions, known as the pump. The Web API is plain HTTP, easier to call from any language, but every read is a network round trip. A sensible split is fast reads from the Manager API's local copy and carefully guarded writes through whichever interface your setup trusts.

2. Read from the pump, not the network

Our first position reads were network requests and could take several seconds per account. Moving them to the Manager API's pumped cache turned seconds into milliseconds, which is the difference between checking risk once a minute and checking it every few seconds. Keep the slower request as a fallback for the rare moment when the cache is not ready.

3. Assume the SDK is not thread-safe

Under concurrent load we saw accounts briefly reading another account's figures. The cause was the SDK itself: concurrent calls on one manager instance corrupted its internal state. The fix was to serialise every SDK call behind one lock, take the same lock for connect and shutdown, and check that the login returned is the login requested. It made batch reads slower. The better long-term design is a small pool of manager connections, each used by one caller at a time.

4. Never act on numbers that do not add up

A risk engine closes positions and disables trading, so a wrong number costs real money. We skip an account instead of failing it when the data looks wrong: deals for another login, a zero starting balance, a login mismatch. Before an account is promoted to the next phase, its equity is cross-checked against a second, independent source. Correct but slower beats fast but wrong.

5. Deploy with the monitor switched off

Restarting a service while the monitor runs can act on a half-ready connection. Our runbook: stop both services, deploy, check a known-bad and a known-good account by hand with the monitor off, and only then switch it on and watch the logs. It takes ten minutes and removes a whole class of incidents.

If you are building on MT5 and want a second pair of eyes, I am happy to talk.

Related serviceCrypto & trading systemsLearn more

More notes