Building MetaTrader-Like Order Handling in a Python Strategy Tester
Summary
This installment describes implementing MetaTrader 5-style trade operations in a Python simulator. It centers on a single request interface modeled on the platform’s order-send function, with request fields specifying actions such as placing pending orders, opening or closing positions, and modifying or deleting orders. In tester mode, the simulator stores orders, positions, and deals in separate containers; outside tester mode, it forwards requests to the MetaTrader client.
The article explains how simulated trade records are created and highlights validation needed when closing positions, including checking that the requested trade direction is opposite the existing position and that the price matches the relevant bid or ask. It also describes a trade helper class for common operations. This is infrastructure for strategy testing rather than a trading strategy or performance study. The excerpt offers implementation examples, but does not establish that the simulator reproduces all broker execution rules or market conditions.
Key ideas
- A unified order-send interface can represent several kinds of trading requests.
- The simulator records pending orders, positions, and deals in separate histories.
- Closing a position requires checking its ticket, opposite trade direction, and valid bid or ask price.
- The code can forward requests to MetaTrader outside tester mode and simulate them internally during testing.
- The article covers simulator mechanics rather than strategy performance or execution fidelity.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.