An automated trading Expert Advisor (EA) for MetaTrader 5, developed in MQL5.
The EA trades gold (XAUUSD) using a multi-timeframe breakout and retest strategy: it defines the first H4 candle of each day as a trading zone, detects breakouts on the M5 chart, waits for a retest, and executes trades with risk management, trailing stops, and optional news filtering.
- H4 → M5 breakout and retest trading logic
- Configurable fixed lot or risk-based position sizing
- Trailing stop management
- Smart news filter that avoids trading around high-impact events
- Spread and terminal safety checks
- Dynamic lot calculation based on risk percentage
-
Zone identification (H4)
The EA marks the first H4 candle of the current day and stores its high and low as the trading zone boundaries. -
Breakout detection (M5)
On each new M5 candle:- A bullish breakout occurs when the candle closes above
zoneHigh. - A bearish breakout occurs when the candle closes below
zoneLow.
When a breakout occurs, the EA starts waiting for a retest.
- A bullish breakout occurs when the candle closes above
-
Retest entry
- If price returns to the broken zone within a specified window (
MaxWaitSeconds):- Buy on bullish retest → SL = breakout candle low, TP = 1.5 × risk distance.
- Sell on bearish retest → SL = breakout candle high, TP = 1.5 × risk distance.
- Uses either a fixed lot (
Lots) or a risk-based lot (UseRiskPercent,RiskPercent).
- If price returns to the broken zone within a specified window (
-
Trade management and protection
- Trailing stop control (
UseTrailingStop,TrailingStart,TrailingStep). - Spread and trade-permission checks (
MaxSpreadPoints,TERMINAL_TRADE_ALLOWED). - Built-in news filter: the EA checks the MetaTrader 5 economic calendar (
CalendarValueHistory) and automatically avoids opening new trades within the defined window around high-impact news events for a selected currency (NewsFilterCurrency,NewsFilterMinutes). - This filter helps prevent entries during volatile periods and improves stability during major market announcements.
- Trailing stop control (
| Parameter | Description | Default |
|---|---|---|
Lots |
Fixed lot size per trade | 0.01 |
UseRiskPercent |
Enable dynamic lot sizing by risk | false |
RiskPercent |
Percent of balance risked per trade | 1.0 |
MaxWaitSeconds |
Maximum time allowed for retest | 86400 |
UseTrailingStop |
Enable trailing stop | true |
TrailingStart |
Profit (points) before trailing begins | 200 |
TrailingStep |
Distance (points) of trailing stop | 100 |
UseNewsFilter |
Enable high-impact news filter | true |
NewsFilterMinutes |
Minutes before/after news to avoid trading | 30 |
MagicNumber |
Unique identifier for EA positions | 202503 |
- MetaTrader 5 platform
- Broker supporting XAUUSD (gold) trading
- MQL5 environment (MetaEditor)
- Historical data for backtesting
- Open MetaEditor → File → Open Data Folder
- Copy the
.mq5file intoMQL5/Experts/ - Compile the EA
- In MetaTrader 5, open Navigator → Expert Advisors
- Attach the EA to a XAUUSD chart
- Adjust input parameters as needed
Symbol: XAUUSD
Timeframe: M5
Period: Jan 2022 – Oct 2025
Model: Every tick (MetaTrader 5 Strategy Tester)
This backtest covers almost three years of market data, including different volatility phases in gold.
The EA demonstrates consistent trade execution, controlled drawdown, and stable risk-adjusted returns under the default settings.
- Default parameters were used unless otherwise stated.
- The test includes realistic spreads and commissions.
- Results are for research and educational purposes only and do not guarantee future performance.