I run into the strangest situation where trend lines can are not deleted – and there is no error code. Here’s a 35 lines program (indicator) that creates 20 simple trendlines. In the deinit() section it delete all of them – but only half are actually deleted.
I would be grateful if someone can tell me where am I wrong.
Your problem is similar to a common mistake done when closing orders in a loop... You must use a loop with a decrementing loop counter.
Every time u delete an object, all objects that have a higher index in the object list will have their index decrement by 1. Hence in your loop u only delete every other order. If u start from the LAST object and count down, u won't miss any of them. Change to this:
for(int i=obj_total-1;i>=0;i--) ObjectDelete(ObjectName(i));
p.s. Please use the SRC button for code.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I run into the strangest situation where trend lines can are not deleted – and there is no error code. Here’s a 35 lines program (indicator) that creates 20 simple trendlines. In the deinit() section it delete all of them – but only half are actually deleted.
I would be grateful if someone can tell me where am I wrong.
#property indicator_separate_window
bool once = true;
int init()
{
IndicatorShortName("A");
return(0);
}
int deinit()
{
int obj_total=ObjectsTotal();
string objectname;
for(int i=0;i<obj_total;i++)
ObjectDelete(ObjectName(i));
return(0);
}
int start()
{
if (once)
for(int i = 20; i > 0; i--)
{
double sPrice = iClose(NULL, 5, i*20);
datetime sTime = iTime(NULL, 5, i*20);
double ePrice = iClose(NULL, 5, i*10);
datetime eTime = iTime(NULL, 5, i*10);
string name = "S"+TimeToStr(sTime, TIME_DATE|TIME_SECONDS));
if (! ObjectCreate(name, OBJ_TREND, 0, sTime, sPrice, eTime, ePrice))
Alert(Symbol()+" [create object="+name+"] [Error="+GetLastError( )+"]");
}
once = false;
return(0);
}