Skip to content
All library documents

Backtesting Python Time-Series Models in MT5 Through a Socket Server

Article MQL5 articles

Summary

The article describes connecting a Python forecasting model to an MQL5 Expert Advisor through a TCP socket, so the model can be used in MetaTrader 5 historical tests. The EA sends recent bar data to a Python server, which runs inference and returns a forecast; the example maps the forecast trend to buy or sell decisions. The server can run locally, in a container, or on a remote system, allowing the model and trading terminal to use different languages and operating systems.

The article outlines server and client responsibilities, message handling, model loading, and stopping the server when a test ends. It also discusses using tick or timer events to control request frequency and adapting the socket setup for container networking. The demonstrated trading rule is explicitly a simple example, not a validated strategy. The document emphasizes that practical deployment requires stability work and that the socket approach adds engineering complexity; it points to direct ONNX inference as an alternative for a later installment.

Key ideas

  • A Python inference server can supply forecasts to an MQL5 EA through a TCP socket.
  • The EA sends recent bar data and receives a forecast that drives a simple directional rule.
  • Separating inference from the terminal permits cross-language and cross-system deployment.
  • Socket communication requires careful handling of messages, connection lifecycle, and server shutdown.
  • The example is intended for backtesting demonstrations and does not establish a live-trading strategy.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.