EA: sync live build (Jul-Nov 2026) — trade report direction/setup fix, VZ indicators, risk modules
- AIGateway/profitgtx: report_trade takes direction and setup from the position's
opening deal ("dir_src":"entry"); the closing deal inverted every direction and
restarts lost the setup (99% "auto")
- add MATheory, RiskGuards, RiskSizing, TradePolicy (setup blocklist, strict AI gate)
- add VZ_Core + VZ_Features/MFIB/Regime/Setups/Sessions indicators, VZ_FeaturesExport
service, VZ_DumpMA, VZ_TogglePause
- margin-aware sizing, news, labels and session updates since 2026-07-14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 13:48:22 -06:00
# 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 ;
}
2026-11-09 02:30:20 -06:00
// v7.20 probation: lot multiplier for a setup that is rebuilding its record (1.0 = not on probation).
// "RE-ENTRY n | <setup>" 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 ;
}
2026-11-09 13:45:19 -06:00
// 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 ;
}
2026-11-09 14:03:35 -06:00
// B: trend-following setups against the H1 trend (v7.30: + Cond_Trend_Setups_Extra)
bool h1_with = ( e20 [ 0 ] - e50 [ 0 ] ) * dir > 0.0 ;
if ( ! h1_with & & ( ( StringLen ( Cond_Trend_Setups ) > 0 & & TP_MatchesAny ( base , Cond_Trend_Setups ) ) | |
( StringLen ( Cond_Trend_Setups_Extra ) > 0 & & TP_MatchesAny ( base , Cond_Trend_Setups_Extra ) ) ) )
2026-11-09 13:45:19 -06:00
{ m = MathMin ( m , Cond_Size_Mult ) ; why = " against the H1 EMA20/50 trend " ; }
2026-11-09 14:03:35 -06:00
MqlDateTime now_dt ; TimeToStruct ( TimeCurrent ( ) , now_dt ) ;
// v7.30 skip hours per setup ("SETUP:from-to;...")
if ( StringLen ( Cond_Setup_Skip_Hours ) > 0 )
{
string items [ ] ; int n_items = StringSplit ( Cond_Setup_Skip_Hours , ' ; ' , items ) ;
for ( int i = 0 ; i < n_items ; + + i )
{
int c = StringFind ( items [ i ] , " : " ) ; if ( c < = 0 ) continue ;
string su = StringSubstr ( items [ i ] , 0 , c ) ; StringTrimLeft ( su ) ; StringTrimRight ( su ) ;
string rg = StringSubstr ( items [ i ] , c + 1 ) ; int d = StringFind ( rg , " - " ) ;
int h_from = ( int ) StringToInteger ( d > 0 ? StringSubstr ( rg , 0 , d ) : rg ) , h_to = ( int ) StringToInteger ( d > 0 ? StringSubstr ( rg , d + 1 ) : rg ) ;
if ( StringFind ( base , su ) = = 0 & & now_dt . hour > = h_from & & now_dt . hour < = h_to )
{
static datetime last_h = 0 ;
if ( TimeCurrent ( ) - last_h > = 300 ) { Print ( " SetupConditions: " , name , " skipped - " , su , " does not trade " , h_from , " - " , h_to , " h (server time) " ) ; last_h = TimeCurrent ( ) ; }
return 0.0 ;
}
}
}
// v7.30 setup needs H1 AND H4 trend agreement
if ( StringLen ( Cond_Need_H1H4_Trend ) > 0 & & TP_MatchesAny ( base , Cond_Need_H1H4_Trend ) )
{
static int sc_h4_20 = INVALID_HANDLE , sc_h4_50 = INVALID_HANDLE ;
if ( sc_h4_20 = = INVALID_HANDLE ) sc_h4_20 = iMA ( _Symbol , PERIOD_H4 , 20 , 0 , MODE_EMA , PRICE_CLOSE ) ;
if ( sc_h4_50 = = INVALID_HANDLE ) sc_h4_50 = iMA ( _Symbol , PERIOD_H4 , 50 , 0 , MODE_EMA , PRICE_CLOSE ) ;
double a20 [ 1 ] , a50 [ 1 ] ;
bool h4_with = ( CopyBuffer ( sc_h4_20 , 0 , 1 , 1 , a20 ) = = 1 & & CopyBuffer ( sc_h4_50 , 0 , 1 , 1 , a50 ) = = 1 ) ? ( a20 [ 0 ] - a50 [ 0 ] ) * dir > 0.0 : true ;
if ( ! ( h1_with & & h4_with ) )
{
static datetime last_t4 = 0 ;
if ( TimeCurrent ( ) - last_t4 > = 300 ) { Print ( " SetupConditions: " , name , ( is_buy ? " BUY " : " SELL " ) , " skipped - needs H1 and H4 trend with the trade (H1 " , ( h1_with ? " with " : " against " ) , " , H4 " , ( h4_with ? " with " : " against " ) , " ) " ) ; last_t4 = TimeCurrent ( ) ; }
return 0.0 ;
}
}
// v7.30 small size for a setup on given symbols ("SETUP@SYMBOL,...")
if ( StringLen ( Cond_Setup_Symbol_Small ) > 0 )
{
string items [ ] ; int n_items = StringSplit ( Cond_Setup_Symbol_Small , ' , ' , items ) ;
for ( int i = 0 ; i < n_items ; + + i )
{
int at = StringFind ( items [ i ] , " @ " ) ; if ( at < = 0 ) continue ;
string su = StringSubstr ( items [ i ] , 0 , at ) ; StringTrimLeft ( su ) ; StringTrimRight ( su ) ;
string sy = StringSubstr ( items [ i ] , at + 1 ) ; StringTrimLeft ( sy ) ; StringTrimRight ( sy ) ;
if ( StringFind ( base , su ) = = 0 & & StringCompare ( sy , _Symbol , false ) = = 0 )
{ m = MathMin ( m , Cond_Size_Mult ) ; why + = ( StringLen ( why ) ? " + " : " " ) + su + " is small on " + _Symbol ; }
}
}
// v7.30 optional hour probation (all setups)
if ( StringLen ( Hour_Probation_Hours ) > 0 )
{
string hs [ ] ; int n_h = StringSplit ( Hour_Probation_Hours , ' , ' , hs ) ;
for ( int i = 0 ; i < n_h ; + + i )
if ( ( int ) StringToInteger ( hs [ i ] ) = = now_dt . hour )
{ m = MathMin ( m , Hour_Probation_Mult ) ; why + = ( StringLen ( why ) ? " + " : " " ) + " hour " + IntegerToString ( now_dt . hour ) + " on probation " ; break ; }
}
2026-11-09 13:45:19 -06:00
// 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 ;
}
EA: sync live build (Jul-Nov 2026) — trade report direction/setup fix, VZ indicators, risk modules
- AIGateway/profitgtx: report_trade takes direction and setup from the position's
opening deal ("dir_src":"entry"); the closing deal inverted every direction and
restarts lost the setup (99% "auto")
- add MATheory, RiskGuards, RiskSizing, TradePolicy (setup blocklist, strict AI gate)
- add VZ_Core + VZ_Features/MFIB/Regime/Setups/Sessions indicators, VZ_FeaturesExport
service, VZ_DumpMA, VZ_TogglePause
- margin-aware sizing, news, labels and session updates since 2026-07-14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 13:48:22 -06:00
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