Unable to deinit Object Create

 

Hi all,

I'm still new to MQL4, so I'm trying to complete someone's else indicator for shooting star Candlestick, but I can't seem to remove the object text that was created when I stop the indicator, and at the same time when I change the timeframe,  the object from 5min will be carried over to other timeframes (15mins, 30mins and etc).  Really appreciate if someone can help to identify the issue, which I have been puzzled for weeks. 

 

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Orange
#property indicator_color2 Aqua
//----
extern bool Show_Alert = true;
extern bool Display_Inverted_Hammer = true;
//---- buffers

double downArrow[];
string PatternText[5000];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() 
  {
   SetIndexStyle(0, DRAW_ARROW, 0, 1);
   SetIndexArrow(0, 242);
   SetIndexBuffer(0, downArrow);      
     
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() 
  {
   ObjectsDeleteAll(0, OBJ_TEXT);
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double Range, AvgRange;
   int counter, setalert;
   static datetime prevtime = 0;
   int shift;
   int shift1;
   int shift2;
   int shift3;
   int shift4;
   string pattern, period;
   int setPattern = 0;
   int alert = 0;
   double O, O1, O2, O3, C, C1, C2, C3, L, L1, L2, L3, H, H1, H2, H3;
   double avgbody, avgbody1, avgbody2, avgbody3;     
//----
   if(prevtime == Time[0]) 
     {
       return(0);
     }
   prevtime = Time[0];   
//----
   switch(Period()) 
     {
       case 1:     period = "M1";  break;
       case 5:     period = "M5";  break;
       case 15:    period = "M15"; break;
       case 30:    period = "M30"; break;      
       case 60:    period = "H1";  break;
       case 240:   period = "H4";  break;
       case 1440:  period = "D1";  break;
       case 10080: period = "W1";  break;
       case 43200: period = "MN";  break;
     }
//----
   for(int j = 0; j < Bars; j++) 
     { 
       PatternText[j] = "pattern-" + j;
     }
//----
   for(shift = 0; shift < Bars; shift++) 
     {
       setalert = 0;
       counter = shift;
       Range = 0;
       AvgRange = 0; 
       for(counter = shift; counter <= shift + 9; counter++) 
         {
           AvgRange = AvgRange + MathAbs(High[counter] - Low[counter]);
         }
       Range = AvgRange / 10;
       shift1 = shift + 1;
       shift2 = shift + 2;
       shift3 = shift + 3;      
       O = Open[shift1];
       O1 = Open[shift2];
       O2 = Open[shift3];
       H = High[shift1];
       H1 = High[shift2];
       H2 = High[shift3];
       L = Low[shift1];
       L1 = Low[shift2];
       L2 = Low[shift3];
       C = Close[shift1];
       C1 = Close[shift2];
       C2 = Close[shift3]; 


     
  // Bearish Patterns  
         // Check for Shooting Star
        if((MathMax(O2,C2)<C1) && //uptrend
          (C1 > O1) && //2nd candlestick is uptrend
          (MathMax(O, C))<(L+(H-L)/3) && //Body is Lower 1/3 
          (MathMin(O,C)-L)<((MathMax(O,C)-MathMin(O,C))/5) && //shadowless bottom
          (C > C1) && (O > C1) && // Body Gap
          (H1 < H)) //Highest High

        {
           if(Display_Inverted_Hammer == true) 
            {   
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + Range*1.5);
               ObjectSetText(PatternText[shift], "Shooting Star", 10, "Times New Roman", Orange);
               downArrow[shift1] = High[shift1] + Range*0.5;
            }
           if(setalert == 0 && Show_Alert == true) 
             {
               pattern = "Shooting Star Pattern";
               setalert = 1;
             }
         }
     
       if(setalert == 1 && shift == 0) 
         {
           Alert(Symbol(), " ", period, " ", pattern);
           setalert = 0;
         }
     } // End of for loop
   return(0);
  }
//+------------------------------------------------------------------+-------------+
 
  1.    for(int j = 0; j < Bars; j++) 
         { 
           PatternText[j] = "pattern-" + j;
         }
    Don't use bar indexes in object names. When a new bar forms, you will be trying to create a new object with an existing name. Use time (int)Time[j].
  2. There is no purpose for PatternText[], drop it. If you have more than 5000 bars, you exceed the array. The contents are constant. Just create the name before you create the object.
  3. if(prevtime == Time[0]) 
         {
           return(0);
         }
       prevtime = Time[0]; 
    Drop that. NewBar code is for EAs. Indicators should update only changed bars, and do your lookback properly.
    int counted  = IndicatorCounted();
    int lookback = 9; // AvgRange
       for(shift = Bars - 1 - MathMax(lookback, counted); shift > 0; shift--)
    You probably don't want to process bar zero. It's not a shooting star until the candle is completely formed. Don't alert unless shift is 1.
  4. ObjectsDeleteAll(0, OBJ_TEXT);
    This makes your indicator incompatible with any other objects on the chart. Do it right.
    int  ObjectsDeleteAll(0, "pattern-", 0, OBJ_TEXT);

 

Hi Roeder, yes, it is working now. Thank you so much, mate!

 Now I know that I have so much to work on and learn from. 

The main issue is solved after I changed the lookback, as recommended by you.

 You're a savior, have a good day mate! 

Reason: