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:
1. The current bar opens below the previous bar's low.
2. The gap is at least `minimumGapSizePoints`.
3. The setup remains within `maxGapValidityBars`.
4. 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:
1. The current bar opens above the previous bar's high.
2. The gap is at least `minimumGapSizePoints`.
3. The setup remains within `maxGapValidityBars`.
4. 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