#ifndef ROTATION_MQH #define ROTATION_MQH // --------------------------------------------------------------------------- // Rotation.mqh — v7.23 watchlist rotation (2026-11-09) // One EA chart is the leader (terminal globals VZ_ROT_LEADER / VZ_ROT_BEAT, taken over when the // heartbeat is older than 3 checks). Every Rotation_Check_Seconds the leader asks the gateway for // the active symbols (watchlist_status with symbol "*"), opens an EA chart (Rotation_Template) for // each active symbol that has none, and closes EA charts whose symbol left the rotation once they // hold no positions. Fails safe: no list from the gateway = no changes. // --------------------------------------------------------------------------- void RotNote(const string s) { static string last = ""; if(s != last) { Print("Rotation: ", s); last = s; } } double RotChartKey(const long id) { return (double)(id % 1000000000000); } // fits a double exactly bool RotIsLeader() { double me = RotChartKey(ChartID()); datetime now = TimeLocal(); if(GlobalVariableCheck("VZ_ROT_LEADER") && GlobalVariableCheck("VZ_ROT_BEAT")) { double leader = GlobalVariableGet("VZ_ROT_LEADER"); datetime beat = (datetime)GlobalVariableGet("VZ_ROT_BEAT"); if(leader != me && now - beat < 3 * MathMax(60, Rotation_Check_Seconds)) return false; } GlobalVariableSet("VZ_ROT_LEADER", me); GlobalVariableSet("VZ_ROT_BEAT", (double)now); return true; } bool RotIsEAChart(const long c) { return ChartGetString(c, CHART_EXPERT_NAME) == MQLInfoString(MQL_PROGRAM_NAME); } bool RotHasPositions(const string sym) { for(int i = PositionsTotal() - 1; i >= 0; --i) { ulong t = PositionGetTicket(i); if(t == 0 || !PositionSelectByTicket(t)) continue; if(PositionGetString(POSITION_SYMBOL) == sym && PositionGetInteger(POSITION_MAGIC) == MagicNumber) return true; } return false; } // v7.24: the rotation only ever closes charts it opened itself (terminal global VZ_ROT_OWN_). // Charts the owner opens by hand are never closed; if their symbol is off the active list they are manage-only. string RotOwnKey(const long c) { return "VZ_ROT_OWN_" + DoubleToString(RotChartKey(c), 0); } bool RotOwned(const long c) { return GlobalVariableCheck(RotOwnKey(c)); } bool RotInList(const string sym, string &list[]) { for(int i = 0; i < ArraySize(list); ++i) if(StringCompare(list[i], sym, false) == 0) return true; return false; } void RotationTick() { if(!Use_Watchlist_Rotation || !GW_Initialized || MQLInfoInteger(MQL_TESTER)) return; static datetime last = 0; if(TimeLocal() - last < MathMax(60, Rotation_Check_Seconds)) return; last = TimeLocal(); if(!RotIsLeader()) return; TradeSyncTick(); // v7.26: the leader also reconciles closed trades (own throttle) // one-time (v7.24): claim the six M15 charts v7.23 opened at 09:55 on 2026-11-09, before ownership was tracked if(!GlobalVariableCheck("VZ_ROT_ADOPTED_V724")) { string v723 = ",US Oil,USDJPY,GBPJPY,USDCAD,GBPUSD,AUDUSD,"; for(long c = ChartFirst(); c >= 0; c = ChartNext(c)) if(RotIsEAChart(c) && ChartPeriod(c) == PERIOD_M15 && StringFind(v723, "," + ChartSymbol(c) + ",") >= 0) GlobalVariableSet(RotOwnKey(c), 1.0); GlobalVariableSet("VZ_ROT_ADOPTED_V724", 1.0); } string resp, csv = ""; if(!GatewayRequest("watchlist_status", "{\"action\":\"watchlist_status\",\"symbol\":\"*\"}", resp)) { RotNote("gateway unavailable - no changes"); return; } if(!GWExtractString(resp, "active_symbols", csv) || StringLen(csv) == 0) { RotNote("no active list from the gateway - no changes"); return; } string active[]; int n = StringSplit(csv, ',', active); for(int i = 0; i < n; ++i) { StringTrimLeft(active[i]); StringTrimRight(active[i]); } // close EA charts that left the rotation (never this chart, never one holding positions) int ea_charts = 0; for(long c = ChartFirst(); c >= 0; ) { long next = ChartNext(c); if(RotIsEAChart(c)) { string sym = ChartSymbol(c); if(c != ChartID() && RotOwned(c) && !RotInList(sym, active) && !RotHasPositions(sym)) { Print("Rotation: closing ", sym, " chart - left the active rotation and is flat"); if(ChartClose(c)) GlobalVariableDel(RotOwnKey(c)); } else ea_charts++; } c = next; } // open an EA chart for each active symbol without one string opened = "", missing = ""; for(int i = 0; i < n; ++i) { string sym = active[i]; if(StringLen(sym) == 0) continue; bool has = false; for(long c = ChartFirst(); c >= 0; c = ChartNext(c)) if(RotIsEAChart(c) && StringCompare(ChartSymbol(c), sym, false) == 0) { has = true; break; } if(has) continue; if(ea_charts >= Rotation_Max_Charts) { RotNote("at Rotation_Max_Charts (" + IntegerToString(Rotation_Max_Charts) + ") - not opening more"); break; } if(!SymbolSelect(sym, true)) { missing += (StringLen(missing) ? "," : "") + sym; continue; } long id = ChartOpen(sym, Rotation_Timeframe); if(id == 0) { Print("Rotation: could not open a chart for ", sym, " (error ", GetLastError(), ")"); continue; } if(!ChartApplyTemplate(id, Rotation_Template)) { Print("Rotation: template ", Rotation_Template, " failed on ", sym, " (error ", GetLastError(), ") - chart closed"); ChartClose(id); continue; } GlobalVariableSet(RotOwnKey(id), 1.0); ea_charts++; opened += (StringLen(opened) ? "," : "") + sym; } if(StringLen(opened)) Print("Rotation: opened EA charts for ", opened); if(StringLen(missing)) RotNote("not offered by the broker, skipped: " + missing); } #endif