From 36ae668706b110ae6859c13be19752ea7e4bea00 Mon Sep 17 00:00:00 2001 From: Garfield Date: Tue, 11 Aug 2026 10:46:28 -0400 Subject: [PATCH] Add CancelAllPendingNow: companion cleanup script to CloseAllNow Needed today: root-caused the "trades are losing" report to a second, deeper persistence bug -- gridPlaced (and the rest of SaveGridState's GlobalVariable-based state) never reaches disk in this environment at all (no .gvr file exists anywhere under the MT5 data dir). It survives a same-process EA reinit (input edit) fine, but a full container restart loses it completely, so every restart made every EA think no grid was resting and place a fresh one on top of whatever was already there, uncancelled. Confirmed via ~29 stacked pending orders on some symbols spanning 24+ hours of normal hourly cycles, not just last night's restarts -- this has likely been happening on every watchdog restart for weeks. Ran it today: cancelled 177 stacked/duplicate pending orders fleet- wide, 0 remaining. Open positions untouched -- each EA's own OnTick "Cycle closed?" check self-heals from an empty pending book and places exactly one clean grid on its next tick. --- CancelAllPendingNow.mq5 | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CancelAllPendingNow.mq5 diff --git a/CancelAllPendingNow.mq5 b/CancelAllPendingNow.mq5 new file mode 100644 index 0000000..324cfb9 --- /dev/null +++ b/CancelAllPendingNow.mq5 @@ -0,0 +1,45 @@ +//+------------------------------------------------------------------+ +//| CancelAllPendingNow.mq5 | +//| One-shot utility: cancel every pending order on the account. | +//| Open positions are left alone -- SL/trail/EA management is | +//| untouched. Companion to CloseAllNow.mq5 (which is the reverse: | +//| closes positions, leaves pendings). Written 2026-08-11 to clean | +//| up duplicate grid orders stacked by the gridPlaced GlobalVariable| +//| not surviving container restarts (see vault note on the | +//| persistence bug) -- each restart made every EA think no grid was| +//| resting and place a brand new one on top of the old, un- | +//| cancelled one. Safe to run any time: any EA that still has zero | +//| orders/positions after this will self-heal and place exactly | +//| one clean fresh grid on its own next tick (see OnTick's | +//| "Cycle closed?" branch). | +//+------------------------------------------------------------------+ +#property copyright "Copyright 2026, Garfield Heron" +#property version "1.0" +#property script_show_inputs false + +#include + +void OnStart() + { + CTrade trade; + ulong start = GetTickCount64(); + int sweep = 0, cancelled = 0; + while(!IsStopped() && GetTickCount64() - start < 600000) + { + for(int i = OrdersTotal() - 1; i >= 0; i--) + { + ulong ticket = OrderGetTicket(i); + if(ticket == 0) continue; + if(trade.OrderDelete(ticket)) + cancelled++; + } + if(OrdersTotal() == 0) break; + sweep++; + if(sweep % 5 == 1) + Print("CancelAllPendingNow: sweep ", sweep, " — ", OrdersTotal(), " still pending, retrying..."); + Sleep(3000); + } + Print("CancelAllPendingNow: DONE — cancelled ", cancelled, ", remaining ", OrdersTotal(), + ". Equity=", DoubleToString(AccountInfoDouble(ACCOUNT_EQUITY), 2), + " Balance=", DoubleToString(AccountInfoDouble(ACCOUNT_BALANCE), 2)); + }