void OnTimer(){ //clear unused variables int deletedCount = 0; VerboseLog("Clearing variables..."); for(int a=GlobalVariablesTotal() - 1; a>=0; a--){ string variableName = GlobalVariableName(a); if(GlobalVariableCheck(variableName)){ int endPos = StringFind(variableName, "_"); if(endPos <= 0) continue; int ticketNo = StrToInteger(StringSubstr(variableName, 0, endPos)); bool variableUsed = false; for(int i=0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS) == true) { if(OrderTicket() == ticketNo){ variableUsed = true; break; } } } ResetLastError(); if(!variableUsed){ if(GlobalVariableDel(variableName)){ deletedCount++; } else { VerboseLog("Cannot delete variable. Error code: ", GetLastError()); } } } } VerboseLog(IntegerToString(deletedCount), " variables cleared"); }
I suggest using this code for creating GV
//+------------------------------------------------------------------+ void sqSetGlobalVariable(int ticket, string name, double value) { GlobalVariableSet(CustomComment+ (IsTesting()?"_test_":"_") + ticket+"_"+name, value); } //+------------------------------------------------------------------+
And the following code to delete them
GlobalVariablesDeleteAll(CustomComment + (IsTesting()?"_test":"_"));