array out of range error - Creating an object using the indicator

 
Hi
I created an indicator that, when executed, draws a Fibonacci tool on the chart. With the help of a button, the display of Fibonacci lines is turned on and off. The problem is that I get the following error at the beginning of the program execution. But by changing the time frame, the indicator is executed correctly.
This indicator was created on the mt5 platform.

This text has been translated with the help of Google Translate, so I hope you understand what I mean.



array out of range in 'BH_Object.mqh' (316,52)

This error occurs due to the execution of the following code. As you can see, I also used the do while loop to ensure that this command is executed correctly, but the situation got worse. That means the whole program failed.


  tempVal = CopyTime(_Symbol, PERIOD_D1, 0, 2, tm);


main code :


#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <BH_include\BH_Object.mqh>
#include <BH_include\MY_definitions.mqh>

bool flag = true ;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+


int OnInit()
  {
  // SetIndexBuffer(0,ExtTenkanBuffer,INDICATOR_DATA);
   if(ObjectFind(0, "Button1h") < 0)
     {
      if(ButtonCreate(0, "Button1h", 0, 50, 130, 70, 18, CORNER_LEFT_UPPER, "Button1h") == false)
        {
         Print("button h1 not created");
        }
     }

   if(ObjectFind(0, "Button4h") < 0)
     {
      if(ButtonCreate(0, "Button4h", 0, 50, 200, 70, 18, CORNER_LEFT_UPPER, "Button4h") == false)
        {
         Print("button h4 not created");
        }
     }


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {

   if(prev_calculated == rates_total  && flag == true )
     {
      DrawingFibo(0, DOWN_FIBONAMEH1, PERIOD_H1, true);
      DrawingFibo(0, UP_FIBONAMEH1, PERIOD_H1, true);
      DrawingFibo(0, DOWN_FIBONAMEH4, PERIOD_H4, false);
      DrawingFibo(0, UP_FIBONAMEH4, PERIOD_H4, false);
      flag = false ;

     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long & lparam,
                  const double & dparam,
                  const string & sparam)
  {

   if(id == CHARTEVENT_OBJECT_CLICK && sparam == "Button1h")

     {
      if(ObjectGetInteger(0, "Button1h", OBJPROP_STATE) == 1)
        {
         ChangeFiboColor(DOWN_FIBONAMEH1, true);
         ChangeFiboColor(UP_FIBONAMEH1, true);
         Print("OBJPROP_STATE 1H is  ");
        }
      else
         if(ObjectGetInteger(0, "Button1h", OBJPROP_STATE) == 0)
           {
            ChangeFiboColor(DOWN_FIBONAMEH1, false);
            ChangeFiboColor(UP_FIBONAMEH1, false);
           }
     }
   if(id == CHARTEVENT_OBJECT_CLICK && sparam == "Button4h")

     {
      if(ObjectGetInteger(0, "Button4h", OBJPROP_STATE) == 1)
        {
         ChangeFiboColor(DOWN_FIBONAMEH4, true);
         ChangeFiboColor(UP_FIBONAMEH4, true);
         Print("OBJPROP_STATE 4H is  ");
        }
      else
         if(ObjectGetInteger(0, "Button4h", OBJPROP_STATE) == 0)
           {
            ChangeFiboColor(DOWN_FIBONAMEH4, false);
            ChangeFiboColor(UP_FIBONAMEH4, false);
           }
     }

  }

If you have a better idea to implement this program, please send your comment. 

thank you for your time.