仕事が完了した
指定
An MT5 Expert Advisor that implements UT-Bot ATR trailing logic, flips position on opposite signals (close buy when sell appears, close sell when buy appears), and adds multi-layer filters, trend confirmation, risk management, and robustness features so it performs well on volatile pairs XAUUSD and BTCUSD.
1 — Core design & signal rules
-
Trail calculation (internal, no external indicator required):
-
ATR-based trailing line (UT Bot style).
-
Inputs: ATR_Period (default 10), ATR_Multiplier (default 2.0).
-
Calculate basic upper/lower, finalUpper/finalLower with persistence, determine trend (UP/DOWN) and trail value.
-
-
Signal (action taken only at bar close):
-
Buy when Close > trail and trend == UP .
-
Sell when Close < trail and trend == DOWN .
-
-
Flip behavior:
-
On opposite signal, always close the current position.
-
If ReverseOnOppSignal == true and filters pass → open the opposite position immediately.
-
If ReverseOnOppSignal == false → close only, do not enter new first.
-
-
Execution moment: Default = act only on new bar (bar-close). Optional intra-bar mode for advanced users (not recommended without careful testing).
2 — Robust entry filters (all must pass to open new trades)
-
Session filter — TradeStartHour / TradeEndHour (default 03:00–18:00 server time).
-
Spread filter — SymbolInfoInteger(SYMBOL_SPREAD) <= MaxSpreadPoints (default e.g., 30 points; user-configurable).
-
Volatility filter — ATR * point >= MinATRPoints (default 0 to disable; set to avoid ultra-low volatility).
-
Trend confirmation (high-quality filter) — Higher timeframe EMA rule:
-
HTF = H1 (configurable)
-
EMA_fast = 50 (HTF) optional: require EMA50 slope positive for buys, negative for sells.
-
Alternatively, ADX > ADX_Threshold (default 20) for trend strength.
-
-
Candle body filter — require abs(Close - Open) >= MinBodyPoints to reduce fake signal entries.
-
Daily trade cap / per-symbol cap — MaxTradesPerDay or MaxOpenTrades to limit churn.
-
News filter (optional) — Integrate calendar (or allow developer to provide hook) to block high-impact events.
Important: Filters should not block the mandatory close on opposite signals — close must always be allowed. Filters only stop new entries.
3 — Trade management & sizing
-
Position policy: Single position per symbol (no hedging). Keep track by MagicNumber .
-
Lot sizing options (configurable):
-
Fixed Lot (e.g., 0.02) — simple and safe for small accounts.
-
Risk % per trade — compute lot from:
Lot = (AccountBalance * RiskPercent/100) / (StopLossInMoneyPerLot)
(EA must use instrument contract specifications to compute StopLossInMoneyPerLot precisely). -
ATR-scaled lot — scale position size by ATR to keep dollar volatility roughly constant.
-
-
Stop Loss / Take Profit:
-
Primary mode: No fixed SL/TP (UT Bot style) — close on opposite signal.
-
Safety mode: Catastrophic SL (recommended) — attach a hidden SL at CatSL = EntryPrice ± CatSL_ATR_Mult * ATR (default 6×ATR) to prevent extreme losses.
-
Optional: user can enable fixed SL/TP or trailing stop.
-
-
Partial close on flip: Option to partially close (e.g., close 50% when opposite signal, open reverse with remaining size or new sized position).
-
Max drawdown safeguard: StopTradingIfDrawdownPercentReached (pauses trading).
4 — Reliability & edge-case handling
-
Bar-close processing (default): check iTime change, then act — prevents repaint/false mid-bar actions.
-
Order retries: on trade send/close failure, retry N times with small delays; log errors.
-
Trade context busy: loop until free with timeout.
-
Slippage tolerance: configurable Slippage .
-
Requote handling: avoid infinite loops; fallback to market order attempts.
-
Logging & on-chart panel: show trail value, trend, spread, ATR, last action; log to file for analysis.
-
Unit tests / debug modes: add DebugMode to simulate entries without placing real trades.
5 — Inputs to expose to user (recommended defaults)
-
MagicNumber = 123456
-
Timeframe = PERIOD_M15 (user-selectable; M5/M15/M30 suggested)
-
ATR_Period = 10
-
ATR_Multiplier = 2.0
-
ReverseOnOppSignal = true
-
TradeStartHour = 3
-
TradeEndHour = 18
-
MaxSpreadPoints = 30
-
MinATRPoints = 0
-
MinBodyPoints = 10 (points; 0 disables)
-
UseEMAConfirmation = true
-
EMA_HTF = PERIOD_H1
-
EMA_Period = 50
-
UseADXFilter = false
-
ADX_Period = 14
-
ADX_Threshold = 20
-
Lots = 0.02 (default fixed)
-
UseRiskPercent = false
-
RiskPercent = 1.0
-
UseCatSL = true
-
CatSL_ATR_Mult = 6.0
-
MaxTradesPerDay = 10
-
OneTradePerBar = true
-
Slippage = 3
6 — Recommended parameter sets (starter presets)
A — Conservative (small account / low risk)
-
Balance: small (≤ $500)
-
Lots = 0.01 or UseRiskPercent = true & RiskPercent = 0.5%
-
Timeframe = M15
-
ATR_Period = 10 , ATR_Mult = 2.0
-
UseEMAConfirmation = true (EMA50 H1)
-
UseCatSL = true , CatSL_ATR_Mult = 6
-
MaxTradesPerDay = 5
B — Balanced (most traders)
-
Balance: ~$1,000–5,000
-
UseRiskPercent = true , RiskPercent = 1.0%
-
Timeframe = M15 (XAU) / M5–M15 (BTC)
-
UseEMAConfirmation = true , ADX_Threshold = 20
-
MaxTradesPerDay = 10
C — Aggressive (experienced)
-
Balance: > $5,000
-
RiskPercent = 1.5–2%
-
Timeframe = M5 (more signals)
-
ReverseOnOppSignal = true
-
EnablePartialCloseOnFlip = true
-
MaxTradesPerDay = 20
Note: Always test on demo/live small balance before scaling.
7 — Backtesting & optimization plan (must be delivered)
-
Historical backtest on both symbols, at least 3–5 years for XAUUSD, 2–3 years for BTCUSD (where data exists).
-
Forward walk-forward testing: optimize on training window, validate on out-of-sample window.
-
Monte Carlo / randomization: test sensitivity to slippage, spread, execution delay, and parameter jitter.
-
Robustness checks: parameter stability, worst-case spread, max slippage, news days.
-
Report to include: net profit, profit factor, max drawdown, CAGR, Sharpe-like measure, total trades, average trade length, win rate, expectancy.
8 — Acceptance criteria for delivery
-
EA compiles in MetaEditor without errors; source .mq5 + compiled .ex5 provided.
-
Implements UT Bot trail internally (no external indicator dependency).
-
Correct flip behavior: closes opposite trade on opposite signal; opens reverse if ReverseOnOppSignal==true .
-
Filters work as specified and block entries but never block closes.
-
Safety features (CatSL, MaxDrawdownStop) work and are configurable.
-
Works on XAUUSD and BTCUSD out-of-the-box using default presets.
-
Includes basic GUI or logs showing active settings and last signal.
-
Provide test reports: at minimum a backtest summary for each symbol and timeframe used.
-
Provide short user guide (1–2 pages) explaining inputs and presets.
9 — Deliverables to ask the developer
-
Source code .mq5 and compiled .ex5 .
-
A 1–2 page PDF user manual with default presets (Conservative, Balanced, Aggressive).
-
Backtest result files (strategy tester reports) for XAUUSD and BTCUSD.
-
A short video (optional) showing EA loaded and running on demo account for basic verification.
-
Support period (e.g., 7–14 days) for bug fixes after delivery.
10 — Additional advanced ideas (v2+)
-
Implement machine-learned filter or pattern recognition for false-signal reduction.
-
Add position-scaling (pyramiding) limited to trend strength.
-
Integrate news API or timezone-aware economic calendar to pause trading.
-
Add correlation filter (avoid trading both XAU and BTC at same time if correlation spikes).
-
Implement trade simulator / sandbox mode inside EA.
11 — Realistic performance expectations
-
Win rate typically 60-80% (trend systems vary).
-
Profit factor target after optimization: ≥ 1.5 (good), ≥ 2.0 (very good).
-
Max drawdown depends on risk settings — keep risk per trade low (≤ 1–1.5%) to maintain acceptable drawdown.
-
No guarantee — past backtest performance does not guarantee future live results. Always forward-test.