Loading…

Regime Gate Library

A regime filter built on a single threshold ("trade only when ADX > 25") flips open and closed repeatedly whenever the metric parks right around that line, because there is nothing stopping the very next bar's small wiggle from crossing back. RegimeGateLibrary gives every filter TWO thresholds instead of one: enterLevel opens a currently-closed gate, exitLevel closes a currently-open one, and the two are set on opposite sides of the obvious midpoint (e.g. ATR volatility might enter at 150 points and only exit below 100) so a metric sitting between them simply holds whatever state the gate was already in. This is the standard hysteresis-band technique borrowed from control systems, applied here to keep an EA from re-evaluating "is the regime tradeable" into a different answer every single bar near the boundary. Same enum-dispatch shape as EntryTriggerLibrary.mqh and ExitPolicyLibrary.mqh in this catalogue: one fixed-shape SRegimeGate struct covers every filter type's parameters and its running open/closed state, and EvaluateRegimeGate() switches on the instance's Type field - MQL4 has no function pointers to build a genuine plugin table with, so this is the closest equivalent the language supports. Four filter kinds ship: ATR volatility, ADX trend strength, moving-average slope, and a plain session-time hour-of-day window (which has no metric to hysteresis over, since hour boundaries are already discrete - it is routed through the same isOpen state field purely for a uniform caller-facing shape). CRegimeGateSet combines several independently-configured gates with AND logic - every gate must be open for the combined set to be open - and evaluates every gate on every call (not short-circuiting on the first closed one) so each gate's own hysteresis state stays current even while the combined verdict is CLOSED. Notes and limitations - enterLevel/exitLevel are set by the caller per gate - the library does not validate that they sit on the "correct" side of each other for the chosen higherIsMoreFavourable direction. Configuring exitLevel above enterLevel on a higher-is-better metric produces a gate with no hysteresis band at all (it will flip on nearly every threshold cross) rather than raising a compile or runtime error. - MA_SLOPE's enter/exit levels are in points, not normalised by volatility - the library deliberately does not couple it to the ATR gate internally, so a slope threshold tuned on one symbol will not transfer directly to a symbol with very different typical price movement without re-tuning. - SESSION_TIME uses server time (TimeCurrent()), which is the broker's server clock, not the trader's local time zone - convert the desired local hours to server hours before setting sessionStartHour/EndHour. - CRegimeGateSet::GateAt() returns a gate BY VALUE (a copy) - mutating the returned struct does not affect the gate held inside the set. Use EvaluateAll() to actually update the set's own gates.

Script · MT5 · v1.0 · Free

About Regime Gate Library

Regime Gate Library is a script for MetaTrader 5 based on Moving Average, ADX and ATR. It has 9 settings, such as Replay bars, Atr period and Atr enter points. MQL source code included.

Files in the download: RegimeGateLibrary.mq5.mqh, RegimeGateLibrary.txt, RegimeGateLibraryDemo.ex5, RegimeGateLibraryDemo.mq5.

Settings

SettingDefaultWhat it does
Replay bars40Bars to replay, oldest to newest
Atr period14ATR gate indicator period
Atr enter points150ATR gate: opens above this many points
Atr exit points100ATR gate: closes below this many points
Adx period14ADX gate indicator period
Adx enter level25ADX gate: opens at/above this reading
Adx exit level18ADX gate: closes below this reading
Session start hour7Session gate: opens at this server hour
Session end hour19Session gate: closes after this server hour

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Moving Average · ATR · ADX · Trend · Trading Sessions · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria