PROJECT: GOLD (XAUUSD) TREND-FOLLOWING SUPPLY & DEMAND EXPERT ADVISOR
PLATFORM: MetaTrader 5
PROGRAMMING LANGUAGE: MQL5
TRADING INSTRUMENT: XAUUSD / Gold Spot ONLY
OBJECTIVE:
Develop a fully automated XAUUSD trading Expert Advisor based on higher-timeframe market structure, supply and demand zones, H1 21/55 EMA trend confirmation, and M15 entry confirmation.
MARKET PHASE:
The EA must analyze H1, H4 and D1 market structure.
UPTREND:
Higher Highs (HH) and Higher Lows (HL).
DOWNTREND:
Lower Lows (LL) and Lower Highs (LH).
RANGE:
No clear HH/HL or LL/LH structure.
The EA must NOT open new trades when the market is classified as ranging or when the trend structure is unclear.
SUPPLY & DEMAND:
The EA must automatically identify valid supply and demand zones on H1, H4 and D1.
Zones must be treated as PRICE AREAS/RANGES, not single horizontal lines.
A valid zone should be based on:
- Clear historical reaction
- Meaningful displacement away from the area
- Previous respect/retest
- Clearly defined upper and lower boundaries
The developer must create objective programming rules for zone formation, strength, retests and invalidation and discuss these rules before implementation.
H1 EMA FILTER:
21 EMA and 55 EMA.
BUY:
H1 21 EMA must be above H1 55 EMA.
SELL:
H1 21 EMA must be below H1 55 EMA.
BUY ENTRY:
1. Higher-timeframe XAUUSD structure indicates an uptrend.
2. H1 21 EMA is above H1 55 EMA.
3. Price reaches a valid demand zone on H1, H4 or D1.
4. Wait for the first COMPLETED bullish M15 candle formed at or within the demand zone.
5. Enter BUY at the close of that M15 confirmation candle.
6. Do not enter while the M15 candle is still forming.
BUY STOP LOSS:
Default = 100 pips below the OPEN price of the first bullish M15 confirmation candle.
The demand-zone boundary must also be considered when determining the final stop-loss location.
BUY BREAKEVEN:
Move the stop-loss to the original entry price when the trade reaches +100 pips profit.
Once breakeven is reached, the stop-loss must never be moved backward.
BUY EXIT:
Close the BUY position when EITHER:
1. XAUUSD reaches/enters a valid supply zone
OR
2. H1 21 EMA crosses below the H1 55 EMA.
SELL ENTRY:
1. Higher-timeframe XAUUSD structure indicates a downtrend.
2. H1 21 EMA is below H1 55 EMA.
3. Price reaches a valid supply zone on H1, H4 or D1.
4. Wait for the first COMPLETED bearish M15 candle formed at or within the supply zone.
5. Enter SELL at the close of that M15 confirmation candle.
6. Do not enter while the M15 candle is still forming.
SELL STOP LOSS:
Default = 100 pips above the OPEN price of the first bearish M15 confirmation candle.
The supply-zone boundary must also be considered when determining the final stop-loss location.
SELL BREAKEVEN:
Move the stop-loss to the original entry price when the trade reaches +100 pips profit.
Once breakeven is reached, the stop-loss must never be moved backward.
SELL EXIT:
Close the SELL position when EITHER:
1. XAUUSD reaches/enters a valid demand zone
OR
2. H1 21 EMA crosses above the H1 55 EMA.
RISK MANAGEMENT:
Default risk per trade = 5% of account equity.
Risk percentage MUST be adjustable through EA settings.
The EA must automatically calculate lot size based on:
- Account equity
- Risk percentage
- Stop-loss distance
- XAUUSD tick value
- XAUUSD tick size
- Contract size where applicable
- Broker minimum volume
- Broker maximum volume
- Broker volume step
The EA must NOT use a fixed lot size.
XAUUSD CALCULATIONS:
The EA must use the actual XAUUSD symbol specifications supplied by the broker.
Do NOT use hard-coded Forex assumptions for pip size, tick size, tick value, contract size or volume.
The 100-pip stop-loss and 100-pip breakeven distances must be correctly converted according to the broker's XAUUSD specifications.
TRADE PROTECTION:
- XAUUSD ONLY.
- No duplicate entries from the same signal.
- No multiple entries from the same M15 confirmation candle.
- No entry from an invalid supply/demand zone.
- No new trades during ranging conditions.
- No entry without the required EMA/trend confirmation.
- Respect broker minimum/maximum/step volume.
- Never move stop-loss backward after breakeven.
- Use a unique Magic Number.
- Handle spread, execution errors and trading-session restrictions safely.
EA INPUTS:
RiskPercent = 5.0
FastEMA = 21
SlowEMA = 55
ConfirmationTimeframe = M15
BuyStopLossPips = 100
SellStopLossPips = 100
BuyBreakevenPips = 100
SellBreakevenPips = 100
UseSupplyDemandExit = true
UseEMAExit = true
AllowBuyTrades = true
AllowSellTrades = true
XAUUSDOnly = true
MagicNumber = adjustable
VISUAL FEATURES:
The EA should optionally display:
- Supply zones
- Demand zones
- 21 EMA
- 55 EMA
- Buy/sell entries
- Stop-loss
- Breakeven status
- Current market phase
- Zone timeframe/source
BACKTESTING:
The EA must be tested specifically on XAUUSD using the MT5 Strategy Tester.
Testing must include:
- Trending markets
- Ranging markets
- Reversals
- High-volatility Gold periods
- Supply/demand detection
- M15 confirmation
- Position sizing
- 100-pip stop-loss
- 100-pip breakeven
- Supply/demand exits
- EMA exits
- Duplicate-entry prevention
Provide backtest results including:
- Net profit
- Profit factor
- Maximum drawdown
- Win rate
- Number of trades
DEVELOPMENT REQUIREMENT:
Subjective concepts such as "valid zone", "bounce", "strong reaction", "higher high", "higher low", "lower high", "lower low", "displacement" and "zone break" must be converted into objective, measurable programming rules.
The developer MUST explain the proposed supply/demand detection algorithm and obtain approval before implementing it.
The developer must NOT replace this strategy with a generic EMA crossover or generic supply/demand strategy.
DELIVERABLES:
1. Fully functional .mq5 source code.
2. Compiled .ex5 file.
3. No compilation errors or warnings.
4. XAUUSD Strategy Tester verification.
5. Explanation of the supply/demand detection algorithm.
6. Explanation of all EA inputs.
7. Backtest report.
8. Installation/use instructions.
9. Confirmation that the EA is restricted to XAUUSD.
IMPORTANT:
The attached Gold/XAUUSD TradingView chart is a visual reference for how I identify supply and demand areas. The orange areas represent ZONES/RANGES, not single price levels. The developer must use the chart as a reference for the methodology and must NOT hard-code the displayed price levels.