MQL4 Objects delete question

 

I am new to mql4 coding. This is my first indicator using arrays and I'm having problems with the object delete function, its not removing indicator after removing from the chart?

I have looked everywhere for a solution I can't see how this is not working, does anyone know what needs changing below?

Thanks in advance

 

#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict



int            period[]={5,60,240,1440,10080};
string         periodString[]={" M5"," H1"," H4"," D1"," W1"};
string         signalNameString[]={"MA"};


extern int     MA_Period = 50,
                  MA_method = 0; 
extern int     scaleX=30, // vertical spacing
               scaleY=20, // horizontal spacing (multiple indicators)
               offsetX=35, // Distance from left of chart
               offsetY=50, // Distance from top of chart
               fontSize=18, // font size
               corner=0; // corner


int init()
{

      // a table of signals
   for(int x=0;x<5;x++)
      for(int y=0;y<1;y++)
      {
         ObjectCreate("signal"+x+y,OBJ_LABEL,0,0,0,0,0);
         ObjectSet("signal"+x+y,OBJPROP_CORNER,corner);
         ObjectSet("signal"+x+y,OBJPROP_XDISTANCE,x*scaleX+offsetX);
         ObjectSet("signal"+x+y,OBJPROP_YDISTANCE,y*scaleY+offsetY);
         ObjectSetText("signal"+x+y,CharToStr(110),fontSize,"Wingdings",Gold);
      }
 
      // name of timeframes
   for(int x=0;x<5;x++)
   {
      ObjectCreate("textPeriod"+x,OBJ_LABEL,0,0,0,0,0);
      ObjectSet("textPeriod"+x,OBJPROP_CORNER,corner);
      ObjectSet("textPeriod"+x,OBJPROP_XDISTANCE,x*scaleX+offsetX);
      ObjectSet("textPeriod"+x,OBJPROP_YDISTANCE,offsetY-10);
      ObjectSetText("textPeriod"+x,periodString[x],8,"Tahoma",Gold);
   }
   
       // names of indicators
   for(int y=0;y<1;y++)
   {
      ObjectCreate("textSignal"+y,OBJ_LABEL,0,0,0,0,0);
      ObjectSet("textSignal"+y,OBJPROP_CORNER,corner);
      ObjectSet("textSignal"+y,OBJPROP_XDISTANCE,offsetX-25);
      ObjectSet("textSignal"+y,OBJPROP_YDISTANCE,y*scaleY+offsetY+8);
      ObjectSetText("textSignal"+y,signalNameString[y],8,"Tahoma",Gold);
   }
   
   
   return(0);
}


int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

  for(int x=0;x<9;x++)
  
         {
      if (iMA (Symbol(),period[x], MA_Period,0,MA_method,PRICE_CLOSE,1) > iMA (Symbol(),period[x], MA_Period,0,MA_method,PRICE_CLOSE,2))
          ObjectSetText("signal"+x+"0",CharToStr(236),fontSize,"Wingdings",LimeGreen);
         
      else
         ObjectSetText("signal"+x+"0",CharToStr(238),fontSize,"Wingdings",Red);
         }
   
   return(0);
  }
//+------------------------------------------------------------------+

int deinit()
   {
   ObjectsDeleteAll();
   return(0);
   }
 

It is bad practice to use ObjectsDeleteAll, as objects not drawn by the indicator will also be deleted.

string Name=WindowExpertName();

 

I normally name my objects beginning with WindowExpertName(), then loop through the objects and delete those that begin with it in deinit

   for(int i=ObjectsTotal()-1;i>=0;i--)
     {
      string ObName=ObjectName(i);
      if(StringFind(ObName,Name,0)!=-1)
         ObjectDelete(ObName);
     }

Check for an array out of range error as this can stop the indicator without executing deinit. 

 

  for(int x=0;x<9;x++)
  
         {
      if (iMA (Symbol(),period[x], MA_Period,0,MA_method,PRICE_CLOSE,1) > iMA (Symbol(),period[x], MA_Period,0,MA_method,PRICE_CLOSE,2))
          ObjectSetText("signal"+x+"0",CharToStr(236),fontSize,"Wingdings",LimeGreen);
         
      else
         ObjectSetText("signal"+x+"0",CharToStr(238),fontSize,"Wingdings",Red);
         }
   

That is probably it.

period[] only has 5 elements, you are trying to access 9 

 
GumRai:

That is probably it.

period[] only has 5 elements, you are trying to access 9 

Fixed

Thanks GumRai 

 
GumRai:

It is bad practice to use ObjectsDeleteAll, as objects not drawn by the indicator will also be deleted.

 

I normally name my objects beginning with WindowExpertName(), then loop through the objects and delete those that begin with it in deinit

Check for an array out of range error as this can stop the indicator without executing deinit. 

 

   ObjectsDeleteAll(0,Name);
 
zirkoner:

Thanks Zirkoner

Reason: