Critical :array out of range error

i have coded the indicator but it showing array out of range can anybody help me get out of it.


//+------------------------------------------------------------------+
//|                                                          bot.mq5 |
//|                        Copyright 2023, harish.trader99@gmail.com |
//|                                        harish.trader99@gmail.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_plots 2
#property indicator_buffers 2

//CREDITS to HPotter for the orginal code. The guy trying to sell this as his own is a scammer lol. 

input double KeyVaule = 3; // This changes the sensitivity
input int ATRPeriod = 10;

double nLoss, xATRTrailingStop, pos;
double xATR = iATR(Symbol(), PERIOD_CURRENT, ATRPeriod);

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[])
{
    ArraySetAsSeries(high, true);
    ArraySetAsSeries(low, true);
    ArraySetAsSeries(close, true);

    nLoss = KeyVaule * xATR;

    if (prev_calculated == 0)
    {
        xATRTrailingStop = 0;
        pos = 0;
    }

    for (int i = prev_calculated; i < rates_total; ++i)
    {
        xATRTrailingStop = (close[i] > xATRTrailingStop && close[i - 1] > xATRTrailingStop) ? MathMax(xATRTrailingStop, close[i] - nLoss) :
                           (close[i] < xATRTrailingStop && close[i - 1] < xATRTrailingStop) ? MathMin(xATRTrailingStop, close[i] + nLoss) :
                           (close[i] > xATRTrailingStop) ? close[i] - nLoss : close[i] + nLoss;

        if (close[i - 1] < xATRTrailingStop && close[i] > xATRTrailingStop)
            pos = 1;
        else if (close[i - 1] > xATRTrailingStop && close[i] < xATRTrailingStop)
            pos = -1;
        else
            pos = pos;

        color xcolor = pos == -1 ? clrRed : pos == 1 ? clrGreen : clrBlue;
        PlotIndexSetDouble(0,PLOT_EMPTY_VALUE, xATRTrailingStop);
        PlotIndexSetInteger(0, PLOT_ARROW, i, xcolor);

        bool buy = (close[i] > xATRTrailingStop) && (close[i - 1] <= xATRTrailingStop);
        bool sell = (close[i] < xATRTrailingStop) && (close[i - 1] >= xATRTrailingStop);
        bool barcolor = close[i] > xATRTrailingStop;

        if (buy)
            PlotIndexSetInteger(1, PLOT_ARROW, i, 233);
        if (sell)
            PlotIndexSetInteger(2, PLOT_ARROW, i, 234);

        if (barcolor)
            PlotIndexSetInteger(0, PLOT_LINE_COLOR, i, clrGreen);
        else
            PlotIndexSetInteger(0, PLOT_LINE_COLOR, i, clrRed);

        if (buy)
            Alert("UT BOT Buy");
        if (sell)
            Alert("UT BOT Sell");
    }
    return(rates_total);
}

If you get an error, it may have the line number in the code to show where it is failing.
Array out of range is a difficult error sometimes.
It means that you are asking for data that is outside the array itself.

Array values start at zero. So, an array of 100, will go from 0 to 99.
If you ask for data at ArrayExample[100] then this will be just outside.
If you ask for data at ArrayExample[-1] then this will also be outside the array.

Usually, in my experience, the array is just a bit over the highest value.

1 Like