Recoil Retrace Engine
- Experts
-
Syarief Azman Bin Rosli
Hello and welcome to my official MQL5 profile!
I am Syarief Azman Bin Rosli, an algorithmic trading systems developer and quantitative software engineer with over a decade of specialized experience creating robust automated trading solutions for MetaTrader 5 and MetaTrader 4. - Version: 1.1
- Updated: 31 October 2026
- Activations: 5
Recoil Retrace Engine is a MetaTrader 4 Expert Advisor that trades the pullback after a strong move. It finds an impulse leg that ran from a confirmed swing by a multiple of ATR, waits for price to recoil into a retracement pocket (default 38.2-61.8 %), enters in the direction of the leg, takes a partial at the leg extreme and runs the rest to an extension target. One position at a time per symbol and magic number; no grid, martingale, averaging or hedging.
Quick Start
- Attach the EA to one chart (H1 recommended, H4 and D1 also work) and enable AutoTrading plus "Allow live trading".
- Keep the defaults first: pocket 38.2-61.8, Entry Trigger = Rejection close, 1 % risk per trade.
- The panel shows SCANNING until a leg qualifies, then ARMED, IN POCKET and IN TRADE.
- Quick check: a visual Strategy Tester run on EURUSD H1 over two months draws every leg, pocket and target.
- Push alerts: set your MetaQuotes ID under Tools > Options > Notifications and Push Notifications = true.
How It Trades
Leg: a swing low (high) is a bar whose low (high) is beyond the Swing Strength bars on each side; it is confirmed once those bars have closed. For a buy leg the origin is the latest confirmed swing low within Swing Lookback bars and the extreme the highest high since then (mirrored for sells). The leg qualifies when it is at least Min Impulse Size x ATR (ATR Period, last closed bar) and took no more than Max Impulse Bars bars. If both sides qualify, the larger leg wins. A leg that already produced a trade is never armed again.
Trend filter: with Higher-Timeframe Trend Filter on, buys are allowed only while the last closed bar of the Trend Timeframe closes above its EMA (Trend EMA Period), sells only below it. Current = next higher: M1 to M5, M5 to M15, M15 to H1, M30 and H1 to H4, H4 to D1, D1 to W1, W1 and MN1 to MN1. With too little history on that timeframe the filter is neutral.
Recoil: retracement is measured from the extreme (0 %) to the origin (100 %). The pocket lies between Pocket Near Edge and Pocket Far Edge. A new extreme before entry extends the leg and moves the pocket. The setup ends INVALIDATED when a bar closes beyond the Invalidation Level, and EXPIRED when more than Setup Expiry bars have closed since the extreme.
Entry Trigger:
- Rejection close: a closed bar traded into the pocket, closed in the leg direction (bullish candle for buys), not beyond Pocket Far Edge, and in the half of its range away from the pocket. Market order at the next bar open; if the GATE is closed at that moment (for example the session opens later), it is retried until that bar ends.
- Pocket touch: market order the moment price reaches Pocket Near Edge. Fastest, no confirmation.
- Resume break: after a bar touched the pocket, buy when Ask breaks that bar's high (sell when Bid breaks its low). Each new touching bar moves the trigger.
A setup is skipped when the distance to TP1 is less than Min Reward:Risk to TP1 x the stop distance (SKIPPED: reward:risk, retried on later bars), when TP2 is closer than the broker stop distance (SKIPPED: target too close), or while the GATE is not OPEN.
Exits:
- Stop Loss Placement: Beyond invalidation level (Invalidation Level minus Stop Buffer x ATR for buys, plus for sells) or Beyond leg origin (origin minus the same buffer). Widened to the broker minimum distance when needed.
- TP1 is the leg extreme. There TP1 Partial Close percent of the lots is closed (rounded down to the lot step). If a part or the rest would fall below the minimum lot, nothing is closed (TP1 REACHED (no split)). 100 closes everything (TP1 FULL EXIT), 0 closes nothing.
- Breakeven at TP1 then moves the stop to the entry price.
- TP2 = extreme + TP2 Extension percent of the leg, set as the order's take profit. 0 puts TP2 at the extreme.
- Trail After TP1: on each new bar after TP1 the stop becomes last close minus value x ATR (buys), only ever tightened.
- Max Bars Before TP1 closes the trade at market when TP1 has not been reached within that many bars (TIME STOP).
- Closes and stop changes are sent only outside the broker freeze distance and inside the symbol's trading session; otherwise they are retried on the next tick.
Lot Sizing: Fixed lots trades Fixed Lots. Risk % of balance: lots = balance x Risk per Trade / 100 / loss per lot at the stop. Lots are rounded down to the lot step and capped by Max Lots; below the broker minimum the minimum lot is used, so small accounts may risk more. Before each order free margin is checked and the size halved until it fits (NO MARGIN if even the minimum does not).
Recommended: H1 on major pairs and gold; H4 and D1 trade less often. On M15 and lower the spread is large compared with ATR and Max Spread blocks most entries. Worth optimizing: Min Impulse Size, Swing Strength, both pocket edges, Entry Trigger, Min Reward:Risk to TP1 and Trail After TP1.
Panel Controls
- [-] / [+]: collapse the panel into a pill (RECOIL plus the state) or expand it.
- <>: move the panel to the next chart corner (top-left, top-right, bottom-right, bottom-left).
- PAUSE / RESUME: stop or allow new entries; an open trade is still managed. Saved in a global variable, so it survives restarts, timeframe changes and re-attaching.
- ZONES ON / ZONES OFF: show or hide the chart overlays. Collapse, corner and zones return to the input values when the EA is re-attached.
Panel Readouts
- Header LED: green = GATE OPEN, amber = PAUSED, coral = entries blocked.
- ENGINE STATE: badge SCANNING (no leg), ARMED (leg found, waiting for the recoil), IN POCKET (price touched the pocket, waiting for the trigger), IN TRADE. Right: BUY SIDE, SELL SIDE or NO LEG. TREND: timeframe, EMA period and BULL, BEAR, NEUTRAL or OFF. GATE: OPEN, PAUSED, ALGO TRADING OFF, MARKET CLOSED, OUT OF SESSION or SPREAD HIGH.
- RECOIL GAUGE: DEPTH is the current retracement in percent; the marker runs on a track that is cyan up to the near edge, amber across the pocket, dark amber to the invalidation level and coral beyond. LEG shows the leg size in ATR and its bars. WAITING FOR AN IMPULSE when no leg exists.
- LEVELS: TP2, TP1, POCKET (price range), INVALID and STOP with the distance from Bid in pts. In a trade TP2 and STOP come from the order and TP1 shows DONE once handled.
- POSITION: FLAT or side, lots, entry price and floating profit in account currency; stage TP1 PENDING, TRAILING or RUNNER; points and bars in trade. When flat: WAITING FOR SETUP or NEW ENTRIES PAUSED.
- ACTIVITY: the last three events with server time: ARMED, IN POCKET, INVALIDATED, EXPIRED, ENTRY, TP1 PARTIAL, TP1 FULL EXIT, TP1 REACHED (no split), BREAKEVEN, TIME STOP, CLOSED, ORDER FAILED, NO MARGIN, SKIPPED: reward:risk, SKIPPED: target too close, PAUSED by user, RESUMED by user.
- Deck line: TRADES (closed trades, partial closes counted once), WIN percent and NET closed profit including swap and commission, for this symbol and magic number.
Chart Overlays
The leg line (cyan for buys, coral for sells) runs from ORIGIN to the extreme. An amber box with a dotted near-edge line marks the POCKET, a dashed coral line INVALID, a dotted green line TP1 EXTREME and a dash-dot green line TP2 +27.2 (your extension). They reach to the setup expiry and are drawn behind the candles.
Alerts
Pop-up Alerts and Push Notifications fire when a setup is ARMED, on ENTRY, at TP1 and when the trade is CLOSED. They are skipped in the Strategy Tester.
Side Effects
- Global variable RRE_PAUSE_<symbol>_<magic> = 1 while paused.
- Global variable RRE_T1_<symbol>_<magic>_<open time> holds the TP1 price of the open trade (positive while pending, negative once handled) and is deleted after the trade closes. If it is missing, TP1 counts as handled: no partial and no breakeven, trailing still works.
- Global variable RRE_LEG_<symbol>_<magic> stores the origin time of the last traded leg (negative for sell legs), so a restart never trades the same leg twice.
- Chart objects prefixed RRE_P_ (panel) and RRE_Z_ (overlays), removed with the EA. A one-second timer refreshes the panel. No files, no DLLs.
bool paused = GlobalVariableCheck("RRE_PAUSE_EURUSD_26103101"); Input Parameters
Impulse Leg
- Swing Strength (2): bars on each side of a swing.
- Min Impulse Size (2.0): minimum leg in ATR.
- Max Impulse Bars (20): most bars from origin to extreme.
- Swing Lookback (120): bars searched for the origin.
- ATR Period (14).
Pocket and Entry
- Pocket Near Edge (38.2) and Pocket Far Edge (61.8): pocket in percent of the leg.
- Invalidation Level (78.6): close beyond it cancels the setup.
- Entry Trigger (Rejection close): Rejection close, Pocket touch or Resume break.
- Setup Expiry (30): bars after the extreme.
- Min Reward:Risk to TP1 (0.5): 0 = off.
Trend Filter
- Higher-Timeframe Trend Filter (true).
- Trend Timeframe (Current = next higher).
- Trend EMA Period (50).
Exits
- Stop Loss Placement (Beyond invalidation level): or Beyond leg origin.
- Stop Buffer (0.25): extra distance in ATR.
- TP1 Partial Close (50): percent closed at TP1.
- Breakeven at TP1 (true).
- TP2 Extension (27.2): percent of the leg beyond the extreme.
- Trail After TP1 (1.5): ATR multiple, 0 = off.
- Max Bars Before TP1 (0): 0 = off.
Money Management
- Lot Sizing (Risk % of balance): or Fixed lots.
- Fixed Lots (0.10).
- Risk per Trade (1.0): percent of balance.
- Max Lots (5.0).
Filters and Execution
- Max Spread (30): percent of ATR, 0 = off.
- Session Start Hour (0) and Session End Hour (0): server hours for new entries; equal values = 24 h; may cross midnight.
- Magic Number (26103101): use a different one per chart on the same symbol.
- Max Slippage (30): points.
- Order Comment (Recoil).
Display and Alerts
- Show Panel (true), Draw Leg and Pocket (true), Start Collapsed (false), Panel Scale (1.0, 0.5-2.5).
- Pop-up Alerts (true), Push Notifications (false).
Typical Setups
- Balanced H1 (majors, gold): all defaults.
- Confirmed gold H1: Entry Trigger = Resume break, Min Impulse Size 2.5, Trail After TP1 2.0.
- Patient H4/D1 swing: Swing Strength 3, Pocket Near Edge 50, Stop Loss Placement = Beyond leg origin, Min Reward:Risk to TP1 0.8.
- Fast exits: TP1 Partial Close 100, Max Bars Before TP1 24, Breakeven at TP1 false.
Notes
- After a restart the setup is rebuilt from history on the next bar; an open trade continues with its TP1 record.
- The Strategy Tester uses the same rules; the panel and overlays appear only in visual mode.
- Test on a demo account first.
Risk Disclaimer
Trading forex and CFDs on margin carries a high risk of loss. Past or simulated results do not guarantee future results. This EA does not promise profit; use only capital you can afford to lose.
