Array out of range

Hello. I would like my expert advisor to create 2 lines, where the high and low of a certain timeframe (10:00 until 11:00) is. When compiling my code doesnt not seem to have an error, but it stops working while backtesting because array is out of range (47,70) (there is only a comment in line 47). Thank you for your help.

input color           InpColor=clrRed;     // Linienfarbe
input ENUM_LINE_STYLE InpStyle=STYLE_DASH; // Linienstil
input int             InpWidth=2;          // Linienbreite
input int             Periode=PERIOD_M5;

//Eingabe der Zeiten für die ORB
input datetime Anfangszeit =D'10:00';
input datetime Endzeit =D'11:00';
//|                                                                  |
void OnTick()
   datetime RangeTimeBegin=StringToTime(Anfangszeit);
   datetime RangeTimeEnde=StringToTime(Endzeit);
datetime timelocal= TimeLocal();
string hoursandminuteslocal=TimeToString(timelocal,TIME_MINUTES);

      int AnzahlBarsBeginn=Bars(_Symbol,_Period,RangeTimeBegin,RangeTimeEnde);
      // Hoch und Tief Array
      double Hoch[];
      double Tief[];


      CopyHigh(_Symbol,_Period,Anfangszeit, Endzeit,Hoch);

      MqlRates PriceInformation[];
      int HighestCandle;
      int LowestCandle;

      int Data=CopyRates(Symbol(),Period(),Anfangszeit, Endzeit,PriceInformation);

      //Hochlinie zeichnen

      //Hochlinie einstellen

      //Tieflinie einstellen


It will try to create a line on every tick which will definitely be problematic.

if(ObjectFind()<0) ObjectCreate() and all the parameter setting.

Also ObjectCreate(0,…, not ObjectCreate(_Symbol,…

MqlDateTime has hours and minutes in it.

Thank you for your help. I changed a little bit and it seems that the problem is caused by the times used for CopyHigh and CopyLow. When I use certain start position and a number to count the problem is resolved, but the line changes with the high of the last bars. How can I use the hour and minutes of MqlDateTime in the Copy High/Low Function so the ea will only copy the highs of this timeframe?

You must get TimeLocal, turn it to MqlDateTime, set the hour to 10/11 then turn it to datetime. It’s actually way more complicated than your solution, I don’t know why I’ve mentioned it.

For CopyHigh/Low I think arrays must be resized manually, especially if the copying is by time, not by index. But you don’t really need those since you have CopyRates later (whose array’s size should probably also be fixed).

Return of CopyRates should be checked as it might not copy the rates on first try.

Hi @hmarkos2
There are several issues with your code. It appears that you’ve tried several things and now they’re unsuccessfully intermingling. Sometimes it’s better to scrap and start over. Like kypa mentioned, you can only call objectcreate once per “name”. Create your objects once, and then update the price only. Removing all the redundant and unnecessary code your project would look something like this.

input color           InpColor=clrRed;     // Linienfarbe
input ENUM_LINE_STYLE InpStyle=STYLE_DASH; // Linienstil
input int             InpWidth=2;          // Linienbreite
input int             Periode=PERIOD_M5;

//Eingabe der Zeiten für die ORB
input datetime anfangszeit = D'1:00';
input datetime endzeit     = D'11:00';

#include <ChartObjects\ChartObjectsLines.mqh>
CChartObjectHLine high_line, low_line;

int OnInit()
   high_line.Create(0, "high", 0, 0.0);   
   low_line.Create(0, "low", 0, 0.0);   
void OnTick()
   double hoch[], tief[];
   CopyHigh(_Symbol, _Period, anfangszeit, endzeit, hoch);
   CopyLow (_Symbol, _Period, anfangszeit, endzeit, tief);
   high_line.Price(0, hoch[ArrayMaximum(hoch)]);
   low_line.Price(0, tief[ArrayMinimum(tief)]);

Thank you for your help. Im a beginner with MQL 5, so i am still really bad at what i am doing :D. Unfortunately the code you posted still has the Array out of Range error. If i add ArrayResize(high,1000) for example the ea creates a line connecting every high and low of a bar. It seems that the CopyHigh/Low function totally ignores the timeframe given by anfangszeit und endzeit.