Array out of size and OnTick Critical Error.

 

Hello. I am practicing coding an EA that uses Heikin Ashi data (only close and open since that's all I need right now) to determine trades. When I backtest it I get an array out of size and ontick critical error, and debugging it points me to the pricedatapoints variable. The last time I had an issue like this I messed up the ordering of lines but it seems like I got it right this time.

I set up a loop to look from shift 2 and beyond for bearish Heikin Ashi bars and count them, as well as nesting an if statement in it to adjust the pricedata array data count (candlestocopy) if the loop counts more or less bearish bars than the size. This is the relevant part of the code. I'm not exactly sure what the issue is, but I am a beginner so I'd appreciate any help, thanks.

#include <Trade\Trade.mqh>

//pre-empting a bunch of variables that will be defined later
CTrade tradecontrol;
MqlRates pricedata[];
double ha_close[], ha_open[], previoushaclose, previoushaopen;
double rsi[], closeprice, openprice;
int rsicontrol, pricedatapoints, rsidata, pricearraysize, candlestocopy, shift, bearishbars;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   //Set up different arrays for time series data
   ArraySetAsSeries(pricedata, true);
   ArraySetAsSeries(rsi, true);
   ArraySetAsSeries(ha_close, true);
   ArraySetAsSeries(ha_open, true);
   
   //initial variable values for shift and the lookback candles
   candlestocopy = 10;
   shift = 2;
     
   //defining RSI   
   rsicontrol = iRSI(_Symbol, PERIOD_CURRENT, rsiperiod, MODE_CLOSE);


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
   //release RSI from memory once EA is deactivated
   IndicatorRelease(rsicontrol);

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {  
      
   //copying price and rsi data for time series
   pricedatapoints = CopyRates(_Symbol, 0, 0, candlestocopy, pricedata);
   rsidata = CopyBuffer(rsicontrol, 0, 0, 3, rsi);
   
   //defining current close and open price
   closeprice = pricedata[0].close;
   openprice = pricedata[0].open; 
   
   //Defining previous Heikin Ashi close and open price
   previoushaclose = (pricedata[1].open + pricedata[1].close + pricedata[1].high + pricedata[1].low) / 4;
   previoushaopen = (pricedata[2].open + pricedata[2].close) / 2;
   
   //Calculating past Heikin Ashi close and open price starting from shift 2 based on actual price data     
   ha_close[shift] = (pricedata[shift].open + pricedata[shift].close + pricedata[shift].high + pricedata[shift].low) / 4;
   ha_open[shift] = (pricedata[shift + 1].open + pricedata[shift + 1].close) / 2;
   
   //Count how many consecutive bearish Heikin Ashi bars there are starting from shift 2, and dynamically adjust price data array size if needed
   while(ha_close[shift] < ha_open[shift])
     {
      bearishbars++;
      if(bearishbars > candlestocopy)
        {
         candlestocopy = bearishbars;
        }
      if(bearishbars < candlestocopy)
        {
         candlestocopy = bearishbars;
        }
      shift++;
     }
   
   //defining conditions for entry and exit: if there's a bullish candle after consecutive bearish candles then enter, 
   //if RSI crosses under oversold level then exit
   bool previousbarbullish = previoushaclose > previoushaopen;
   bool bullishcondition = bearishbars >= bearishbarthreshold && previousbarbullish == true; 
   bool rsioversold = rsi[1] >= oversold && rsi[0] < oversold;

   if(bullishcondition == true)
      longentry();
   if(rsioversold == true)
      longexit();

  }
 
  1. You should encapsulate your iCustom calls to make your code self-documenting.
              take candle color hekin ashi - MQL4 and MetaTrader 4 #8-9 or #1 (2018)

  2. double ha_close[], ha_open[], …
    ⋮
       ArraySetAsSeries(ha_close, true);
    ⋮
       ha_close[shift] = (pricedata[shift].open + pricedata[shift].close + pricedata[shift].high + pricedata[shift].low) / 4;

    Your array has zero size; of course, you get array exceeded.

  3.    candlestocopy = 10;
       shift = 2;
    ⋮
       pricedatapoints = CopyRates(_Symbol, 0, 0, candlestocopy, pricedata);
    ⋮
       while(ha_close[shift] < ha_open[shift])
         {
          ⋮
          shift++;
    
    You start shift at two; each tick you increase it in the while.
 
William Roeder #:
  1. You should encapsulate your iCustom calls to make your code self-documenting.
              take candle color hekin ashi - MQL4 and MetaTrader 4 #8-9 or #1 (2018)

  2. Your array has zero size; of course, you get array exceeded.

  3. You start shift at two; each tick you increase it in the while.

I'm not super knowledgeable in MQL5 yet so can you explain more as to what you mean by these things and what I should do?

So you mean I should make it a function instead? Your links give me an idea but it's MQL4 so it's a little confusing. I haven't learned much in the way of OOP.

As for point 3: I'm not sure what you mean here, because I want it to calculate only the bars that I want, so I figured making the shift++ conditional would be fine. 

Thanks for the help so far.

 
fjgwey #: As for point 3: I'm not sure what you mean here, because I want it to calculate only the bars that I want, so I figured making the shift++ conditional would be fine.
Shift never goes down. How can that possibly be fine?
 
William Roeder #:
Shift never goes down. How can that possibly be fine?

Ah. I see now. 

I changed it to this, should this be alright? The error still persists, however, though I didn't expect this alone to fix it.

while(ha_close[shift] < ha_open[shift])
     {
      bearishbars++;
      if(bearishbars > candlestocopy || bearishbars < candlestocopy)
        {
         candlestocopy = bearishbars;
        }
      shift++;
      if(ha_close[shift] >= ha_open[shift]) shift = 2;         
     }  
Reason: