Array out of range.

 

Hello. I am attempting to make an EA using RSI and candle patterns. When I try to backtest it with real ticks, I get the tick mismatch errors like I usually do lately. Then when I use it on the simulated 'every tick' option I get an 'array out of size' error and OnTick() critical error

Here is the code. When I debugged it it seemed like the error at something to do with the rsi array. Thanks in advance for the help.

//--- input parameters
input int      bullishbarthreshold=3;
input int      lotsize=1;
input int      rsiperiod=14;
input int      oversold=30;
input int      overbought=70;

#include <Trade\Trade.mqh>

CTrade tradecontrol;
MqlRates pricedata[];
double rsi[], closeprice, openprice;
int rsicontrol, pricedatapoints, rsidata, pricearraysize, candlestocopy, shift, bearishbars;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   ArraySetAsSeries(pricedata, true);
   ArraySetAsSeries(rsi, true);

   rsicontrol = iRSI(_Symbol, PERIOD_CURRENT, rsiperiod, MODE_CLOSE);


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

   IndicatorRelease(rsicontrol);

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   pricedatapoints = CopyRates(_Symbol, 0, 0, candlestocopy, pricedata);
   candlestocopy = 10;
   rsidata = CopyBuffer(rsicontrol, 0, 0, 3, rsi);
   closeprice = pricedata[shift].close;
   openprice = pricedata[shift].open;

   for(shift = 2; closeprice < openprice; shift++)
     {
      bearishbars++;
      if(bearishbars >= candlestocopy)
        {
         candlestocopy = bearishbars;
        }
     }

   bool previousbarbullish = pricedata[1].close > pricedata[1].open;
   bool rsioversold = rsi[1] >= oversold && rsi[0] < oversold;

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

  }
//+------------------------------------------------------------------+
//| Entry and Exit Functions                                         |
//+------------------------------------------------------------------+

void longentry()
  {

   if(PositionSelect(_Symbol) == false)
     {
      tradecontrol.Buy(lotsize, _Symbol, PRICE_OPEN, 0, 0, "Buy Trade. Magic Number #" + (string) tradecontrol.RequestMagic());

      if(tradecontrol.ResultRetcode()==10008 || tradecontrol.ResultRetcode()==10009) //Request is completed or order placed
        {
         Print("Entry rules: A Buy order has been successfully placed with Ticket#: ", tradecontrol.ResultOrder());
        }
      else
        {
         Print("Entry rules: The Buy order request could not be completed. Error: ", GetLastError());
         ResetLastError();
         return;
        }
     }
  }

void longexit()
  {

   if(PositionSelect(_Symbol) == true)
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         tradecontrol.PositionClose(_Symbol);
         if(tradecontrol.ResultRetcode()==TRADE_RETCODE_PLACED || tradecontrol.ResultRetcode()==TRADE_RETCODE_DONE) //Request is completed or order placed
           {
            Print("Exit rules: A close order has been successfully placed with Ticket#: ",tradecontrol.ResultOrder());
           }
         else
           {
            Print("Exit rules: The close order request could not be completed.Error: ",GetLastError());
            ResetLastError();
            return;
           }
        }
     }

  }
 
  1. fjgwey: When I debugged it it seemed like the error at something to do with the rsi array. 
    Your error message told you what line, not what “seemed like.”
  2. int rsicontrol, pricedatapoints, rsidata, pricearraysize, candlestocopy, shift, bearishbars;
    ⋮
       pricedatapoints = CopyRates(_Symbol, 0, 0, candlestocopy, pricedata);
       ⋮
       closeprice = pricedata[shift].close;
    
    Your variable was uninitialized (random value including zero), you copied nothing, array exceeded.
 
William Roeder #:
  1. Your error message told you what line, not what “seemed like.”
  2. Your variable was uninitialized (random value including zero), you copied nothing, array exceeded.
I see. Thanks, I made a mistake in ordering the lines. I just debugged it and got no errors.  Still got the real tick mismatch stuff but at least my code has no issues.