Overview: Build an MT5 Expert Advisor that receives webhook alerts from TradingView (RayAlgo indicator) across three timeframes (H1, M5, M1) for XAUUSD and executes a dual-position strategy when all three conditions align, including strict FTMO server-request limits and automated pre-market-close trade closures.
1. Webhook Receiver & Multi-Timeframe State Logic:
H1 Bias State: Updates to BULLISH or BEARISH upon receiving an H1 webhook ( {"tf":"H1","dir":"BUY"} or "SELL" ). Persists until the opposite H1 webhook arrives.
M5 Setup State: Updates to BULLISH or BEARISH upon receiving an M5 CHoCH webhook ( {"tf":"M5","dir":"BUY"} or "SELL" ). Expires automatically after a configurable timeout (default: 45 minutes) if no entry triggers, or resets if an opposite M5 webhook arrives.
M1 Execution Trigger: When an M1 CHoCH/BOS webhook arrives ( {"tf":"M1","dir":"BUY"} or "SELL" ), verify that H1 State == M1 Direction AND M5 State == M1 Direction (and M5 setup is not expired). If true, execute the dual-position entry immediately and mark the current M5 setup as "used" so it cannot trigger duplicate entries on subsequent M1 alerts.
Webhook Connectivity: TradingView requires HTTPS (port 443) or HTTP (port 80) and enforces a 3-second timeout. Please include the complete receiving architecture (e.g., lightweight local reverse proxy or Python/Node webhook relay script) and clear setup instructions so MT5 receives POST requests reliably on Windows/VPS.
2. Dual-Position Entry & Risk Calculation:
Upon a valid trigger, open two simultaneous market orders on XAUUSD:
Stop Loss (Both Orders): Input in Pips (default: 110 pips, configurable 100–120 pips). Auto-detect broker digits and tick value so pip distance and lot sizing remain accurate across 2-digit or 3-digit Gold brokers.
Position 1 (Target 1 — Fixed 1:1):
Risk: 0.5% of account balance (configurable).
Take Profit: 1.0x SL distance (1:1 R:R).
Position 2 (Target 2 — Partial + Runner):
Risk: 0.5% of account balance (configurable).
Take Profit: Open runner (optional configurable max TP cap).
3. Automated Trade Management Rules:
Break-Even at Target 1: The instant Position 1 closes at its 1:1 Take Profit (or price reaches 1.0x SL distance), automatically modify Position 2's Stop Loss to Break-Even ( Entry Price + configurable offset points to cover commission/spread).
Partial Close at Target 2 (1:2 R:R): When Position 2 reaches 2.0x SL distance (1:2 R:R), automatically close 50% of its open volume.
Minimum Lot Fallback: If Position 2's initial lot size is 0.01 (cannot be split), close 100% at 1:2 R:R instead.
Runner Exit Options (Configurable Input):
Option 1: Leave open until Break-Even SL, Daily/Weekend Close, or manual close.
Option 2: Close remaining runner automatically if an opposite M5 or H1 webhook arrives.
Option 3: Optional trailing stop (in pips) activated only after Target 2 (1:2) is hit (must not modify every tick—use step increments to protect FTMO request limits).
4. Daily & Weekend Pre-Close Rule (Mandatory):
Close Before Daily Market Break & Weekend Close: Automatically close all open positions managed by this EA 5 minutes before XAUUSD daily market closure (typically 23:55 broker server time before the 1-hour daily rollover break) and 5 minutes before Friday weekend market closure.
Make the daily close time, Friday close time, and pre-close offset (default: 5 minutes) configurable in the EA inputs so they can be matched exactly to FTMO's server schedule ( SymbolInfoSessionTrade or manual HH:MM inputs).
Also block any new webhook entries during the last 15 minutes (configurable) before daily or weekend market close.
5. FTMO Compliance & Safety Filters (Inputs):
Server Request Limit Protection: Ensure order modifications ( OrderModify ) execute strictly once per state change (never in a tick loop) so total daily server requests stay well below FTMO's 2,000/day hyperactivity threshold.
Trading Session Window: Configurable Start Hour / End Hour (broker server time) to restrict entries to preferred sessions (e.g., London/NY).
Max Spread Filter: Block new entries if XAUUSD spread exceeds [X] points.
Max Concurrent Setups / Daily Loss Guard: Optional toggle to prevent opening a new setup while a previous setup is still active, plus an optional daily equity drawdown cut-off.
Unique Magic Number: Distinct magic numbers or order comments ( Pos1_T1 , Pos2_Runner ) so the EA reliably tracks both legs without interfering with manual trades.
6. Deliverables:
Compiled .ex5 EA file + full .mq5 source code.
Webhook relay/proxy files (if applicable) + step-by-step setup guide for TradingView alert JSON formatting and MT5 installation.