Arrray out of range

To add comments, please log in or register
MrSeedan
32
MrSeedan  

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[];

      ArraySetAsSeries(Hoch,true);
      ArraySetAsSeries(Tief,true);

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

      //PreisInfoArray
      MqlRates PriceInformation[];
      ArraySetAsSeries(PriceInformation,true);
      int HighestCandle;
      int LowestCandle;
      HighestCandle=ArrayMaximum(Hoch,1,WHOLE_ARRAY);
      LowestCandle=ArrayMinimum(Tief,1,WHOLE_ARRAY);

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

      //Hochlinie zeichnen
      ObjectCreate(_Symbol,"Hochlinie",OBJ_HLINE,0,0,PriceInformation[HighestCandle].high);
      ObjectCreate(_Symbol,"Tieflinie",OBJ_HLINE,0,0,PriceInformation[LowestCandle].low);

      //Hochlinie einstellen
      ObjectSetInteger(0,"Hochlinie",OBJPROP_COLOR,InpColor);
      ObjectSetInteger(0,"Hochlinie",OBJPROP_WIDTH,InpWidth);
      ObjectSetInteger(0,"Hochlinie",OBJPROP_STYLE,InpStyle);

      //Tieflinie einstellen
      ObjectSetInteger(0,"Tieflinie",OBJPROP_COLOR,InpColor);
      ObjectSetInteger(0,"Tieflinie",OBJPROP_WIDTH,InpWidth);
      ObjectSetInteger(0,"Tieflinie",OBJPROP_STYLE,InpStyle);
     
      ObjectMove(0,"Hochlinie",0,0,PriceInformation[HighestCandle].high);
      ObjectMove(0,"Tieflinie",0,0,PriceInformation[LowestCandle].low);
      ChartRedraw(0);
     
     

}
     
      //+------------------------------------------------------------------+
kypa
980
kypa  

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.
MrSeedan
32
MrSeedan  
kypa:

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?
kypa
980
kypa  

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.

nicholi shen
1880
nicholi shen  

Hi MrSeeden, 


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);   
   high_line.Color(InpColor);
   high_line.Style(InpStyle);
   high_line.Width(InpWidth);
   low_line.Create(0, "low", 0, 0.0);   
   low_line.Color(InpColor);
   low_line.Style(InpStyle);
   low_line.Width(InpWidth);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
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)]);
}
MrSeedan
32
MrSeedan  
nicholi shen:

Hi MrSeeden, 


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.


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.
To add comments, please log in or register