for(int i=ObjectsTotal()-1;i>=0;i--) if(StringFind(ObjectName(i),"_tbeg",0)>0) ObjectDelete(ObjectName(i));
Try to change loop direction
for(int i=ObjectsTotal()-1;i>=0;i--) if(StringFind(ObjectName(i),"_tbeg",0)>0) ObjectDelete(ObjectName(i));
That works. Thanks stringo!
Bill
FWIW this no longer works. Maybe there was a change with how StringFind operates? Anyway, this is what I use now which works fine.
//+------------------------------------------------------------------+ //| RemoveObjects.mq4 | //| Bill Doerrfeld | //| billworld@mac.com | //+------------------------------------------------------------------+ #property copyright "Bill Doerrfeld" #property link "billworld@mac.com" #property show_inputs extern string NameOfObject = "test"; //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ int start() { for(int i = ObjectsTotal() - 1; i >= 0; i--) { string label = ObjectName(i); if(StringSubstr(label, 0, StringLen(NameOfObject)) != NameOfObject) continue; ObjectDelete(label); } return(0); } //+------------------------------------------------------------------+
I've checked your code. It works. More details please
To be clear, in my last post I supplied two code excerpts. The top one which uses StringFind does NOT work but it used to work in earlier versions of MT4. The bottom one which uses StringSubstr DOES work and this is the method I now use to remove objects. No big deal, I just struggled with trying to figure out why StringFind wasn't yielding the expected result until I just decided to go with StringSubstr.
Once more - it works.
Hint for your code
if(StringFind(ObjectName(i),"_tbeg",0)>0)
Your objects should have such names as "1_tbegTrendLine", "XXX_tbegSymbol" and so on. If your objects begin from "_tbeg" then You must change condition
if(StringFind(ObjectName(i),"_tbeg",0)==0)
or
if(StringFind(ObjectName(i),"_tbeg",0)>=0)
See https://docs.mql4.com/strings/StringFind. Position begins from 0. So was always.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
What am I doing wrong?
Thanks
Bill