Designing a Modular Engine for Five Trailing Stop Methods
Summary
The article describes an MQL5 architecture for applying different trailing stop rules to individual open positions. A shared interface lets each rule calculate a proposed stop price, while a central engine handles registration, evaluation, and modification requests. The methods covered are fixed pip distance, an ATR multiple, Parabolic SAR, a fraction of unrealized profit, and recent swing highs or lows.
The engine updates a stop only when the proposed level improves on the existing one by at least one symbol point, preventing backward movement and unnecessary broker requests. The examples explain pip-to-price conversion, indicator and position checks, and how an EA can assign methods to trades. The article also describes verification through a demo EA and a script with assertions. Method selection remains the strategy’s responsibility, and the design does not cover partial position sizes, splitting positions, or profit targets denominated in account currency. Its implementation examples are specific to MQL5 and require care around broker constraints and indicator availability.
Key ideas
- A common method interface allows new trailing rules to be added without changing the engine.
- Each trailing method returns a proposed stop price or a zero value when it cannot calculate a valid level.
- Fixed-distance, ATR-based, Parabolic SAR, profit-percentage, and swing-based trailing rules suit different trade behaviors.
- The engine modifies a stop only when it improves the current level by at least one point.
- The strategy must choose which trailing method to assign to each position.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.