- implement bullish and bearish gap detection - track setup validity across completed bars - confirm reversals using closed candles - calculate structural stops and risk-based targets - support manual and automatic position sizing - prevent overlapping EA-managed positions - validate market data and position properties - check trade-server return codes before reporting success - add Strategy Tester presets and project documentation
3.4 KiB
Larry Williams Oops Gap Reversal Expert Advisor
An MQL5 Expert Advisor that detects and trades Larry Williams' Oops gap reversal pattern in MetaTrader 5.
The EA identifies qualifying gap-up and gap-down setups, tracks one active setup for a limited number of bars, confirms the reversal using a completed candle, calculates the stop loss, take profit, and position size, and validates the trade-server response before reporting success.
This repository supports the MQL5 article:
Larry Williams Market Secrets (Part 16): Detecting and Trading the Oops Gap Reversal Pattern
Purpose
The Oops pattern is simple to describe, but applying it consistently requires several decisions:
- Whether the opening gap is large enough
- How long the setup should remain active
- Whether price has returned into the previous range
- Where the stop loss should be placed
- How the take profit should be calculated
- What trade volume should be used
- Whether another EA-managed position is already open
- Whether the trade server accepted the order request
This Expert Advisor converts those decisions into explicit MQL5 rules that can be compiled, inspected, and tested in the MetaTrader 5 Strategy Tester.
Strategy Rules
Bullish setup
A bullish Oops setup requires:
- The current bar opens below the previous bar's low.
- The gap is at least
minimumGapSizePoints. - The setup remains within
maxGapValidityBars. - A later completed candle closes at or above the stored previous low.
The EA then prepares a buy trade.
- Entry reference: current Ask price
- Stop loss: low of the original gap bar
- Take profit: entry price plus the risk distance multiplied by
riskRewardRatio
Bearish setup
A bearish Oops setup requires:
- The current bar opens above the previous bar's high.
- The gap is at least
minimumGapSizePoints. - The setup remains within
maxGapValidityBars. - A later completed candle closes at or below the stored previous high.
The EA then prepares a sell trade.
- Entry reference: current Bid price
- Stop loss: high of the original gap bar
- Take profit: entry price minus the risk distance multiplied by
riskRewardRatio
Confirmation method
This implementation uses closed-bar confirmation.
The gap bar cannot confirm itself. A later completed candle must close through the stored previous-range boundary before the EA generates a signal.
Main Features
- Gap-up and gap-down detection
- Configurable minimum gap size
- Configurable setup validity window
- Closed-bar reversal confirmation
- Persistent setup state across ticks and bars
- Stop loss based on the original gap bar
- Risk-reward-based take-profit calculation
- Manual position sizing
- Percentage-risk-based position sizing
- Broker volume-limit handling
- Long-only, short-only, or both-direction modes
- One EA-managed position at a time
- Checked symbol-property access
- Checked candle-data access
- Checked position-property access
- Checked
OrderCalcProfit()result - Trade-server return-code validation
- Runtime diagnostics in the Experts log
- Visual and historical Strategy Tester support
Requirements
- MetaTrader 5
- MetaEditor
- Historical data for the selected symbol and timeframe
- A demo account or Strategy Tester environment for testing
No external libraries are required.
The EA uses the standard MQL5 trading library:
#include <Trade\Trade.mqh>