Loading…

Exit Policy Library

Exit management code tends to accrete inside an EA's OnTick as one long if/else chain mixing trailing logic, time limits and partial-close rules together, which makes comparing exit styles on the same entry logic awkward. This library separates "what should happen to this trade right now" from "how the EA acts on it" the same way EntryTriggerLibrary.mqh in this catalogue separates entry conditions from entry execution: one fixed-shape struct (SExitPolicy) and one evaluation function (EvaluateExitPolicy) cover four policy kinds via enum-dispatch, since MQL4 has no function pointers to build a real callback table with. The library only recommends an action (EXIT_ACTION_NONE / MOVE_STOP / CLOSE_PARTIAL / CLOSE_FULL) - it never calls OrderModify or OrderClose itself. That keeps it usable identically whether the caller is live trading, running in the tester, or just logging what it would have done without touching a real order, and it keeps broker-specific concerns (stop-level validation, retry-on-requote) entirely in the caller's hands where this catalogue's house style already expects them to live. The target ladder is the policy with the most interesting state: it walks a fixed-size array of up to four (rMultiple, closeFraction) stages and fires at most one newly-reached, not-yet-hit stage per call, so a caller polling once per tick sees each stage as one distinct, individually actionable event rather than re-firing on a stage already handled. A stage whose closeFraction happens to be the entire remaining position (commonly the last rung of a ladder) is reported as EXIT_ACTION_CLOSE_FULL rather than a partial close, so the caller does not need special-case logic to detect "this partial actually closes everything". Notes and limitations - Trailing policies only ever tighten the stop, never loosen it - the library enforces this internally, so a caller does not need to guard against a computed trail distance temporarily suggesting a worse stop than the one already in place. - TARGET_LADDER's rMultiple is measured against initialRiskDistance, a price distance the caller sets once at policy configuration time (e.g. the distance from entry to the initial stop loss) - it is not recalculated if the caller later moves the actual stop, by design, so R-multiples stay anchored to the trade's original risk regardless of subsequent trailing. - TRAILING_ATR returns EXIT_ACTION_NONE while iATR has not yet produced a value greater than zero (e.g. immediately after attaching on a symbol with insufficient history) rather than trailing off a zero distance. - The demo's TARGET_LADDER stages are illustrative (1R/2R/3R closing 33/33/34 percent) - a real strategy's stage count, R-multiples and fractions should be chosen to match its actual risk model.

Script · MT4 · v1.0 · Free

About Exit Policy Library

Exit Policy Library is a script for MetaTrader 4 based on ATR. It has 5 settings, such as Replay bars, Fixed trail points and Atr period. MQL source code included.

Files in the download: ExitPolicyLibrary.mqh, ExitPolicyLibrary.txt, ExitPolicyLibraryDemo.mq4.

Settings

SettingDefaultWhat it does
Replay bars60Bars to replay, oldest to newest
Fixed trail points150TRAILING_FIXED distance, points
Atr period14TRAILING_ATR ATR period
Atr multiplier2.0TRAILING_ATR distance multiplier
Time stop seconds3600TIME_STOP max hold, seconds

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: ATR · Trailing Stop & Break-even · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria