1. Overview • Platform: MetaTrader 5 (MT5) • Type: Expert Advisor (EA) • Symbols: Must work on any symbol (not limited to GBPUSD / USDJPY). • Timeframe: Use the chart timeframe (no internal timeframe change). • Data Source: ClusterDelta “#Footprint” (or similar), using Bid / Ask volume per price level for each completed candle. • Direction: Long and Short, both supported. • Execution Timing: All conditions are checked only on bar close (no intrabar trading logic). The EA trades reversals after a trend leg using footprint volume (Bid/Ask) and a daily range filter. ⸻ 2. Daily Range & Trading Session 2.1 Daily Range Definition • Input: • RangeStartTime (time of day, broker server time) • Default: 09:00 • From RangeStartTime of the current trading day up to “now”, calculate: • DayHigh = highest High of all candles since RangeStartTime • DayLow = lowest Low of all candles since RangeStartTime This defines the intraday range. 2.2 Range Filter On/Off • Input: • UseRangeFilter (bool) • true = trade only when price is outside the daily range • false = ignore range; trade both inside and outside the range • When UseRangeFilter = true: • For a long setup, the candle body of the last trend candle must close below DayLow (not just a wick). • For a short setup, the candle body of the last trend candle must close above DayHigh. No range restriction when UseRangeFilter = false. ⸻ 3. Trading Session Filter • Inputs: • SessionStartHour (time of day, server) – default 16:00 • SessionEndHour (time of day, server) – default 02:00 (next day) Logic: • EA opens new trades only if current server time is within the trading session. • Session may cross midnight (e.g. 16:00 → next day 02:00). Developer must handle wrap-around: • If SessionStartHour < SessionEndHour: normal same-day interval • If SessionStartHour > SessionEndHour: session = [SessionStartHour → 23:59:59] UNION [00:00 → SessionEndHour] ⸻ 4. Risk & Money Management 4.1 Risk per Trade • Inputs: • RiskPercent – risk per trade as % of account balance • Allowed range: 2% – 50% • Default: 5% • Calculation: • MaxLossMoney = AccountBalance * RiskPercent / 100.0 4.2 Stop Loss Distance & Filter • Inputs: • SLBufferPipsMin, SLBufferPipsMax • Both adjustable; lower bound must be ≥ 0 • EA must use an internal value SLBufferPips that stays in [SLBufferPipsMin, SLBufferPipsMax] • MaxSLDistancePipsMin, MaxSLDistancePipsMax • EA uses MaxSLDistancePips within that range • If computed SL distance (in pips) is greater than MaxSLDistancePips, EA must skip the trade (no order). 4.3 Risk–Reward Ratio • Inputs: • RRMin, RRMax • Must allow at least 1.1 to 5.0 • EA uses RR (Risk–Reward ratio) within [RRMin, RRMax]. • Take Profit: • TPDistancePips = SLDistancePips * RR All numeric inputs must have both lower and upper bounds, with lower bound 0 (or appropriate positive minimum) as discussed. ⸻ 5. General Parameter Rule (Min / Max) For every numeric threshold used in logic (volumes, pips, etc.): • The EA should provide two inputs: • XxxMin • XxxMax • The effective value used in logic must be inside [XxxMin, XxxMax]. • The user can set both Min and Max via inputs. • If needed, developer may: • Clamp the value into this range, or • Use validation and skip trade when inconsistent. ⸻ 6. Trend Leg (Pre-Reversal) Logic We define a trend leg as a sequence of candles (up to N bars) in the direction of the upcoming reversal. 6.1 Configurable Trend Bar Count • Input: • TrendBarsCountMin, TrendBarsCountMax • Allowed 1 to 10 • EA will use TrendBarsCount (integer) such that: • 1 ≤ TrendBarsCount ≤ 10 • TrendBarsCount ∈ [TrendBarsCountMin, TrendBarsCountMax] This number is how many most recent candles are included in the “trend leg” before looking for a reversal. Note: These candles do not have to be all bullish or all bearish. They are just the most recent bars forming the trend leg; we evaluate them by volume thresholds, not purely by candle color. 6.2 Per-Bar Volume Thresholds For each trend bar index i (1 to TrendBarsCount), we define: • For long setup (downward trend leg): • We use Bid volume thresholds. • Inputs: • TrendBidMin[i], TrendBidMax[i] for each i (or a generic pattern if developer prefers; concept: per-bar adjustable min/max Bid sum) • Condition (per bar): • SumBidVolumeOfBar(i) must be within [TrendBidMin[i], TrendBidMax[i]]. • For short setup (upward trend leg): • We use Ask volume thresholds. • Inputs: • TrendAskMin[i], TrendAskMax[i] for each i • Condition (per bar): • SumAskVolumeOfBar(i) must be within [TrendAskMin[i], TrendAskMax[i]]. Candle indexing direction is up to the developer, but must be consistent and documented internally (e.g., i=1 is the oldest of the sequence or the nearest). 6.3 Bottom/Top 2 Slots of the Last Trend Bar We also inspect the last trend bar (the most recent one in the trend leg) and its footprint volume distribution: • For LONG setup (downward trend before reversal): • We focus on the bottom 2 price levels of that candle: • LastTrendBottomBid1, LastTrendBottomBid2 • LastTrendBottomAsk1, LastTrendBottomAsk2 • Inputs: • LastTrendBottomBid1Min, LastTrendBottomBid1Max • LastTrendBottomBid2Min, LastTrendBottomBid2Max • LastTrendBottomAsk1Min, LastTrendBottomAsk1Max • LastTrendBottomAsk2Min, LastTrendBottomAsk2Max • Conditions: • Each of these four values must fall within its respective [Min, Max]. • For SHORT setup (upward trend before reversal): • We focus on the top 2 price levels: • LastTrendTopBid1, LastTrendTopBid2 • LastTrendTopAsk1, LastTrendTopAsk2 • Inputs similarly: • LastTrendTopBid1Min, LastTrendTopBid1Max, etc. No requirement that other slots (price levels) of the trend bar are zero. Only these 2×2 levels must be within their configured min/max ranges. ⸻ 7. Wick Filters (Trend Bar & Reversal Bar) We have two separate wick filters: 7.1 Last Trend Bar Wick Filter • Input: UseTrendBarWickFilter (bool) If UseTrendBarWickFilter = true: • For LONG setup: • The last trend bar must have a lower wick (Low < min(Open, Close)). • For SHORT setup: • The last trend bar must have an upper wick (High > max(Open, Close)). If the required wick is not present, no entry (setup invalid). If UseTrendBarWickFilter = false: • Wick presence on the last trend bar is ignored. 7.2 Reversal Bar Wick Filter • Input: UseReversalBarWickFilter (bool) If UseReversalBarWickFilter = true: • For LONG entry: • If the reversal bar has an upper wick, this is NO-ENTRY. (We require no upper wick on the reversal bar.) • For SHORT entry: • If the reversal bar has a lower wick, this is NO-ENTRY. (We require no lower wick on the reversal bar.) If UseReversalBarWickFilter = false: • Wick presence on the reversal bar is ignored. ⸻ 8. Reversal Detection Logic (Footprint-Based) After we have a valid trend leg, we look for a reversal bar (or one of several bars) that satisfies strict footprint conditions. 8.1 Configurable Number of Reversal Bars • Inputs: • ReversalBarsCountMin, ReversalBarsCountMax • Allowed range: 1 to 5 • EA uses ReversalBarsCount such that: • 1 ≤ ReversalBarsCount ≤ 5 • ReversalBarsCount ∈ [ReversalBarsCountMin, ReversalBarsCountMax] We check up to the last ReversalBarsCount completed candles after the trend leg. If any of these bars meets the reversal conditions, we open a trade at close of that bar. 8.2 Long Reversal Conditions (Buy Setup) Let the candidate bar be the reversal bar. 1. Bid volume distribution (strict zero rule): • Using footprint at each price level inside this bar: • For Bid: • All price levels except the bottom 2 levels must have Bid volume = 0. • Only the bottom 2 price levels may have non-zero Bid. • Inputs for bottom 2 Bid levels: • RevBottomBid1Min, RevBottomBid1Max • RevBottomBid2Min, RevBottomBid2Max • Conditions: • RevBottomBid1 ∈ [RevBottomBid1Min, RevBottomBid1Max] • RevBottomBid2 ∈ [RevBottomBid2Min, RevBottomBid2Max] 2. Ask volume distribution (no zero restriction except bottom 2 are adjustable): • Bottom 2 Ask levels: • RevBottomAsk1, RevBottomAsk2 • Inputs: • RevBottomAsk1Min, RevBottomAsk1Max • RevBottomAsk2Min, RevBottomAsk2Max • Conditions: • RevBottomAsk1 ∈ [RevBottomAsk1Min, RevBottomAsk1Max] • RevBottomAsk2 ∈ [RevBottomAsk2Min, RevBottomAsk2Max] • Ask at all other slots (above bottom 2) has no restriction about zero; they can be any value. • Additionally, we define a total Ask volume threshold: • Inputs: • ReversalAskTotalMin, ReversalAskTotalMax • Condition: • SumAskVolumeOfReversalBar ∈ [ReversalAskTotalMin, ReversalAskTotalMax] 3. Long-side extra rule for Bid = 0 case • If the user wants “Bid completely 0” behavior, they can simply set: • RevBottomBid1Min = RevBottomBid1Max = 0 • RevBottomBid2Min = RevBottomBid2Max = 0 • Because we force all other Bid levels to 0, this produces Bid total = 0. 4. Wick rule for reversal bar • Handled by UseReversalBarWickFilter (explained above). If all above hold, this bar is a valid long reversal bar. 8.3 Short Reversal Conditions (Sell Setup) This is essentially the mirror of the long side, but with “top 2 slots” and Ask/Bid swapped: 1. Ask volume distribution (strict zero rule): • For Ask: • All price levels except the top 2 levels must have Ask volume = 0. • Only the top 2 price levels may have non-zero Ask. • Inputs: • RevTopAsk1Min, RevTopAsk1Max • RevTopAsk2Min, RevTopAsk2Max • Conditions: • RevTopAsk1 ∈ [RevTopAsk1Min, RevTopAsk1Max] • RevTopAsk2 ∈ [RevTopAsk2Min, RevTopAsk2Max] 2. Bid volume distribution (no zero restriction except top 2 adjustable): • Top 2 Bid levels: • RevTopBid1, RevTopBid2 • Inputs: • RevTopBid1Min, RevTopBid1Max • RevTopBid2Min, RevTopBid2Max • Conditions: • RevTopBid1 ∈ [RevTopBid1Min, RevTopBid1Max] • RevTopBid2 ∈ [RevTopBid2Min, RevTopBid2Max] • Bid at other slots (below top 2) is not required to be zero. • Total Bid volume threshold: • ReversalBidTotalMin, ReversalBidTotalMax • Condition: • SumBidVolumeOfReversalBar ∈ [ReversalBidTotalMin, ReversalBidTotalMax] 3. Ask total = 0 pattern • If the user wants “Ask completely 0” behavior, they can set: • RevTopAsk1Min = RevTopAsk1Max = 0 • RevTopAsk2Min = RevTopAsk2Max = 0 4. Wick rule for reversal bar • Controlled by UseReversalBarWickFilter for short side (as described before). If all above hold, this bar is a valid short reversal bar. ⸻ 9. Entry Rules 9.1 Long Entry • Pre-conditions: • Session filter passes. • Range filter conditions satisfied if UseRangeFilter = true. • Valid trend leg with TrendBarsCount bars. • Last trend bar bottom 2 slots matched. • Reversal bar (within last ReversalBarsCount bars) satisfies all long reversal conditions. • Wick filters satisfied. • Entry: • Open market Buy order at close price of the reversal bar. 9.2 Short Entry • Same structure, but for short side (mirrored conditions). • Entry: • Open market Sell order at close price of the reversal bar. ⸻ 10. Stop Loss & Take Profit 10.1 Long • Stop Loss: • Let TrendLow = lowest Low of the trend leg (or the last trend leg low; user’s intention is “recent swing low of the downward move”). • SLPrice = TrendLow - SLBufferPips * PointSize • SL Distance (pips): • SLDistancePips = (EntryPrice - SLPrice) / PipValueFactor • If SLDistancePips > MaxSLDistancePips → NO TRADE. • Take Profit: • TPDistancePips = SLDistancePips * RR • TPPrice = EntryPrice + TPDistancePips * PipValueFactor 10.2 Short • Stop Loss: • Let TrendHigh = highest High of the trend leg (recent swing high). • SLPrice = TrendHigh + SLBufferPips * PointSize • SL Distance: • SLDistancePips = (SLPrice - EntryPrice) / PipValueFactor • If SLDistancePips > MaxSLDistancePips → NO TRADE. • Take Profit: • TPDistancePips = SLDistancePips * RR • TPPrice = EntryPrice - TPDistancePips * PipValueFactor 10.3 Lot Size Calculation (both sides) • MaxLossMoney = AccountBalance * RiskPercent / 100 • LotSize = MaxLossMoney / (SLDistancePips * PipValuePerLot) • Use broker constraints: • min lot, max lot, step, etc. • If computed LotSize < broker minimum → either: • skip trade, or • use minimum lot (clarify in implementation; recommended: skip trade). ⸻ 11. Other Implementation Notes 1. Bar-Close Logic Only • EA must evaluate and enter trades only at bar close, not intrabar. 2. One Trade per Signal (locking) • To avoid duplicate entries on the same bar, use a flag or bar-time check. 3. No Hedging • Optionally, allow only one position per symbol & direction. • Exact behavior can be kept simple: no new trade if there is already an open position on this symbol. 4. Logging & Debugging • On each bar, log major decision points: • Trend leg valid / invalid and reason • Range filter pass/fail • Reversal condition pass/fail • Wick filters result • SLDistance vs MaxSLDistance • Final decision: trade opened / skipped and reason 5. Footprint Data Access • EA needs to access the Bid / Ask volume per price level in the completed candle of the #Footprint indicator. • If needed, we can agree on: • Indicator buffer indices • How “slots / levels” are defined (price step, depth, total number of levels) • The important logic is: • For long: check bottom 2 levels of the bar. • For short: check top 2 levels of the bar. • Enforce zero / non-zero and min/max ranges as described. Below is an additional specification I would like to request on top of the previously delivered EA specification. Please integrate this as an optional filter, keeping all existing logic untouched. ⸻ Optional MA Trend Filter (5MA) This is an optional filter used to confirm the slope of the market trend based on a moving average. It does not replace any existing conditions. When enabled, it simply verifies that the selected trend legs (1–10 bars) are aligned with a consistent MA slope before allowing an entry. New Parameters • UseMATrendFilter Boolean. Enable or disable MA slope filtering. Default: false. • MATrendPeriod Integer. MA period used for slope analysis. Default: 5. • MATrendTimeframe Timeframe for the MA calculation. Default: current chart timeframe. • MATrendBarsToCheck Integer. Number of trend bars to validate MA slope. Range: 1–10. Default: 5. • MATrendMinStepPoints Minimum movement of the MA (in points) per bar to consider the slope valid. If set to 0, only the direction matters. Default: 0. Logic Summary Let N be the number of bars used for this check: N = the smaller value of TrendBarsUsed and MATrendBarsToCheck. For each of these N bars, the MA value at the close of that bar is referenced. Long Setup Requirement For a long entry, the MA must be consistently sloping down across the selected N bars. Condition: MA[i] - MA[i-1] must be less than or equal to -MATrendMinStepPoints. If this slope condition fails, the long entry is rejected. Short Setup Requirement For a short entry, the MA must be consistently sloping up across the selected N bars. Condition: MA[i] - MA[i-1] must be greater than or equal to MATrendMinStepPoints. If this slope condition fails, the short entry is rejected. General Notes • This filter is optional. • Turning UseMATrendFilter OFF fully disables all MA slope checks. • This filter does not alter the footprint logic, trend leg logic, range logic, wick logic, or SL/TP logic. • It only serves as an additional confirmation of trend structure quality.