| #ifndef ENTRY_UPGRADE_MQH
|
| #define ENTRY_UPGRADE_MQH
|
| // ---------------------------------------------------------------------------
|
| // EntryUpgrade.mqh — v7.31 (2026-11-09) smarter entries instead of skips/size cuts.
|
| // Entry-upgrade lab (scripts/research/upgrades.py, 799 triggers, v7.25 exits, R of each entry's own stop):
|
| // MAGNET-WALK-UP enter now only when H1+H4 trends agree and not in London; else watch for a FAILED break
|
| // and trade the other way (+15.5R -> +69.1R, both halves better; London -65R -> +7R)
|
| // FIB-BREAK/RECLAIM same adaptive rule (FIB-BREAK-UP -29.4R -> +21.3R, FIB-BREAK-DN +4.8R -> +13.8R)
|
| // MAGNET-WALK-DN in London enter now; outside London wait for break -> retest -> hold (+196R -> ~+280R)
|
| // Watches are evaluated once per closed M1 bar. Upgraded orders are named FLIP-<setup> / RETEST-<setup>,
|
| // keep every risk guard and the AI gate, use a structure stop (beyond the spike / retest wick) and a target of
|
| // TP_ATR_Mult/SL_ATR_Mult x that stop; the v7.25 R-exits manage them.
|
| // ---------------------------------------------------------------------------
|
| #define EU_MAX 16
|
| #define EU_FLIP 1
|
| #define EU_RETEST 2
|
|
|
| struct EUWatch
|
| {
|
| bool used;
|
| int mode;
|
| string setup;
|
| bool buy; // direction of the ORIGINAL trigger
|
| double level; // trigger price
|
| double r0; // stop distance the trigger would have used (price)
|
| datetime t0;
|
| datetime bar_end; // end of the trigger's chart bar (RETEST confirmation)
|
| bool confirmed;
|
| bool touched;
|
| double ext; // FLIP: extreme in the trigger direction; RETEST: worst adverse during the retest
|
| };
|
| EUWatch g_eu[EU_MAX];
|
| int g_sl_override_pts = 0; // consumed by OpenBuy/OpenSell (one order)
|
| int g_tp_override_pts = 0;
|
|
|
| string EU_Base(const string name)
|
| {
|
| int bar = StringFind(name, " | ");
|
| string b = (StringFind(name, "RE-ENTRY") == 0 && bar >= 0) ? StringSubstr(name, bar + 3) : name;
|
| // FIB-RECLAIM uses the same trigger as FIB-BREAK (audit 2026-11-09): one watch, one rule set
|
| if(StringFind(b, "FIB-RECLAIM") == 0) b = "FIB-BREAK" + StringSubstr(b, 11);
|
| return b;
|
| }
|
| bool EU_IsUpgraded(const string name) { return StringFind(name, "FLIP-") == 0 || StringFind(name, "RETEST-") == 0; }
|
|
|
| bool EU_InHours(const string spec)
|
| {
|
| int d = StringFind(spec, "-");
|
| if(d <= 0) return false;
|
| int a = (int)StringToInteger(StringSubstr(spec, 0, d)), b = (int)StringToInteger(StringSubstr(spec, d + 1));
|
| MqlDateTime t; TimeToStruct(TimeCurrent(), t);
|
| return t.hour >= a && t.hour <= b;
|
| }
|
|
|
| // H1 and H4 EMA20/50 trend agreement with the trade direction (closed bars). Unknown data = agree.
|
| bool EU_BothTrendsWith(const bool buy)
|
| {
|
| static int h1a = INVALID_HANDLE, h1b = INVALID_HANDLE, h4a = INVALID_HANDLE, h4b = INVALID_HANDLE;
|
| if(h1a == INVALID_HANDLE) h1a = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE);
|
| if(h1b == INVALID_HANDLE) h1b = iMA(_Symbol, PERIOD_H1, 50, 0, MODE_EMA, PRICE_CLOSE);
|
| if(h4a == INVALID_HANDLE) h4a = iMA(_Symbol, PERIOD_H4, 20, 0, MODE_EMA, PRICE_CLOSE);
|
| if(h4b == INVALID_HANDLE) h4b = iMA(_Symbol, PERIOD_H4, 50, 0, MODE_EMA, PRICE_CLOSE);
|
| double a[1], b[1], c[1], d[1];
|
| if(CopyBuffer(h1a, 0, 1, 1, a) != 1 || CopyBuffer(h1b, 0, 1, 1, b) != 1 ||
|
| CopyBuffer(h4a, 0, 1, 1, c) != 1 || CopyBuffer(h4b, 0, 1, 1, d) != 1) return true;
|
| int s = buy ? 1 : -1;
|
| return (a[0] - b[0]) * s > 0.0 && (c[0] - d[0]) * s > 0.0;
|
| }
|
|
|
| int EU_Mode(const string base, const bool buy)
|
| {
|
| bool london = EU_InHours(EU_London_Hours);
|
| if(StringLen(EU_Flip_Setups) > 0 && TP_MatchesAny(base, EU_Flip_Setups))
|
| {
|
| if(StringLen(EU_Flip_In_London) > 0 && TP_MatchesAny(base, EU_Flip_In_London) && london) return EU_FLIP;
|
| if(!EU_BothTrendsWith(buy)) return EU_FLIP;
|
| return 0;
|
| }
|
| if(StringLen(EU_Retest_Outside_London) > 0 && TP_MatchesAny(base, EU_Retest_Outside_London) && !london) return EU_RETEST;
|
| return 0;
|
| }
|
|
|
| // Called at the top of OpenBuy/OpenSell. true = handled here (a watch exists/was registered): do not open now.
|
| bool EU_Intercept(const bool buy, const string name)
|
| {
|
| if(!Use_Entry_Upgrades || EU_IsUpgraded(name)) return false;
|
| string base = EU_Base(name);
|
| int mode = EU_Mode(base, buy);
|
| if(mode == 0) return false;
|
| for(int i = 0; i < EU_MAX; ++i)
|
| if(g_eu[i].used && g_eu[i].setup == base && g_eu[i].buy == buy && g_eu[i].mode == mode) return true; // already watching
|
| int k = -1;
|
| for(int i = 0; i < EU_MAX; ++i) if(!g_eu[i].used) { k = i; break; }
|
| if(k < 0) return true;
|
| double r0 = GetSLPoints(true) * _Point;
|
| if(r0 <= 0.0) return true;
|
| int tf = PeriodSeconds(PERIOD_CURRENT);
|
| g_eu[k].used = true; g_eu[k].mode = mode; g_eu[k].setup = base; g_eu[k].buy = buy;
|
| g_eu[k].level = MidPrice(); g_eu[k].r0 = r0; g_eu[k].t0 = TimeCurrent();
|
| g_eu[k].bar_end = (datetime)((TimeCurrent() / tf) * tf + tf);
|
| g_eu[k].confirmed = false; g_eu[k].touched = false;
|
| g_eu[k].ext = MidPrice();
|
| Print("EntryUpgrade: ", name, (buy ? " BUY" : " SELL"), " -> ", (mode == EU_FLIP ? "watching for a failed break (flip)" : "waiting for break-retest"),
|
| " at ", DoubleToString(g_eu[k].level, _Digits), " (R0 ", DoubleToString(r0, _Digits), ")");
|
| return true;
|
| }
|
|
|
| double EU_Clamp(const double d, const double r0) { return MathMax(EU_Stop_Min_R * r0, MathMin(EU_Stop_Max_R * r0, d)); }
|
|
|
| bool EU_Open(const bool buy, const string name, const double stop_dist)
|
| {
|
| double ratio = (SL_ATR_Mult > 0.0) ? TP_ATR_Mult / SL_ATR_Mult : 2.33;
|
| g_sl_override_pts = (int)MathMax(MinStopPoints(), stop_dist / _Point);
|
| g_tp_override_pts = (int)MathMax(MinStopPoints(), ratio * stop_dist / _Point);
|
| bool ok = buy ? OpenBuy(name) : OpenSell(name);
|
| g_sl_override_pts = 0; g_tp_override_pts = 0;
|
| return ok;
|
| }
|
|
|
| // Once per closed M1 bar.
|
| void EU_Tick()
|
| {
|
| if(!Use_Entry_Upgrades) return;
|
| static datetime last_m1 = 0;
|
| datetime m1 = iTime(_Symbol, PERIOD_M1, 1);
|
| if(m1 == 0 || m1 == last_m1) return;
|
| last_m1 = m1;
|
| double hi = iHigh(_Symbol, PERIOD_M1, 1), lo = iLow(_Symbol, PERIOD_M1, 1), cl = iClose(_Symbol, PERIOD_M1, 1);
|
| for(int i = 0; i < EU_MAX; ++i)
|
| {
|
| if(!g_eu[i].used) continue;
|
| int s = g_eu[i].buy ? 1 : -1;
|
| double L = g_eu[i].level, r0 = g_eu[i].r0;
|
| if(g_eu[i].mode == EU_FLIP)
|
| {
|
| if(TimeCurrent() - g_eu[i].t0 > EU_Flip_Window_Min * 60) { g_eu[i].used = false; continue; } // break held: no trade
|
| double fav = g_eu[i].buy ? hi : lo;
|
| if((fav - g_eu[i].ext) * s > 0.0) g_eu[i].ext = fav;
|
| if((cl - L) * s < -EU_Flip_Through_R * r0)
|
| {
|
| double d = EU_Clamp((g_eu[i].ext - cl) * s + EU_Stop_Buffer_R * r0, r0);
|
| string nm = "FLIP-" + g_eu[i].setup;
|
| Print("EntryUpgrade: ", g_eu[i].setup, " break failed at ", DoubleToString(cl, _Digits), " -> ", (g_eu[i].buy ? "SELL" : "BUY"),
|
| " ", nm, " stop beyond the spike (", DoubleToString(d / r0, 2), " R0)");
|
| EU_Open(!g_eu[i].buy, nm, d);
|
| g_eu[i].used = false;
|
| }
|
| }
|
| else if(g_eu[i].mode == EU_RETEST)
|
| {
|
| if(!g_eu[i].confirmed)
|
| {
|
| if(TimeCurrent() < g_eu[i].bar_end) continue;
|
| int tf = PeriodSeconds(PERIOD_CURRENT);
|
| int sh = iBarShift(_Symbol, PERIOD_CURRENT, (datetime)(g_eu[i].bar_end - tf), false);
|
| double c = (sh >= 1) ? iClose(_Symbol, PERIOD_CURRENT, sh) : 0.0;
|
| if(c == 0.0 || (c - L) * s <= 0.0) { g_eu[i].used = false; continue; } // bar did not close beyond: no break
|
| g_eu[i].confirmed = true;
|
| continue;
|
| }
|
| if(TimeCurrent() - g_eu[i].bar_end > EU_Retest_Window_Min * 60) { g_eu[i].used = false; continue; } // never retested
|
| double adv = g_eu[i].buy ? lo : hi;
|
| if(!g_eu[i].touched)
|
| {
|
| if((adv - (L + s * EU_Stop_Buffer_R * r0)) * s <= 0.0) { g_eu[i].touched = true; g_eu[i].ext = adv; }
|
| continue;
|
| }
|
| if((adv - g_eu[i].ext) * s < 0.0) g_eu[i].ext = adv;
|
| if((cl - L) * s > 0.0)
|
| {
|
| double d = EU_Clamp((cl - g_eu[i].ext) * s + EU_Stop_Buffer_R * r0, r0);
|
| string nm = "RETEST-" + g_eu[i].setup;
|
| Print("EntryUpgrade: ", g_eu[i].setup, " retest held at ", DoubleToString(L, _Digits), " -> ", (g_eu[i].buy ? "BUY" : "SELL"),
|
| " ", nm, " stop beyond the retest wick (", DoubleToString(d / r0, 2), " R0)");
|
| EU_Open(g_eu[i].buy, nm, d);
|
| g_eu[i].used = false;
|
| }
|
| }
|
| }
|
| }
|
| #endif
|