The existing Z-Score EAs test profitable on M1 OHLC but fail under Every Tick because they decide on individual ticks: noise fires the Z-exit at the worst price, market-order exits pay spread twice, and no minimum-edge check means quiet-market trades only ever pay spread. XAU_Quant_Reversion_TickRobust.mq5 closes that gap: - All decisions (entry, exits, breakeven, TP retarget) run exactly once per bar on closed-bar data, so OHLC and Every Tick converge - Cost gate: StdDev >= 3x and TP distance >= 4x the round-trip cost (live spread + commission points) or the trade is skipped - Server-side limit TP placed at the mean, retargeted per bar with a cost-covering floor (no spread paid on exit) - Turn-confirmation bar before entry (no knife-catching) - Volatility-aware SL: max(800 pts, 2.5x ATR) against tick spikes Docs: README section for the new EA, CLAUDE.md overview updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012knbN7oGPQ7K4TnRroZjWF
3.0 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
MetaTrader 5 Expert Advisor (EA) for automated gold (XAUUSD) trading. Written in MQL5.
Primary EA: XAU_Quant_Reversion_TickRobust.mq5 - Mean reversion built for Every Tick modelling: once-per-bar closed-bar decisions, round-trip cost gate, server-side limit TP at the mean (magic 777555)
Legacy EAs: XAU_Quant_Reversion_m1_OLHC.mq5 / XAU_Quant_Reversion_m1_EveryTick.mq5 - tick-level Z-Score scalpers (profitable on M1 OHLC modelling only)
Secondary EA: XAU_Quant_Reversion_Breakout.mq5 - Dual strategy combining mean reversion + Donchian channel breakout
Build & Deploy
- Copy
.mq5files to MetaTrader 5'sMQL5/Experts/folder - Open in MetaEditor and press F7 to compile (produces
.ex5binary) - Attach to GOLD/XAUUSD M1 chart with AutoTrading enabled
No external dependencies. MQL5 is compiled within MetaEditor.
Architecture
Core Strategy Logic (XAU_Quant_Reversion.mq5)
Entry: Z-Score measures how many standard deviations price is from its SMA. Enters when Z-Score exceeds threshold (±2.4) indicating statistical extremes.
Entry Filters (all must pass):
- ADX < 20 (market is ranging, not trending)
- Spread < 50 points
- ATR ratio between 0.5x-2.0x of 50-period average (volatility filter)
- Trading session window (10:00-20:00 broker time)
- No high-impact USD news (uses MQL5
CalendarValueHistoryAPI)
Exit Priority:
- Z-Score TP - closes when Z returns to ±0.3 (mean reversion complete)
- ATR trailing stop (new bar only, not every tick)
- Fixed SL (800 points - survives gold spikes)
- Hard TP (1500 points - server-side safety net)
Dynamic Risk Tiers (GetRiskPct(), GetDailyLossLimitPct()):
- Risk and daily loss limits auto-scale based on equity thresholds
- Aggressive at micro level (10% risk under $500), conservative as capital grows (1.5% risk above $20k)
Dual Strategy (XAU_Quant_Reversion_Breakout.mq5)
Combines mean reversion with Donchian channel trend breakout:
- Mean Reversion: same as primary EA (ADX < 20)
- Trend Breakout: Donchian channel breakout when ADX > 30, with EMA and DI+/DI- confirmation
Uses separate magic numbers (777333 for MR, 777444 for TB) to manage positions independently.
Key Constants
MAX_NEWS = 40- max high-impact news events cached per dayInpMagic = 777333- position identification- SL/TP in points (not pips) - 800 pts SL, 1500 pts hard TP
MQL5-Specific Notes
- Indicator handles created in
OnInit(), released inOnDeinit() - Position iteration via
PositionsTotal()/PositionGetTicket() - Order execution via
OrderSend()withMqlTradeRequest/MqlTradeResult - News data from
CalendarValueHistory()- onlyCALENDAR_IMPORTANCE_HIGH(red folder) is filtered - Fill mode detection: check
SYMBOL_FILLING_MODEfor FOK/IOC support
After Every EA Update
After every update to any .mq5 EA file, ensure this comment is the very last line of the file:
// This work is my worship unto GOD