Problem
MetaTrader 5’s Python API is request/response based. I built Socketrade so the terminal could push ticks, completed bars and trade transactions into Python strategies, while commands went back for execution. Multiple setups needed to run concurrently, reload without a restart and continue managing positions when new entries were disabled.
Constraints
A socket link can disconnect, return partial writes or build up backpressure. Shared subscriptions should not be torn down when one setup stops. Bar completion is only observable when the next bar opens; historical replay cannot recreate every live fill, spread or connection failure.
Solution
An MQL5 Expert Advisor and Python asyncio hub communicate over a newline-delimited JSON socket bridge. YAML and model files configure strategies, symbols, timeframes and risk. The event runtime manages setups and persistent positions; a backtester feeds historical bars through the same strategy logic, with a configuration-driven web UI.
My Role
I built both sides of the bridge, event subscriptions, account/history/position/order commands, the hot-reloadable runtime and persistent position state. I implemented rule-based and PyTorch strategies, streaming features, sizing and controls, plus historical replay and its web interface. The related statistical-arbitrage project covers earlier pair research and engines.
Technologies
Python/asyncio, MQL5, MetaTrader 5, PyTorch, scikit-learn, pandas.
Engineering Challenges
- Recovering a stateful event channel: Request IDs and timeouts pair replies with callers; queued writes, partial-write recovery, reconnection and subscription restoration address real socket failure modes.
- Lifecycle is not the same as position ownership: Shared subscriptions are reference-counted and strategy failures isolated. Disabling entries leaves management of existing positions active rather than silently orphaning them.
- Comparable logic, unequal conditions: Live and replay paths share strategy code, but closed-bar delivery waits for the next bar and replay cannot model every execution or network condition.
Outcome
Socketrade supported concurrent rule-based and ML setups, reloadable configuration and historical replay in one runtime. It provides operational continuity for open positions when entries are disabled; no trading-performance claim follows from that capability.