Loading…

Optimal Trailing Distance Sweep

Trailing stop distance is usually picked by habit - "30 pips always works for me" - without ever comparing it against the alternatives on the same trade history. This script does the comparison directly. Entries and reversals come from a simple, fully mechanical moving-average crossover, computed once from real bar history. That signal set is then replayed once per candidate trailing distance in the requested sweep range, each time starting from a flat position and applying only that one distance's trailing-stop logic bar by bar. Keeping the signal fixed and varying only the trailing distance is the part a naive version gets wrong - if the trailing distance were allowed to also influence when a trade opens, the sweep would be comparing different trade sets rather than isolating the effect of the exit distance, and the "best" result could just be reflecting noise in entry timing. For every distance tested the script reports total profit in pips, the number of trades taken and the average profit per trade, then names the single best-performing distance across the whole sweep. Notes and limitations The signal is a plain SMA crossover chosen to give every distance in the sweep an identical, reproducible set of trade timings - it is not presented as a profitable strategy on its own, and results should be read as "how did trailing distance affect this particular fixed signal", not as a general trading recommendation. The replay ignores spread, commission and slippage - every entry and exit is simulated at the bar's own High/Low/Close. Real trading costs would shift every distance's total down by a similar amount, so the RELATIVE ranking across distances is the useful output, more than the absolute pip totals. A position open at the end of the replay window is marked to the final bar's close rather than left out of the total, so every candidate distance is scored on a consistent, fully realised profit figure. This is a historical replay over one fixed sample. The best distance found here reflects what happened on this instrument, timeframe and period specifically - re-running the sweep on a different history window can find a different optimum, particularly if volatility regime changed.

Script · MT4 · v1.0 · Free

About Optimal Trailing Distance Sweep

Optimal Trailing Distance Sweep is a script for MetaTrader 4 based on Moving Average. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as Timeframe, Fast ma period and Slow ma period. MQL source code included.

Files in the download: TrailingDistanceSweep.mq4, TrailingDistanceSweep.txt.

Settings

SettingDefaultWhat it does
TimeframePERIOD_H1Timeframe to replay
Fast ma period10Fast SMA period (entry/reversal signal)
Slow ma period40Slow SMA period (entry/reversal signal)
Max bars5000Most recent bars to replay
Min trail pips10.0Smallest trailing distance to test
Max trail pips100.0Largest trailing distance to test
Step pips10.0Step between tested distances
Save csvtrueWrite the full sweep table to a CSV file

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · Trailing Stop & Break-even · Alerts · Moving Average · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria