ObjectDelete(0,label_name); can delete all object with the same name pluss index add? - page 2

 

OnDeInit TimeToString: jj is not the datetime of the bar.

Anyway it makes more sense to iterate over the objects attachted to the chart as suggested by forex2start.

Because I think there are always less objects than bars.

Uwe


 
ugo58:

OnDeInit TimeToString: jj is not the datetime of the bar.

Anyway it makes more sense to iterate over the objects attachted to the chart as suggested by forex2start.

Because I think there are always less objects than bars.


Uwe


ugo58:

OnDeInit TimeToString: jj is not the datetime of the bar.

thank you. i fix it .

.
.
TOTAL_BAR=rates_total;
.
.
//+------------------------------------------------------------------+
//| Function for drawing a trend line in the indicator window        |
//+------------------------------------------------------------------+
void DrawIndicatorTrendLine(int bar,datetime x1,datetime x2,double y1,double y2,color lineColor,int style)         
  {
   int indicatorWindow=ChartWindowFind(0,short_name);  // short_name  indicatorName
   if(indicatorWindow<0) return;
   string label="AO_Indicator_Divergence"+IntegerToString(bar)+TimeToString(x2);
   if(ObjectFind(0,label)==-1)
     {
      ObjectCreate(0,label,OBJ_TREND,indicatorWindow,x1,y1,x2,y2);
      ObjectSetInteger(0,label,OBJPROP_COLOR,lineColor);
      ObjectSetInteger(0,label,OBJPROP_STYLE,style);
      ObjectSetInteger(0,label,OBJPROP_WIDTH,0);
      ObjectSetInteger(0,label,OBJPROP_RAY,0);
      ObjectSetInteger(0,label,OBJPROP_BACK,false);
     }
   else
     {
      ObjectMove(0,label,0,x1,y1);
      ObjectMove(0,label,1,x2,y2);
     }
  }
//----------------------------------------------------------------------------------------------------------//
//+------------------------------------------------------------------+
//| Function for drawing a trend line in a price chart window        |
//+------------------------------------------------------------------+
void DrawPriceTrendLine(int bar,datetime x1,datetime x2,double y1,double y2,color lineColor,int style)
  {
   string label="AO_Price_Divergence"+IntegerToString(bar)+TimeToString(x2);
   if(ObjectFind(0,label)==-1)
     {
      ObjectCreate(0,label,OBJ_TREND,0,x1,y1,x2,y2);
      ObjectSetInteger(0,label,OBJPROP_COLOR,lineColor);
      ObjectSetInteger(0,label,OBJPROP_STYLE,style);
      ObjectSetInteger(0,label,OBJPROP_WIDTH,0);
      ObjectSetInteger(0,label,OBJPROP_RAY,0);
      ObjectSetInteger(0,label,OBJPROP_BACK,false);
     }
   else
     {
      ObjectMove(0,label,0,x1,y1);
      ObjectMove(0,label,1,x2,y2);
     }
  }
//----------------------------------------------------------------------------------------------------------//

&

void OnDeinit(const int reason)
  {
   Comment("");
   //ObjectsDeleteAll(0,0,OBJ_ARROW);
   //ObjectsDeleteAll(0,-1,OBJ_ARROW);
   //ObjectsDeleteAll(0,0,OBJ_TREND);
   //ObjectsDeleteAll(0,-1,OBJ_TREND);
   //ObjectsDeleteAll(0);
   //ObjectsDeleteAll(0,0,OBJ_VLINE);
   //ObjectsDeleteAll(0,0,OBJ_RECTANGLE);
   
   for(int j=1;j<=TOTAL_BAR;j++)
      {  
       for(datetime jj=1;jj<TOTAL_BAR;jj++)
          {  
           if(ObjectFind(0,"AO_Indicator_Divergence"+IntegerToString(j)+TimeToString(jj))>=0) 
              ObjectDelete(0,"AO_Indicator_Divergence"+IntegerToString(j)+TimeToString(jj));
           if(ObjectFind(0,"AO_Price_Divergence"+IntegerToString(j)+TimeToString(jj))>=0) 
              ObjectDelete(0,"AO_Price_Divergence"+IntegerToString(j)+TimeToString(jj));    
          }
      }

   ChartRedraw();
   ChartSetInteger(0,CHART_FOREGROUND,foreground);
  }


ugo58:

Anyway it makes more sense to iterate over the objects attachted to the chart as suggested by forex2start.

Because I think there are always less objects than bars.

yes , is this make error?

compiling has no error or warning

but chart have problem.

===================================================

when i use :

ObjectsDeleteAll(0,-1,OBJ_TREND);

there is no problem.

but when i inactive it by comment it & active 2 for(...) in  " void OnDeinit(const int reason) " as like as shown in above , chart have problem. below pic.


how can fix this problem?

thank you.

 
TIMisthebest:


...

how can fix this problem?

thank you.

ObjectDelete() is a function, it return a value, you have to check this value to build robust code.

Your loops in DeInit() don't give you the right name for your objects. This is why you get problem.

Try this way :

   for(int j=0;j<TOTAL_BAR;j++)
      {  
      if(StringFind(ObjectName(0,j),"AO_Indicator_Divergence")!=-1 || StringFind(ObjectName(0,j),"AO_Price_Divergence")!=-1)
         if(!ObjectDelete(0,ObjectName(0,j)))
            Print("Error in deleting object ", ObjectName(0,j), "(", GetLastError(),")");
      }

Compiled but not tested.

 
angevoyageur:

ObjectDelete() is a function, it return a value, you have to check this value to build robust code.

Your loops in DeInit() don't give you the right name for your objects. This is why you get problem.

Try this way :

Compiled but not tested.

thank you for new & right way;

but still have problem: (pic. below)


can you please check if no need to :

void OnDeinit(const int reason)
  {
   Comment("");
   //ObjectsDeleteAll(0,0,OBJ_ARROW);
   //ObjectsDeleteAll(0,-1,OBJ_ARROW);
   //ObjectsDeleteAll(0,0,OBJ_TREND);
   //ObjectsDeleteAll(0,-1,OBJ_TREND);
   
   //ObjectsDeleteAll(0);
   //ObjectsDeleteAll(0,0,OBJ_VLINE);
   //ObjectsDeleteAll(0,0,OBJ_RECTANGLE);

 /*
   for(int j=1;j<=TOTAL_BAR;j++)
      {  
       for(datetime jj=1;jj<TOTAL_BAR;jj++)
          {  
           if(ObjectFind(0,"AO_Indicator_Divergence"+IntegerToString(j)+TimeToString(jj))>=0) 
              ObjectDelete(0,"AO_Indicator_Divergence"+IntegerToString(j)+TimeToString(jj));
           if(ObjectFind(0,"AO_Price_Divergence"+IntegerToString(j)+TimeToString(jj))>=0) 
              ObjectDelete(0,"AO_Price_Divergence"+IntegerToString(j)+TimeToString(jj));    
          }
      }   
*/
      for(int j=0;j<TOTAL_BAR;j++)
      {  
      if(StringFind(ObjectName(0,j,-1),"AO_Indicator_Divergence")!=-1 || StringFind(ObjectName(0,j,-1),"AO_Price_Divergence")!=-1)
         //ObjectDelete(0,ObjectName(0,j,-1));
         if(!ObjectDelete(0,ObjectName(0,j,-1)))
            Print("Error in deleting object ", ObjectName(0,j,-1), "(", GetLastError(),")");
      }
      
      
   ChartRedraw();
   ChartSetInteger(0,CHART_FOREGROUND,foreground);
  }

and it also have problem.  

 
TIMisthebest:

thank you for new & right way;

but still have problem: (pic. below)


can you please check if no need to :

and it also have problem.  

There is no need to use third parameter for ObjectName to -1, as this is the default value.

Sorry, I have not paid enough attention to your issue, try this way :

   for(int j=ObjectsTotal()-1;j>=0;j--)
      {  
      if(StringFind(ObjectName(0,j),"AO_Indicator_Divergence")!=-1 || StringFind(ObjectName(0,j),"AO_Price_Divergence")!=-1)
         if(!ObjectDelete(0,ObjectName(0,j)))
            Print("Error in deleting object (", GetLastError(),")");
      }
 
angevoyageur:

There is no need to use third parameter for ObjectName to -1, as this is the default value.

Sorry, I have not paid enough attention to your issue, try this way :

?


 
angevoyageur:

There is no need to use third parameter for ObjectName to -1, as this is the default value.

Sorry, I have not paid enough attention to your issue, try this way :


thank you

   for(int j=ObjectsTotal(0)-1;j>=0;j--)
      {  
      if(StringFind(ObjectName(0,j),"AO_Indicator_Divergence")!=-1 || StringFind(ObjectName(0,j),"AO_Price_Divergence")!=-1)
         if(!ObjectDelete(0,ObjectName(0,j)))
            Print("Error in deleting object (", GetLastError(),")");
      }
 

THANK YOU ALL

problem solved.


Reason: