Skip to content
All library documents

Building Modular Trailing Stops for MQL5 Expert Advisors

Article MQL5 articles

Summary

The article presents a class-based framework for adding trailing stop logic to MQL5 expert advisors. It defines three common controls: the profit threshold that activates trailing, the price movement that triggers another adjustment, and the distance between the current price and the stop. Instead of a fixed distance, a trailing rule can use a price from an indicator or a prior bar. A base class accepts a candidate stop price and applies checks before modifying a position.

Those checks include respecting the symbol’s minimum stop distance, ensuring the new stop improves on the existing one, and applying any configured trailing conditions. Specialized classes cover indicator-based approaches such as Parabolic SAR and moving averages, as well as specified stop levels. Objects can be instantiated with different symbols and settings, or organized into collections through inheritance from the standard library’s base object. The article illustrates stops following prior candle highs and lows in a visual test, but reports no strategy performance; it explicitly leaves profitability to independent testing.

Key ideas

  • A general trailing framework can use an activation threshold, adjustment step, and distance from price.
  • A candidate stop must respect minimum stop distances and improve on the existing stop level.
  • Indicator values or prior bar prices can replace a fixed trailing distance.
  • Separate class instances allow different trailing rules and settings to run within one expert advisor.
  • The examples demonstrate implementation, not evidence that a particular trailing rule is profitable.

Tags

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