| #ifndef TRADING_SETUPS_MQH
|
| #define TRADING_SETUPS_MQH
|
|
|
| void ExecuteReEntries();
|
|
|
| bool IsDirectionAllowedByCoordinator(const bool buy)
|
| {
|
| if(!Use_Signal_Coordinator)
|
| return true;
|
|
|
| if(Coordinator_Gates_Setups && !Coordinator_AllowTrade)
|
| return false;
|
|
|
| if(buy && Coordinator_Bias_Direction < 0)
|
| return false;
|
|
|
| if(!buy && Coordinator_Bias_Direction > 0)
|
| return false;
|
|
|
| return true;
|
| }
|
|
|
| // Setup class by name: 0 = momentum (needs CONTINUATION), 1 = pullback (needs PULLBACK or CONTINUATION),
|
| // 2 = mean reversion (RANGE-*/CHOP-*/FORCE-STANDARD, mode-gated by the caller).
|
| int SetupClass(const string name)
|
| {
|
| if(StringFind(name, "TREND-CROSS") == 0) return 3; // 7x50 cross: allowed in any state, with the new trend
|
| if(StringFind(name, "MOMENTUM-") == 0) return 3; // 14 / 21 following through the 50: same
|
| if(StringFind(name, "STACK-CROSS") == 0) return 1; // pullback over: continuation signal
|
| if(StringFind(name, "MA-FLOOR-BOUNCE") == 0 || StringFind(name, "MA-CEIL-REJECT") == 0) return 1; // level reaction
|
| if(StringFind(name, "MA-BREAK-") == 0) return 0; // momentum through a level
|
| if(StringFind(name, "RANGE-") == 0 || StringFind(name, "CHOP-") == 0 || StringFind(name, "FORCE-STANDARD") == 0)
|
| return 2;
|
| if(StringFind(name, "FORCE-URGENT") == 0 || // "keep exposure" setup, bias-directed: fine in a pullback
|
| StringFind(name, "EMA-RETEST") == 0 || StringFind(name, "EMA-REJECT") == 0 ||
|
| StringFind(name, "MA50-BOUNCE") == 0 || StringFind(name, "MA140-BOUNCE") == 0 ||
|
| StringFind(name, "MA230-BOUNCE") == 0 || StringFind(name, "MA500-TOUCH") == 0 ||
|
| StringFind(name, "FIB-RECLAIM") == 0)
|
| return 1;
|
| return 0;
|
| }
|
|
|
| // Journal a mode/state refusal once per (setup, reason) change so the chart can be read.
|
| void StateNote(const string name, const string reason)
|
| {
|
| static string names[32];
|
| static string reasons[32];
|
| static int n = 0;
|
| for(int i = 0; i < n; ++i)
|
| {
|
| if(names[i] != name) continue;
|
| if(reasons[i] == reason) return; // same refusal as last time for this setup: stay quiet
|
| reasons[i] = reason;
|
| Print("ModeState: ", name, " skipped - ", reason);
|
| return;
|
| }
|
| if(n < 32) { names[n] = name; reasons[n] = reason; n++; }
|
| Print("ModeState: ", name, " skipped - ", reason);
|
| }
|
|
|
| // True while the chart is in a trend but not in CONTINUATION (pullback or reversal leg).
|
| bool InTrendPullback()
|
| {
|
| return (Current_Mode == MODE_TRENDING && Current_State != STATE_CONTINUATION);
|
| }
|
|
|
| // Direction bias-directed setups should offer right now: +1 buy, -1 sell, 0 both.
|
| int EntryDirForState()
|
| {
|
| int dir = BiasDirection();
|
| if(!Mode_State_Gating || !InTrendPullback()) return dir;
|
| if(Pullback_Trade_Direction == PB_COUNTER_TREND) return -dir;
|
| if(Pullback_Trade_Direction == PB_BOTH) return 0;
|
| return dir;
|
| }
|
|
|
| void FireSetup(const bool buy, const string name, const bool continuation)
|
| {
|
| if(!IsDirectionAllowedByCoordinator(buy))
|
| return;
|
|
|
| if(Mode_State_Gating)
|
| {
|
| int cls = SetupClass(name);
|
| if(cls == 3 && ((buy && Current_Bias_Direction < 0) || (!buy && Current_Bias_Direction > 0)))
|
| { StateNote(name, "against the new 21x50 trend"); return; }
|
| if(cls == 0 && Current_State != STATE_CONTINUATION)
|
| { StateNote(name, "momentum setup needs State CONTINUATION (now " + (Current_State == STATE_PULLBACK ? "PULLBACK" : "REVERSAL") + ")"); return; }
|
| if(cls == 0 && Current_Mode != MODE_TRENDING)
|
| { StateNote(name, "momentum setup needs Mode TRENDING (now " + (Current_Mode == MODE_RANGING ? "RANGING" : "CHOP") + ")"); return; }
|
| if(cls == 1 && Current_Mode == MODE_CHOP)
|
| { StateNote(name, "pullback setup blocked in Mode CHOP"); return; }
|
| if(cls <= 1 && Current_Bias_Direction != 0)
|
| {
|
| bool with_trend = (buy == (Current_Bias_Direction > 0));
|
| if(InTrendPullback())
|
| {
|
| // pullback / reversal leg: Pullback_Trade_Direction decides which side is allowed
|
| if(Pullback_Trade_Direction == PB_COUNTER_TREND && with_trend)
|
| { StateNote(name, "with-trend entry in a " + (Current_State == STATE_REVERSAL ? "REVERSAL" : "PULLBACK") + " (counter-trend mode)"); return; }
|
| if(Pullback_Trade_Direction == PB_WITH_TREND && !with_trend)
|
| { StateNote(name, "against Bias in a pullback (with-trend mode)"); return; }
|
| // buying the dip is fine, but only on a continuation signal, not early
|
| if(with_trend && Pullback_WithTrend_Needs_Confirm)
|
| {
|
| bool confirm_setup = (cls == 1 && StringFind(name, "FORCE-URGENT") != 0);
|
| if(StringFind(name, "STACK-CROSS") == 0) confirm_setup = true; // the cross IS the confirmation
|
| bool stoch_ok = buy ? (StochCrossUp() || Stoch_K_Current > Stoch_Mid)
|
| : (StochCrossDown() || Stoch_K_Current < Stoch_Mid);
|
| if(!confirm_setup)
|
| { StateNote(name, "with-trend in a pullback needs an MA reject/retest/bounce/reclaim signal"); return; }
|
| if(!stoch_ok)
|
| { StateNote(name, "with-trend in a pullback waits for Stoch to turn (" + DoubleToString(Stoch_K_Current, 0) + ")"); return; }
|
| }
|
| }
|
| else if(!with_trend)
|
| { StateNote(name, "against Bias in CONTINUATION"); return; }
|
| }
|
| }
|
| else if(continuation)
|
| {
|
| if(Current_State != STATE_CONTINUATION && State_Bias_Confidence < 60.0)
|
| return;
|
| }
|
|
|
| double setup_mult = continuation ? 1.0 : 0.8;
|
| string htf_note = "";
|
| double htf_mult = MAT_HTF_SizeMult(buy, htf_note);
|
| if(StringLen(htf_note) > 0) StateNote(name, htf_note);
|
| if(htf_mult <= 0.0) return; // MAT_HTF_Block
|
| setup_mult *= htf_mult;
|
|
|
| // Armed re-entry? Same setup, same direction, conditions valid right now -> trade it as a
|
| // re-entry (cooldown bypass), otherwise as a normal setup.
|
| int ri = -1;
|
| if(ReEntryArmedFor(name, buy, ri))
|
| {
|
| string label = "RE-ENTRY " + IntegerToString(g_reentry[ri].attempts + 1) + " | " + name;
|
| g_reentry_attempt = true;
|
| bool ok = buy ? OpenBuy(label, setup_mult) : OpenSell(label, setup_mult);
|
| g_reentry_attempt = false;
|
| if(ok) Print("ReEntry: ", label, " opened - setup valid again");
|
| ReEntryConsume(ri, ok);
|
| return;
|
| }
|
|
|
| if(buy) OpenBuy(name, setup_mult);
|
| else OpenSell(name, setup_mult);
|
| }
|
|
|
| void ExecuteMARetestAndRejectionSetups()
|
| {
|
| if(!Use_MA_Retest_Entries && !Use_MA_Rejection_Entries)
|
| return;
|
|
|
| double current = MidPrice();
|
|
|
| // EMA14/21/50/140/230 retests
|
| int watched[5] = {1,2,3,4,5};
|
| for(int i = 0; i < 5; ++i)
|
| {
|
| int idx = watched[i];
|
| double ma = MA_Current[idx];
|
|
|
| bool touched = NearLevel(current, ma, MA_Touch_Buffer);
|
| bool bull_retest = (MA_Previous[0] > MA_Previous[idx] && iLow(_Symbol, PERIOD_CURRENT, 0) <= ma && iClose(_Symbol, PERIOD_CURRENT, 0) > ma);
|
| bool bear_retest = (MA_Previous[0] < MA_Previous[idx] && iHigh(_Symbol, PERIOD_CURRENT, 0) >= ma && iClose(_Symbol, PERIOD_CURRENT, 0) < ma);
|
|
|
| if(Use_MA_Retest_Entries && touched && TimeCurrent() - Last_MA_Retest_Trade >= MA_Retest_Cooldown_Seconds)
|
| {
|
| if(bull_retest) { FireSetup(true, "EMA-RETEST-UP", true); Last_MA_Retest_Trade = TimeCurrent(); }
|
| if(bear_retest) { FireSetup(false, "EMA-RETEST-DN", true); Last_MA_Retest_Trade = TimeCurrent(); }
|
| }
|
|
|
| if(Use_MA_Rejection_Entries && touched && TimeCurrent() - Last_MA_Reject_Trade >= MA_Retest_Cooldown_Seconds)
|
| {
|
| double o = iOpen(_Symbol, PERIOD_CURRENT, 0);
|
| double c = iClose(_Symbol, PERIOD_CURRENT, 0);
|
| double h = iHigh(_Symbol, PERIOD_CURRENT, 0);
|
| double l = iLow(_Symbol, PERIOD_CURRENT, 0);
|
| double body = MathAbs(c - o);
|
| double up_wick = h - MathMax(o, c);
|
| double lo_wick = MathMin(o, c) - l;
|
|
|
| bool bull_reject = (lo_wick > body * 2.0 && c > ma);
|
| bool bear_reject = (up_wick > body * 2.0 && c < ma);
|
|
|
| if(bull_reject) { FireSetup(true, "EMA-REJECT-UP", true); Last_MA_Reject_Trade = TimeCurrent(); }
|
| if(bear_reject) { FireSetup(false, "EMA-REJECT-DN", true); Last_MA_Reject_Trade = TimeCurrent(); }
|
| }
|
| }
|
| }
|
|
|
| void ExecuteForceEntrySetups()
|
| {
|
| // FORCE-* entries have no chart setup behind them (bias only). When both are blocklisted there is
|
| // nothing to evaluate — skip so they don't burn gateway calls or journal lines (2026-09-17).
|
| if(TP_MatchesAny("FORCE-URGENT-BUY", Setup_Blocklist) && TP_MatchesAny("FORCE-STANDARD-BUY", Setup_Blocklist))
|
| return;
|
| int open_count = CountOpenTradesForSymbol();
|
| if(open_count >= Min_Active_Entries)
|
| return;
|
|
|
| int dir = EntryDirForState(); // flipped in a trend pullback/reversal when fading (Pullback_Trade_Direction)
|
|
|
| if(open_count < 2)
|
| {
|
| if(dir >= 0) FireSetup(true, "FORCE-URGENT-BUY", true);
|
| if(dir <= 0) FireSetup(false, "FORCE-URGENT-SELL", true);
|
| return;
|
| }
|
|
|
| if(dir >= 0 && (Active_Praise_Signals >= Active_Warnings || Current_Mode != MODE_CHOP))
|
| FireSetup(true, "FORCE-STANDARD-BUY", false);
|
|
|
| if(dir <= 0 && (Active_Warnings >= Active_Praise_Signals || Current_Mode != MODE_CHOP))
|
| FireSetup(false, "FORCE-STANDARD-SELL", false);
|
| }
|
|
|
| void ExecuteContinuationSetups()
|
| {
|
| if(Current_Mode != MODE_TRENDING || Current_State != STATE_CONTINUATION)
|
| return;
|
|
|
| double current = MidPrice();
|
| double buffer = MA_Touch_Buffer;
|
| int dir = BiasDirection();
|
|
|
| if(!Use_MA_Theory) // under the MA theory this is MOMENTUM-14 (MATheory.mqh)
|
| {
|
| if(MA_Previous[1] <= MA_Previous[3] && MA_Current[1] > MA_Current[3]) FireSetup(true, "MA14-CROSS-UP", true);
|
| if(MA_Previous[1] >= MA_Previous[3] && MA_Current[1] < MA_Current[3]) FireSetup(false, "MA14-CROSS-DN", true);
|
| }
|
|
|
| if(NearLevel(current, MA_Current[3], buffer))
|
| {
|
| if(dir >= 0) FireSetup(true, "MA50-BOUNCE-UP", true);
|
| if(dir <= 0) FireSetup(false, "MA50-BOUNCE-DN", true);
|
| }
|
|
|
| if(NearLevel(current, MA_Current[4], buffer))
|
| {
|
| if(dir >= 0) FireSetup(true, "MA140-BOUNCE-UP", true);
|
| if(dir <= 0) FireSetup(false, "MA140-BOUNCE-DN", true);
|
| }
|
|
|
| if(NearLevel(current, MA_Current[5], buffer))
|
| {
|
| if(dir >= 0) FireSetup(true, "MA230-BOUNCE-UP", true);
|
| if(dir <= 0) FireSetup(false, "MA230-BOUNCE-DN", true);
|
| }
|
|
|
| if(NearLevel(current, MA_Current[6], buffer))
|
| {
|
| if(dir >= 0) FireSetup(true, "MA500-TOUCH-UP", true);
|
| if(dir <= 0) FireSetup(false, "MA500-TOUCH-DN", true);
|
| }
|
|
|
| for(int i = 1; i <= 5; ++i)
|
| {
|
| double c0 = iClose(_Symbol, PERIOD_CURRENT, 0);
|
| double c1 = iClose(_Symbol, PERIOD_CURRENT, 1);
|
| if(c1 <= PriceLevels[i] && c0 > PriceLevels[i]) FireSetup(true, "FIB-BREAK-UP", true);
|
| if(c1 >= PriceLevels[i] && c0 < PriceLevels[i]) FireSetup(false, "FIB-BREAK-DN", true);
|
| }
|
|
|
| for(int i = 1; i <= 5; ++i)
|
| {
|
| double c0 = iClose(_Symbol, PERIOD_CURRENT, 0);
|
| double c1 = iClose(_Symbol, PERIOD_CURRENT, 1);
|
| if(c1 < PriceLevels[i] && c0 > PriceLevels[i]) FireSetup(true, "FIB-RECLAIM-UP", true);
|
| if(c1 > PriceLevels[i] && c0 < PriceLevels[i]) FireSetup(false, "FIB-RECLAIM-DN", true);
|
| }
|
|
|
| // MA hugging is read against EMA7 (2026-09-17, user: the 7 is the magnet, not the 14/21)
|
| bool hugging = (Current_ATR > 0.0 && (MathAbs(current - MA_Current[0]) / Current_ATR) < 0.7);
|
| if(hugging && dir >= 0) FireSetup(true, "MAGNET-WALK-UP", true);
|
| if(hugging && dir <= 0) FireSetup(false, "MAGNET-WALK-DN", true);
|
|
|
| if(NearLevel(current, MFIB_Level_382, buffer) || NearLevel(current, MFIB_Level_618, buffer))
|
| {
|
| if(dir >= 0) FireSetup(true, "MFIB-LADDER-UP", true);
|
| if(dir <= 0) FireSetup(false, "MFIB-LADDER-DN", true);
|
| }
|
| }
|
|
|
| void ExecuteRangeSetups()
|
| {
|
| if(Current_Mode != MODE_RANGING)
|
| return;
|
|
|
| double current = MidPrice();
|
| double buffer = MA_Touch_Buffer;
|
|
|
| if(NearLevel(current, PriceLevels[1], buffer) && Stoch_K_Current < Stoch_Weak_Low)
|
| FireSetup(true, "RANGE-EDGE-BUY", false);
|
|
|
| if(NearLevel(current, PriceLevels[5], buffer) && Stoch_K_Current > Stoch_Weak_High)
|
| FireSetup(false, "RANGE-EDGE-SELL", false);
|
|
|
| if(NearLevel(current, MFIB_Level_382, buffer) && Stoch_K_Current < Stoch_Mid)
|
| FireSetup(true, "RANGE-MFIB-BUY", false);
|
|
|
| if(NearLevel(current, MFIB_Level_618, buffer) && Stoch_K_Current > Stoch_Mid)
|
| FireSetup(false, "RANGE-MFIB-SELL", false);
|
| }
|
|
|
| void ExecuteChopSetups()
|
| {
|
| if(Current_Mode != MODE_CHOP)
|
| return;
|
|
|
| if(Stoch_K_Current <= Stoch_Oversold && Active_Warnings >= Active_Praise_Signals)
|
| FireSetup(true, "CHOP-REV-BUY", false);
|
|
|
| if(Stoch_K_Current >= Stoch_Overbought && Active_Warnings >= Active_Praise_Signals)
|
| FireSetup(false, "CHOP-REV-SELL", false);
|
|
|
| double current = MidPrice();
|
| if(NearLevel(current, MA_Current[0], MA_Touch_Buffer) && Active_Warnings >= 2)
|
| {
|
| if(iClose(_Symbol, PERIOD_CURRENT, 0) < MA_Current[0]) FireSetup(true, "CHOP-SNAP-BUY", false);
|
| if(iClose(_Symbol, PERIOD_CURRENT, 0) > MA_Current[0]) FireSetup(false, "CHOP-SNAP-SELL", false);
|
| }
|
| }
|
|
|
| #endif
|