#ifndef MA_THEORY_MQH #define MA_THEORY_MQH // --------------------------------------------------------------------------- // MATheory.mqh — the EMA 7/14/21 x 50 model (2026-09-17) // // Trend = EMA7 vs EMA50 (the 7x50 cross flips the bias and is the TREND-CROSS setup); // EMA14 and EMA21 following through the 50 are the MOMENTUM confirmations // State = the 7/14/21 stack relative to that trend: // 7>14>21 with the trend -> CONTINUATION // 7 under 14 (or 21), trend intact -> PULLBACK // 7<14<21 against the trend -> REVERSAL // Setups = TREND-CROSS (7x50), MOMENTUM-14 / MOMENTUM-21 (14x50, 21x50 follow-through), // STACK-CROSS (7 back above 14 and 21 after a pullback) // HTF = the same 7x50 read on the next timeframe up; disagreement halves the size (or blocks) // Big MA = where price sits against EMA140/230/500/1400 (the higher MA bias) // // MA index map (Indicators.mqh): 0=EMA7 1=EMA14 2=EMA21 3=EMA50. MA_Current is the live bar, // MA_Previous the closed bar (fixed 2026-09-17 - they were swapped). // --------------------------------------------------------------------------- int g_mat_htf_h21 = INVALID_HANDLE; int g_mat_htf_h50 = INVALID_HANDLE; ENUM_TIMEFRAMES g_mat_htf = PERIOD_CURRENT; int g_mat_htf_trend = 0; datetime g_mat_cross_bar = 0; datetime g_mat_stack_bar = 0; datetime g_mat_mom14_bar = 0; datetime g_mat_mom21_bar = 0; ENUM_TIMEFRAMES MAT_HigherTF() { if(MAT_HTF_Use_H1 && Period() < PERIOD_H1) return PERIOD_H1; switch(Period()) { case PERIOD_M1: return PERIOD_M5; case PERIOD_M5: return PERIOD_M15; case PERIOD_M15: return PERIOD_M30; case PERIOD_M30: return PERIOD_H1; case PERIOD_H1: return PERIOD_H4; case PERIOD_H4: return PERIOD_D1; default: return PERIOD_D1; } } bool MAT_Init() { if(!Use_MA_Theory || !MAT_HTF_Confirm) return true; g_mat_htf = MAT_HigherTF(); g_mat_htf_h21 = iMA(_Symbol, g_mat_htf, 7, 0, MODE_EMA, PRICE_CLOSE); // 7 vs 50 on the higher TF too g_mat_htf_h50 = iMA(_Symbol, g_mat_htf, 50, 0, MODE_EMA, PRICE_CLOSE); if(g_mat_htf_h21 == INVALID_HANDLE || g_mat_htf_h50 == INVALID_HANDLE) { Print("MATheory: could not create higher-TF EMA handles (", EnumToString(g_mat_htf), ") - HTF confirmation off"); return false; } Print("MATheory: on | trend = EMA7 x EMA50, momentum = 14/21 follow-through | state = 7/14/21 stack | HTF confirm on ", EnumToString(g_mat_htf)); return true; } void MAT_Release() { if(g_mat_htf_h21 != INVALID_HANDLE) IndicatorRelease(g_mat_htf_h21); if(g_mat_htf_h50 != INVALID_HANDLE) IndicatorRelease(g_mat_htf_h50); g_mat_htf_h21 = INVALID_HANDLE; g_mat_htf_h50 = INVALID_HANDLE; } // +1 bull (7 above 50), -1 bear, 0 flat — live bar and closed bar int MAT_Trend() { return (MA_Current[0] > MA_Current[3]) ? 1 : (MA_Current[0] < MA_Current[3]) ? -1 : 0; } int MAT_PrevTrend() { return (MA_Previous[0] > MA_Previous[3]) ? 1 : (MA_Previous[0] < MA_Previous[3]) ? -1 : 0; } // How many of EMA14 / EMA21 have followed the 7 through the 50 (0..2) int MAT_Momentum() { int t = MAT_Trend(); if(t == 0) return 0; int n = 0; if((t > 0 && MA_Current[1] > MA_Current[3]) || (t < 0 && MA_Current[1] < MA_Current[3])) n++; if((t > 0 && MA_Current[2] > MA_Current[3]) || (t < 0 && MA_Current[2] < MA_Current[3])) n++; return n; } // Higher MA bias: how many of EMA140/230/500/1400 price is above. 4 or 3 = BULL, 0 or 1 = BEAR, 2 = mixed. int MAT_BigMAAbove() { double px = iClose(_Symbol, PERIOD_CURRENT, 0); int n = 0; for(int i = 4; i < 8; ++i) if(MA_Current[i] > 0.0 && px > MA_Current[i]) n++; return n; } int MAT_BigMABias() { int n = MAT_BigMAAbove(); return n >= 3 ? 1 : n <= 1 ? -1 : 0; } string MAT_BigMAText() { int n = MAT_BigMAAbove(); int b = MAT_BigMABias(); return (b > 0 ? "BULL " : b < 0 ? "BEAR " : "mixed ") + IntegerToString(n) + "/4 above"; } // +1 when 7>14>21, -1 when 7<14<21, 0 mixed int MAT_Stack() { if(MA_Current[0] > MA_Current[1] && MA_Current[1] > MA_Current[2]) return 1; if(MA_Current[0] < MA_Current[1] && MA_Current[1] < MA_Current[2]) return -1; return 0; } // State from the stack relative to the 21x50 trend int MAT_State() { int t = MAT_Trend(), s = MAT_Stack(); if(t == 0) return STATE_PULLBACK; if(s == t) return STATE_CONTINUATION; if(s == -t) return STATE_REVERSAL; return STATE_PULLBACK; // 7 has dipped under 14 or 21, 21x50 still intact } void MAT_Update() { if(!Use_MA_Theory || !MAT_HTF_Confirm || g_mat_htf_h21 == INVALID_HANDLE) return; double b21[1], b50[1]; if(CopyBuffer(g_mat_htf_h21, 0, 1, 1, b21) > 0 && CopyBuffer(g_mat_htf_h50, 0, 1, 1, b50) > 0) g_mat_htf_trend = (b21[0] > b50[0]) ? 1 : (b21[0] < b50[0]) ? -1 : 0; } // Size multiplier from higher-TF agreement (1.0 agree / undecided, MAT_HTF_Size_Mult otherwise) double MAT_HTF_SizeMult(const bool buy, string ¬e) { note = ""; if(!Use_MA_Theory || !MAT_HTF_Confirm || g_mat_htf_trend == 0) return 1.0; bool agree = (buy && g_mat_htf_trend > 0) || (!buy && g_mat_htf_trend < 0); if(agree) return 1.0; if(MAT_HTF_Block) { note = "HTF " + EnumToString(g_mat_htf) + " 7x50 is " + (g_mat_htf_trend > 0 ? "BULL" : "BEAR") + " - blocked"; return 0.0; } note = "HTF " + EnumToString(g_mat_htf) + " 7x50 is " + (g_mat_htf_trend > 0 ? "BULL" : "BEAR") + " - size x" + DoubleToString(MAT_HTF_Size_Mult, 2); return MAT_HTF_Size_Mult; } // Cross count 0..9 (2026-09-17): the nine MA pairs along the chain, each counted when aligned in // the same direction: 7>14, 7>21, 14>21, 7>50, 14>50, 21>50, 50>140, 140>230, 230>500 (or all <). // "7 just crossed 14" = 1; "7 and 14 through the 50" adds those pairs; a full bull chain = 9. int MAT_CrossCount(const int dir) { if(dir == 0) return 0; int pairs[9][2] = {{0,1},{0,2},{1,2},{0,3},{1,3},{2,3},{3,4},{4,5},{5,6}}; int n = 0; for(int i = 0; i < 9; ++i) { double a = MA_Current[pairs[i][0]], b = MA_Current[pairs[i][1]]; if(a <= 0.0 || b <= 0.0) continue; if((dir > 0 && a > b) || (dir < 0 && a < b)) n++; } return n; } // Dominant side and its count int MAT_MomentumDir() { int bu = MAT_CrossCount(1), be = MAT_CrossCount(-1); return bu > be ? 1 : be > bu ? -1 : 0; } int MAT_MomentumScore() { return MAT_CrossCount(MAT_MomentumDir() == 0 ? MAT_Trend() : MAT_MomentumDir()); } // Lot multiplier from the cross count (replaces the praise-count multiplier under the theory; capped by Risk_Mult_Cap) double MAT_SizeMult() { int sc = MAT_MomentumScore(); if(sc >= 7) return 1.5; if(sc >= 4) return 1.0; return 0.75; } string MAT_MomentumText() { int d = MAT_MomentumDir(); return IntegerToString(MAT_MomentumScore()) + "/9 " + (d > 0 ? "BULL" : d < 0 ? "BEAR" : "flat") + " x" + DoubleToString(MAT_SizeMult(), 2); } string MAT_TrendText() { int t = MAT_Trend(); return t > 0 ? "BULL" : t < 0 ? "BEAR" : "FLAT"; } string MAT_StackText() { int s = MAT_Stack(); return (s > 0 ? "7>14>21" : s < 0 ? "7<14<21" : "mixed") + " mom " + IntegerToString(MAT_Momentum()) + "/2"; } string MAT_HTFText() { if(!MAT_HTF_Confirm) return "off"; return (g_mat_htf_trend > 0 ? "BULL" : g_mat_htf_trend < 0 ? "BEAR" : "FLAT") + " " + StringSubstr(EnumToString(g_mat_htf), 7); } void FireSetup(const bool buy, const string name, const bool continuation); // The two MA setups. Each fires at most once per bar. void ExecuteMATheorySetups() { if(!Use_MA_Theory) return; datetime bar = iTime(_Symbol, PERIOD_CURRENT, 0); int t = MAT_Trend(), pt = MAT_PrevTrend(); // 7 crossing 50: the trend change itself if(t != 0 && pt != t && g_mat_cross_bar != bar) { g_mat_cross_bar = bar; Print("MATheory: EMA7 crossed ", t > 0 ? "above" : "below", " EMA50 -> TREND-CROSS-", t > 0 ? "UP" : "DN"); FireSetup(t > 0, t > 0 ? "TREND-CROSS-UP" : "TREND-CROSS-DN", true); } // 14 then 21 following through the 50 in the trend direction: momentum confirmations if(t != 0 && g_mat_mom14_bar != bar) { bool now = (t > 0) ? MA_Current[1] > MA_Current[3] : MA_Current[1] < MA_Current[3]; bool prev = (t > 0) ? MA_Previous[1] > MA_Previous[3] : MA_Previous[1] < MA_Previous[3]; if(now && !prev) { g_mat_mom14_bar = bar; Print("MATheory: EMA14 followed through the 50 -> MOMENTUM-14-", t > 0 ? "UP" : "DN"); FireSetup(t > 0, t > 0 ? "MOMENTUM-14-UP" : "MOMENTUM-14-DN", true); } } if(t != 0 && g_mat_mom21_bar != bar) { bool now = (t > 0) ? MA_Current[2] > MA_Current[3] : MA_Current[2] < MA_Current[3]; bool prev = (t > 0) ? MA_Previous[2] > MA_Previous[3] : MA_Previous[2] < MA_Previous[3]; if(now && !prev) { g_mat_mom21_bar = bar; Print("MATheory: EMA21 followed through the 50 -> MOMENTUM-21-", t > 0 ? "UP" : "DN"); FireSetup(t > 0, t > 0 ? "MOMENTUM-21-UP" : "MOMENTUM-21-DN", true); } } // 7 back above (below) both 14 and 21 in trend direction after dipping: the pullback is over if(g_mat_stack_bar != bar) { bool up_now = (MA_Current[0] > MA_Current[1] && MA_Current[0] > MA_Current[2]); bool up_prev = (MA_Previous[0] > MA_Previous[1] && MA_Previous[0] > MA_Previous[2]); bool dn_now = (MA_Current[0] < MA_Current[1] && MA_Current[0] < MA_Current[2]); bool dn_prev = (MA_Previous[0] < MA_Previous[1] && MA_Previous[0] < MA_Previous[2]); if(t > 0 && up_now && !up_prev) { g_mat_stack_bar = bar; FireSetup(true, "STACK-CROSS-UP", true); } if(t < 0 && dn_now && !dn_prev) { g_mat_stack_bar = bar; FireSetup(false, "STACK-CROSS-DN", true); } } } // --------------------------------------------------------------------------- // Floors and ceilings (2026-09-17): every EMA below price is a floor, every EMA above is a // ceiling. Periods 50/140/230/500/1400 (indices 3..7). Three setups per MA, once per bar: // MA-FLOOR-BOUNCE-

price came down to a floor (within MAT_Touch_ATR) and closed back above -> BUY // MA-CEIL-REJECT-

price rose to a ceiling and closed back below -> SELL // MA-BREAK-UP/DN-

bar closed through the MA (ceiling broken -> BUY, floor lost -> SELL) // The direction rules in FireSetup still apply (with the trend in CONTINUATION, fades or // confirmed continuation in a pullback), so a floor bounce against a bear 21x50 is a fade. // --------------------------------------------------------------------------- datetime g_mat_fc_bar[8]; // last bar a floor/ceiling setup fired per MA index datetime g_mat_brk_bar[8]; int MAT_NearestFloor(double &dist_atr) { double px = iClose(_Symbol, PERIOD_CURRENT, 0); int best = -1; double bd = 1e9; for(int i = 3; i < 8; ++i) { double d = px - MA_Current[i]; if(d > 0 && d < bd) { bd = d; best = i; } } dist_atr = (best >= 0 && Current_ATR > 0) ? bd / Current_ATR : 0.0; return best; } int MAT_NearestCeiling(double &dist_atr) { double px = iClose(_Symbol, PERIOD_CURRENT, 0); int best = -1; double bd = 1e9; for(int i = 3; i < 8; ++i) { double d = MA_Current[i] - px; if(d > 0 && d < bd) { bd = d; best = i; } } dist_atr = (best >= 0 && Current_ATR > 0) ? bd / Current_ATR : 0.0; return best; } string MAT_FloorText() { double d; int i = MAT_NearestFloor(d); return i < 0 ? "none" : "EMA" + IntegerToString(MA_Periods[i]) + " (" + DoubleToString(d, 1) + " ATR)"; } string MAT_CeilingText() { double d; int i = MAT_NearestCeiling(d); return i < 0 ? "none" : "EMA" + IntegerToString(MA_Periods[i]) + " (" + DoubleToString(d, 1) + " ATR)"; } void ExecuteMAFloorCeilingSetups() { if(!Use_MA_Theory || !MAT_Floor_Ceiling || Current_ATR <= 0.0) return; datetime bar = iTime(_Symbol, PERIOD_CURRENT, 0); double lo = iLow(_Symbol, PERIOD_CURRENT, 0), hi = iHigh(_Symbol, PERIOD_CURRENT, 0); double cl = iClose(_Symbol, PERIOD_CURRENT, 0), pc = iClose(_Symbol, PERIOD_CURRENT, 1); double buf = Current_ATR * MAT_Touch_ATR; for(int i = 3; i < 8; ++i) { double ma = MA_Current[i]; if(ma <= 0.0) continue; string per = IntegerToString(MA_Periods[i]); if(g_mat_fc_bar[i] != bar) { // floor bounce: opened/closed above, dipped into the MA, closing back above it if(pc > ma && lo <= ma + buf && cl > ma) { g_mat_fc_bar[i] = bar; FireSetup(true, "MA-FLOOR-BOUNCE-" + per, true); } // ceiling reject: below, poked the MA, closing back under it else if(pc < ma && hi >= ma - buf && cl < ma) { g_mat_fc_bar[i] = bar; FireSetup(false, "MA-CEIL-REJECT-" + per, true); } } if(g_mat_brk_bar[i] != bar) { // break: the bar closes through the MA (needs a real close, not a wick) if(pc < ma && cl > ma + buf) { g_mat_brk_bar[i] = bar; FireSetup(true, "MA-BREAK-UP-" + per, true); } else if(pc > ma && cl < ma - buf) { g_mat_brk_bar[i] = bar; FireSetup(false, "MA-BREAK-DN-" + per, true); } } } } #endif