While I load the EA I get Array out of range error (NEED Help Trouble Shoot) New to MQL5

 
//+------------------------------------------------------------------+
//|                                                       VolumeEA.mq5 |
//|                         Copyright 2023, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>

input int calculationBars = 10;
input int labelXDistance = 10;
input int labelYDistance = 10;
input double volumeThreshold = 60.0;

double GreenBuffer[];
double RedBuffer[];

enum TradeDirection
{
   NoTrade,
   Buy,
   Sell
};

TradeDirection tradeDirection = NoTrade;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0, GreenBuffer);
   SetIndexBuffer(1, RedBuffer);
   
   CreateLabel();
   
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
{
   int rates_total = iBars(Symbol(), PERIOD_CURRENT);
   int limit = rates_total - calculationBars;
   
   for(int i = limit; i >= 0; i--)
   {
      calculateVolumePercentages(i);
      updateLabel(i);
   }
   
   checkTradingConditions();
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void calculateVolumePercentages(int index)
{
   double green_volume = 0.0;
   double red_volume = 0.0;
   bool is_volume_zero = true;

   for(int i = 0; i < calculationBars; i++)
   {
      double open = iOpen(Symbol(), PERIOD_CURRENT, index + i);
      double close = iClose(Symbol(), PERIOD_CURRENT, index + i);
      double volume = (double)iVolume(Symbol(), PERIOD_CURRENT, index + i);

      if(volume != 0)
      {
         is_volume_zero = false;
      }
      
      if(close > open)
      {
         green_volume += volume;
      }
      else
      {
         red_volume += volume;
      }
   }

   if(is_volume_zero)
   {
      Print("Volume data is zero for all ", calculationBars, " bars starting from bar ", index);
   }

   double total_volume = green_volume + red_volume;
   if(total_volume != 0)
   {
      GreenBuffer[index] = (green_volume / total_volume) * 100.0;
      RedBuffer[index] = (red_volume / total_volume) * 100.0;
   }
   else
   {
      GreenBuffer[index] = 0;
      RedBuffer[index] = 0;
   }
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CreateLabel()
{
   ObjectCreate(0, "Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetInteger(0, "Label", OBJPROP_CORNER, 0);
   ObjectSetInteger(0, "Label", OBJPROP_XDISTANCE, labelXDistance);
   ObjectSetInteger(0, "Label", OBJPROP_YDISTANCE, labelYDistance);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void updateLabel(int index)
{
   string symbol = Symbol();
   datetime time = iTime(Symbol(), PERIOD_CURRENT, index);
   string time_str = TimeToString(time, TIME_DATE | TIME_MINUTES);

   string labelText = "Buy: " + DoubleToString(GreenBuffer[index], 2) + "%. Sell: " + DoubleToString(RedBuffer[index], 2) + "%";
   int labelHandle = ObjectCreate(0, "Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetInteger(0, "Label", OBJPROP_CORNER, 0);
   ObjectSetInteger(0, "Label", OBJPROP_XDISTANCE, labelXDistance);
   ObjectSetInteger(0, "Label", OBJPROP_YDISTANCE, labelYDistance);
   ObjectSetInteger(0, "Label", OBJPROP_COLOR, clrLime);
   ObjectSetString(0, "Label", OBJPROP_FONT, "Arial");
   ObjectSetInteger(0, "Label", OBJPROP_FONTSIZE, 9);
   ObjectSetString(0, "Label", OBJPROP_TEXT, labelText);
   
   Print("Volume for ", symbol, " at ", time_str, " is : Buy: ", DoubleToString(GreenBuffer[index], 2), "%. Sell: ", DoubleToString(RedBuffer[index], 2), "%.");
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void checkTradingConditions()
{
   int lastBar = iBars(Symbol(), PERIOD_CURRENT) - 1;
   
   if (lastBar < 0 || lastBar >= calculationBars)
   {
      Print("Not enough data in buffers for current bar.");
      return;
   }
   
   double buyPercentage = GreenBuffer[lastBar];
   double sellPercentage = RedBuffer[lastBar];
   
   // Close existing trade if the opposite signal is generated
   if ((tradeDirection == Buy && sellPercentage >= volumeThreshold) || (tradeDirection == Sell && buyPercentage >= volumeThreshold))
   {
      CloseTrade();
   }
   
   if (buyPercentage >= volumeThreshold)
   {
      // Execute a Buy trade
      if (tradeDirection != Buy)
      {
         CloseTrade();
         tradeDirection = Buy;
         Print("Executing Buy trade as Buy Volume is ", DoubleToString(buyPercentage, 2), "%.");
         // Place your Buy trade execution code here
      }
   }
   else if (sellPercentage >= volumeThreshold)
   {
      // Execute a Sell trade
      if (tradeDirection != Sell)
      {
         CloseTrade();
         tradeDirection = Sell;
         Print("Executing Sell trade as Sell Volume is ", DoubleToString(sellPercentage, 2), "%.");
         // Place your Sell trade execution code here
      }
   }
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseTrade()
{
   tradeDirection = NoTrade;
   // Place your trade closing code here if you have open trades
}

I get this error while backtesting the EA 2023.07.27 13:52:46.704 VolumeEA (USDCHF,M5) array out of range in 'VolumeEA.mq5' (94,19)  Please can anyone help whats wrong with the code ?


 
  1.    SetIndexBuffer(0, GreenBuffer);
       SetIndexBuffer(1, RedBuffer);

    How many plots and buffers did you declare your indicator uses?


  2. #include <Trade\Trade.mqh>

    Indicators can not trade. EAs do not have buffers.

 
William Roeder #:
  1. How many plots and buffers did you declare your indicator uses?


  2. Indicators can not trade. EAs do not have buffers.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrGreen
#property indicator_color2 clrRed

double GreenBuffer[];
double RedBuffer[];

int OnInit()
{
   SetIndexBuffer(0, GreenBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, RedBuffer, INDICATOR_DATA);
   
   IndicatorSetString(INDICATOR_SHORTNAME, "Buy Sell Volume %");
   
   CreateLabel();
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0, "Label");
}

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[])
{
   for(int i = prev_calculated; i < rates_total; i++) {
      calculateVolumePercentages(i, open, close, tick_volume);
      updateLabel(i, time);
   }
   return(rates_total);
}

void calculateVolumePercentages(int index, const double &open[], const double &close[], const long &volume[])
{
   if(index < 10) return;

   double green_volume = 0.0;
   double red_volume = 0.0;
   bool is_volume_zero = true;

   for(int i = 0; i < 10; i++) {
      if(volume[index - i] != 0)
      {
         is_volume_zero = false;
      }
      if(close[index - i] > open[index - i]) {
         green_volume += (double)volume[index - i];
      } else {
         red_volume += (double)volume[index - i];
      }
   }

   if(is_volume_zero) {
      Print("Volume data is zero for all 10 bars.");
   }

   double total_volume = green_volume + red_volume;
   if(total_volume != 0) {
      GreenBuffer[index] = (green_volume / total_volume) * 100.0;
      RedBuffer[index] = (red_volume / total_volume) * 100.0;
   } else {
      GreenBuffer[index] = 0;
      RedBuffer[index] = 0;
   }
}

void CreateLabel() {
   ObjectCreate(0, "Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetInteger(0, "Label", OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetInteger(0, "Label", OBJPROP_XDISTANCE, 600);
   ObjectSetInteger(0, "Label", OBJPROP_YDISTANCE, 10);
}

void updateLabel(int index, const datetime &time[]) {
   if(index < 10) return;

   string symbol = Symbol();
   string time_str = TimeToString(time[index], TIME_DATE|TIME_MINUTES);

   ObjectSetString(0, "Label", OBJPROP_TEXT, "Buy: " + DoubleToString(GreenBuffer[index], 2) + "%. Sell: " + DoubleToString(RedBuffer[index], 2) + "%");
   
   Print("Volume for " + symbol + " at " + time_str + " is : Buy: " + DoubleToString(GreenBuffer[index], 2) + "%. Sell: " + DoubleToString(RedBuffer[index], 2) + "%.");
}
This is my Indicator code on basis of it I am trying to make a EA which will buy and sell if either one of the volume percentage is 60% or above