指定
Overview
I need an MT5 Expert Advisor (MQL5) for a mechanical trading system.
It is a hedging account and must run as a true 3-position (3-leg) system (3 separate positions), not partial closes.
The EA must match my rules exactly and behave consistently in Strategy Tester and live charts (bar-close signal logic, no forward-looking).
Timeframe / Execution
- Signal timeframe: input
- All signals are evaluated on the last closed candle (bar1).
- Strong signals execute at the open of the new candle (market order at bar0 open).
- Weak signals are 1-candle expiry stop orders (virtual is fine, but must replicate stop behavior).
Indicators (EA must calculate internally from OHLC)
1) XC (Expansion/Contraction)
EA must compute:
- XC Short+ (PositiveShort)
- XC Short- (NegativeShort)
- XC Long+ (PositiveLong)
- XC Long- (NegativeLong)
2) WAD (Williams Accumulation/Distribution), WAD SMA
3) Keltner Channel (TradingView style)
Must match TradingView settings:
Entry Rules (Signals on bar t = bar1)
Strong Buy
Weak Buy (1-bar expiry STOP)
Strong Sell
Weak Sell (1-bar expiry STOP)
Reversal Handling (Important)
If there is an opposite direction trigger, the EA must:
- Close all current legs immediately at market
- Then open the opposite direction trade
Weak reversal only happens if the weak stop actually triggers.
Also handle edge case:
- If a weak stop from day t triggers on day t+1 AND day t+1 is also a strong signal day, the weak stop fill price takes priority (stop filled intrabar), not the strong close/open price.
3-Leg Trade Management
When trade opens, open 3 separate positions with same SL but different exits.
Define risk unit:
- R = KC(20,1) width = KC_upper(20,1) - KC_lower(20,1) on signal candle (bar1)
Stop Loss:
- Long SL = entry - R
- Short SL = entry + R
Leg exits:
- Leg 1 TP1 = entry ± 1R
- Leg 2 TP2 selectable by input:
- Mode A: 2R
- Mode B: KC(20,3) value from previous candle (bar1):
- Long TP2 = KC upper(20,3)[bar1]
- Short TP2 = KC lower(20,3)[bar1]
- Leg 3 TP3 trailing:
- TP3 activates once price reaches 3R.
- After 3R, trail by 25% retracement from best extreme since TP3 activation (keep 75% profit):
- For long: keep level = entry + 0.75*(highestHighSince3R - entry)
- For short: keep level = entry - 0.75*(entry - lowestLowSince3R)
- Stop should only tighten, never loosen.
Optional input:
- MOVE_BE_AFTER_TP1 (default true): after TP1 hits, move SL of legs 2 & 3 to breakeven.
Re-entry Lockout Logic (Very Important)
When flat, re-entry is restricted depending on how last trade closed:
If last trade did NOT hit TP3
Use XC Long series lockout:
- After a LONG trade: must see XC Long- > XC Long+ (bear phase) and then later XC Long+ > XC Long- (bull phase) before allowing new LONG trades. Until then, only SHORT setups are allowed.
- After a SHORT trade: must see XC Long+ > XC Long- then later XC Long- > XC Long+ before allowing new SHORT trades. Until then, only LONG setups are allowed.
If last trade DID hit TP3
Use XC Short series lockout instead (same two-step flip logic).
This must be coded as a small state machine (phase0 → phase1 → unlock).
Risk Sizing
Inputs:
- Capital M (e.g. 100000)
- Risk percent N (e.g. 1%)
- EA risks N% of M per trade, then splits into 3 legs automatically.
Must respect symbol min lot / step / max.
Visual Requirements (Strategy Tester)
When EA attached in Strategy Tester Visual mode:
- Apply chart template-like settings:
- White background, black foreground
- Bull candles white, bear candles black
- Bar up/down black
- Grid OFF
- Add indicators automatically (or embed indicator plots inside EA):
- XC with:
- Short+ green line, Short- red line
- Long area fill: green when Long+>Long-, red when Long->Long+
- WAD with:
- WAD green when WAD>WAD_SMA, red when WAD<WAD_SMA
- WAD_SMA black
NEW ENTRY METHOD OPTION: “Retracement Entry” (Trail-from-trigger, not fixed % of R)
Add an optional entry mode that replaces immediate entry with a retracement limit order.
Inputs
- USE_RETRACEMENT_ENTRY (bool)
- RETRACE_PCT (double, e.g. 0.50 for 50%)
When retracement is enabled
A “trigger event” still occurs (Strong signal or Weak trigger). On trigger:
- If holding opposite direction positions, close ALL 3 legs immediately at market.
- Do NOT enter immediately. Instead, start tracking the move from the trigger and place a retracement limit order derived from the post-trigger extreme.
What counts as a “trigger event”
- Strong Buy/Sell trigger: conditions met on bar1 close (normal strong signal timing).
- Weak Buy/Sell trigger: only when the 1-bar expiry stop is actually hit intrabar (weak setups that don’t trigger do nothing).
How retracement price is computed (trail-from-trigger extreme)
Define:
- TriggerPrice = the normal entry reference for that signal
- Strong: Close(bar1) (or the system’s strong reference price)
- Weak: the weak stop trigger price (e.g., 1.0015*Close(bar1) or 0.9985*Close(bar1))
After the trigger event, track the best extreme since trigger:
For LONG retracement
- Track ExtremeHigh = highest high since trigger
- Compute candidate retrace price:
- Q = ExtremeHigh - RETRACE_PCT * (ExtremeHigh - TriggerPrice)
- If Q > TriggerPrice, then queue at TriggerPrice instead (cap it):
- Q = TriggerPrice
- Place BUY LIMIT at Q
For SHORT retracement
- Track ExtremeLow = lowest low since trigger
- Compute candidate retrace price:
- Q = ExtremeLow + RETRACE_PCT * (TriggerPrice - ExtremeLow)
- If Q < TriggerPrice, then queue at TriggerPrice instead:
- Q = TriggerPrice
- Place SELL LIMIT at Q
One critical cancellation rule
If price reaches TP1 level (defined by the system) before the retracement limit order is filled, then:
- Cancel the limit order
- Treat the setup as “missed” (no position opens)
Exits stay based on the trigger reference (NOT the fill price)
Even though the fill may be better/worse, the SL/TP levels remain exactly as if we entered at TriggerPrice:
- R = KC(20,1)[bar1] width
- SL = TriggerPrice ± 1R
- TP1 = TriggerPrice ± 1R
- TP2 = TriggerPrice ± 2R OR KC(20,3)[prev] depending on mode
- TP3 activates at TriggerPrice ± 3R and trails per TP3 rules
Very important: lockout/re-entry logic must run on “trigger”, even if no fill happens
The system’s Re-entry Lockout Logic must continue running as if a trade “event” happened, even if the retracement limit never gets filled.
So the EA must record the trigger direction and trigger time and use it to update lockout state exactly as if the trade was taken.
Re-entry Lockout Logic (must remain intact):
If last trade/trigger did NOT hit TP3
- Use XC Long series lockout
- After a LONG: require XC Long- > XC Long+ then later XC Long+ > XC Long- before allowing LONG again
- After a SHORT: require XC Long+ > XC Long- then later XC Long- > XC Long+ before allowing SHORT again
If last trade/trigger DID hit TP3
- Use XC Short series lockout instead (same two-step flip logic)
Additional edge cases the EA must handle
- If an opposite trigger occurs while a retracement limit is pending:
- Cancel the pending limit
- Apply the opposite trigger rules (including close-all if needed)
- Then start a new retracement tracking/limit for the new trigger
Deliverables
- .mq5 EA source (clean, commented, no obfuscation)
- If needed: XC and WAD indicator source or integrated indicator plotting inside EA
- Must compile with 0 errors on latest MT5
- Instructions on where to place files and how to test
Testing
I will validate by:
- Running Strategy Tester on the same symbol/timeframe and comparing trades to my Python backtest logic.
I can provide:
- Example trades / reference logs
- The XC indicator source code (already have it)
- Expected behavior in tricky cases (weak+strong same day, reversal same day, etc.)