Loading…

Entry Trigger Library

Most published entry-trigger code hardwires one specific condition straight into an EA's OnTick, which means comparing two trigger styles means maintaining two near-duplicate EAs. This library separates "what counts as an entry" from "what the EA does with an entry" by putting every trigger behind one fixed-shape struct (SEntryTrigger) and one evaluation function (EvaluateEntryTrigger). MQL4 has no function pointers, so this is not a callback table - it is enum-dispatch. Every trigger's configuration AND running state (an armed breakout level for a retest trigger, a tracked swing extreme for a pullback trigger) live directly on the SEntryTrigger instance the caller owns, and EvaluateEntryTrigger() switches on the instance's Type field to run the matching check. Six trigger kinds ship: two crosses (fast/slow series), two breakouts (rolling N-bar high/low), a retest (arms on breakout, fires on the return-and-hold) and a pullback (requires a minimum adverse excursion before the resumption counts, so it does not fire on every minor wiggle in the trend's favour). The retest and pullback triggers are the two that genuinely need memory between calls - a retest has to remember it already broke out and is now watching for the return, and a pullback has to remember how far price has already retraced. Because MQL4 gives no other natural home for per- instance state without a class hierarchy the language cannot express either (no virtual dispatch through function pointers), that state sits as plain fields on the same struct the caller already passes by reference every call - ResetTriggerState() clears it once a signal has been acted on. Notes and limitations - Every trigger evaluates against already-closed bars (signalShift >= 1 in the usual case) - none of them repaint, but a caller passing shift 0 is evaluating against the still-forming current bar, which will change its own answer as the bar completes. Pass shift 1 or higher for a stable signal. - The cross trigger takes fast/slow series values as plain doubles - it does not compute any indicator itself, so it works identically whether the caller feeds it two moving averages, an oscillator against its signal line, or two arbitrary price series. - RETEST and PULLBACK both hold state on the struct instance itself. Reusing the same instance for a different symbol/timeframe without calling ResetTriggerState() first carries over stale armed/extreme values from whatever it was previously tracking. - The retest trigger's "hold" check only looks at the closing price within tolerance of the broken level - a long wick through the level that closes back on the correct side still counts as holding, which matches how most discretionary retest reads work but is a deliberate simplification versus requiring the whole bar's range stay inside tolerance.

Script · MT5 · v1.0 · Free

About Entry Trigger Library

Entry Trigger Library is a script for MetaTrader 5 based on Moving Average. It has 5 settings, such as Scan bars, Breakout lookback and Retest tolerance points. MQL source code included.

Files in the download: EntryTriggerLibrary.mq5.mqh, EntryTriggerLibrary.txt, EntryTriggerLibraryDemo.ex5, EntryTriggerLibraryDemo.mq5.

Settings

SettingDefaultWhat it does
Scan bars200Bars to scan, oldest to newest
Breakout lookback20Lookback window for breakout/retest triggers
Retest tolerance points30How close price must return to the broken level
Pullback lookback8Bars back used as the pullback trigger's reference bar
Min pullback points80Minimum adverse excursion required before resume counts

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: Breakout · Moving Average · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria