#ifndef TRADE_POLICY_MQH #define TRADE_POLICY_MQH // --------------------------------------------------------------------------- // TradePolicy.mqh — setup allow/block policy (2026-09-16) // // Every order passes through OpenBuy/OpenSell with a setup name ("FORCE-URGENT-BUY", // "MAGNET-WALK-UP", "RE-ENTRY 1 | POST-CLOSE VALIDATION", ...). This gate decides, // by name prefix, whether that setup is allowed to trade at all: // - Setup_Blocklist: comma-separated prefixes that never trade. // - Setup_Allowlist: if non-empty, only these prefixes trade (checked after the blocklist). // Matching is case-insensitive on the start of the name, so "FORCE-URGENT" covers both // FORCE-URGENT-BUY and FORCE-URGENT-SELL. Blocked setups are logged once per name change // so the journal shows what the policy is doing without spamming every tick. // --------------------------------------------------------------------------- bool TP_MatchesAny(const string name, const string csv) { if(StringLen(csv) == 0) return false; string parts[]; int n = StringSplit(csv, ',', parts); string upper = name; StringToUpper(upper); for(int i = 0; i < n; ++i) { string p = parts[i]; StringTrimLeft(p); StringTrimRight(p); if(StringLen(p) == 0) continue; StringToUpper(p); if(StringFind(upper, p) == 0) return true; } return false; } // Condition filters (2026-09-16) — from splitting the EA's own log per setup: // FORCE-URGENT-BUY : ATRn>=12 -> 31 trades -358; price >500pts above MFIB.382 -> 27 trades -355. // Below both: 27 trades -18 / 31 trades -21. So: calm + not extended only. // FORCE-URGENT-SELL: Stoch K<35 (selling into oversold) -> 23 trades 13% win -31; // K>=50 -> 12 trades 67-83% win +25. ATRn>=9 better (+12 vs -16). So: K>=50 & ATRn>=9. // RE-ENTRY : ADX 20-33 -> +304; ADX<20 -> -4. So: ADX>=20. // Uses live EA globals: Current_ATR, Current_ADX, Stoch_K_Current, MFIB_Level_382 (Indicators/FibLevels). bool SetupPassesFilters(const string name, const bool is_buy, string &why) { string up = name; StringToUpper(up); double close0 = iClose(_Symbol, PERIOD_CURRENT, 0); double atrn = (Current_ATR > 0.0 && close0 > 0.0) ? (Current_ATR / close0) * 10000.0 : 0.0; if(StringFind(up, "FORCE-URGENT") == 0) { if(is_buy) { if(Force_Buy_Max_ATRn > 0.0 && atrn >= Force_Buy_Max_ATRn) { why = "FORCE-BUY: ATRn " + DoubleToString(atrn, 1) + " >= " + DoubleToString(Force_Buy_Max_ATRn, 1) + " (volatile)"; return false; } double ext_pts = (close0 - MFIB_Level_382) / _Point; if(Force_Buy_Max_Ext_Pts > 0 && MFIB_Level_382 > 0.0 && ext_pts > Force_Buy_Max_Ext_Pts) { why = "FORCE-BUY: " + DoubleToString(ext_pts, 0) + " pts above MFIB.382 (chasing)"; return false; } } else { if(Force_Sell_Min_StochK > 0.0 && Stoch_K_Current < Force_Sell_Min_StochK) { why = "FORCE-SELL: Stoch K " + DoubleToString(Stoch_K_Current, 1) + " < " + DoubleToString(Force_Sell_Min_StochK, 0) + " (selling into oversold)"; return false; } if(Force_Sell_Min_ATRn > 0.0 && atrn < Force_Sell_Min_ATRn) { why = "FORCE-SELL: ATRn " + DoubleToString(atrn, 1) + " < " + DoubleToString(Force_Sell_Min_ATRn, 1) + " (too quiet)"; return false; } } } if(StringFind(up, "RE-ENTRY") == 0 && ReEntry_Min_ADX > 0.0 && Current_ADX < ReEntry_Min_ADX) { why = "RE-ENTRY: ADX " + DoubleToString(Current_ADX, 1) + " < " + DoubleToString(ReEntry_Min_ADX, 0); return false; } return true; } // v7.20 probation: lot multiplier for a setup that is rebuilding its record (1.0 = not on probation). // "RE-ENTRY n | " is judged by its base setup. double SetupProbationMult(const string name) { if(StringLen(Setup_Probation) == 0) return 1.0; string base = name; int bar = StringFind(name, " | "); if(StringFind(name, "RE-ENTRY") == 0 && bar >= 0) base = StringSubstr(name, bar + 3); if(!TP_MatchesAny(base, Setup_Probation)) return 1.0; double m = MathMax(0.05, MathMin(1.0, Probation_Size_Mult)); static string last = ""; if(last != name) { Print("TradePolicy: ", name, " on probation - size x", DoubleToString(m, 2)); last = name; } return m; } // v7.29 setup conditions (see InputParams). Returns the lot multiplier: 0 = no entry, 1 = full size. double SetupConditionMult(const string name, const bool is_buy) { if(!Use_Setup_Conditions) return 1.0; static int sc_e20 = INVALID_HANDLE, sc_e50 = INVALID_HANDLE, sc_atr = INVALID_HANDLE; if(sc_e20 == INVALID_HANDLE) sc_e20 = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE); if(sc_e50 == INVALID_HANDLE) sc_e50 = iMA(_Symbol, PERIOD_H1, 50, 0, MODE_EMA, PRICE_CLOSE); if(sc_atr == INVALID_HANDLE) sc_atr = iATR(_Symbol, PERIOD_M15, 14); string base = name; int bar = StringFind(name, " | "); if(StringFind(name, "RE-ENTRY") == 0 && bar >= 0) base = StringSubstr(name, bar + 3); double e20[], e50[], atr[]; ArraySetAsSeries(e20, true); ArraySetAsSeries(e50, true); ArraySetAsSeries(atr, true); if(CopyBuffer(sc_e20, 0, 1, 4, e20) != 4 || CopyBuffer(sc_e50, 0, 1, 1, e50) != 1) return 1.0; // data not ready: no change int dir = is_buy ? 1 : -1; string why = ""; double m = 1.0; // A: H1 EMA20 slope against the trade if(Cond_Block_Slope_Against && (e20[0] - e20[3]) * dir < 0.0) { static datetime last_a = 0; if(TimeCurrent() - last_a >= 60) { Print("SetupConditions: ", name, (is_buy ? " BUY" : " SELL"), " skipped - H1 EMA20 slopes against the trade"); last_a = TimeCurrent(); } return 0.0; } // B: trend-following setups against the H1 trend if(StringLen(Cond_Trend_Setups) > 0 && TP_MatchesAny(base, Cond_Trend_Setups) && (e20[0] - e50[0]) * dir < 0.0) { m = MathMin(m, Cond_Size_Mult); why = "against the H1 EMA20/50 trend"; } // C: quiet market if(Cond_Quiet_Ratio > 0.0 && !(StringLen(Cond_Quiet_Exempt) > 0 && TP_MatchesAny(base, Cond_Quiet_Exempt))) { int n = CopyBuffer(sc_atr, 0, 1, 960, atr); if(n >= 200) { double s[]; ArrayResize(s, n); ArrayCopy(s, atr, 0, 0, n); ArraySort(s); double med = s[n / 2]; if(med > 0.0 && atr[0] / med < Cond_Quiet_Ratio) { m = MathMin(m, Cond_Size_Mult); why += (StringLen(why) ? " + " : "") + "quiet market (M15 ATR " + DoubleToString(atr[0] / med, 2) + "x its 10-day median)"; } } } if(m < 1.0) { static string last_why = ""; static datetime last_t = 0; if(why != last_why || TimeCurrent() - last_t >= 300) { Print("SetupConditions: ", name, (is_buy ? " BUY" : " SELL"), " size x", DoubleToString(m, 2), " - ", why); last_why = why; last_t = TimeCurrent(); } } return m; } bool SetupAllowedByPolicy(const string name, const bool is_buy = true) { static string last_blocked = ""; bool allowed = true; string why = ""; if(TP_MatchesAny(name, Setup_Blocklist)) { allowed = false; why = "blocklist"; } else if(StringLen(Setup_Allowlist) > 0 && !TP_MatchesAny(name, Setup_Allowlist)) { allowed = false; why = "not in allowlist"; } else if(!SetupPassesFilters(name, is_buy, why)) { allowed = false; } if(!allowed && last_blocked != name) { static string tp_note_name[16]; static datetime tp_note_time[16]; static int tp_note_n = 0; int slot = -1; for(int i = 0; i < tp_note_n; ++i) if(tp_note_name[i] == name) { slot = i; break; } if(slot < 0 && tp_note_n < 16) { slot = tp_note_n++; tp_note_name[slot] = name; tp_note_time[slot] = 0; } if(slot < 0 || TimeCurrent() - tp_note_time[slot] >= 60) { Print("TradePolicy: blocked setup '", name, "' (", why, ")"); if(slot >= 0) tp_note_time[slot] = TimeCurrent(); } last_blocked = name; } return allowed; } #endif