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)); + }