#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; } 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