Got Critical Error While Running Expert, Array Out Of Range

 

Hi guys, I am trying to develop an EA based on few indicator But When I attached to a chart got this massage "Critical Error While Running Expert, Array Out Of Range"

Please help me to fix this error thank you in advance.  Here is my code:


//+------------------------------------------------------------------+
//|                            EMA_BB_AO_EA.mq4                      |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict

// Input parameters
input int EMA_Fast_Period = 9; // Fast EMA period
input int EMA_Slow_Period = 200; // Slow EMA period
input int BB_Period = 20; // Bollinger Bands period
input double BB_Deviation = 2.0; // Bollinger Bands deviation
input int AO_Period = 34; // Awesome Oscillator period
input double Lots = 0.1; // Lot size
input int StopLoss = 100; // Stop loss in points
input int TakeProfit = 200; // Take profit in points
input int MagicNumber = 12345; // Magic number

// Global variables
double EMA_Fast[];
double EMA_Slow[];
double BB_Upper[];
double BB_Lower[];
double AO[];

int OnInit() {
  // Initialize indicators
  SetIndexBuffer(0, EMA_Fast);
  SetIndexBuffer(1, EMA_Slow);
  SetIndexBuffer(2, BB_Upper);
  SetIndexBuffer(3, BB_Lower);
  SetIndexBuffer(4, AO);

  IndicatorSetString(INDICATOR_SHORTNAME, "EMA_BB_AO_EA");

  return (INIT_SUCCEEDED);
}

void OnTick() {
  // Calculate indicators
  int counted_bars = IndicatorCounted();
  int limit = Bars-counted_bars;
  int    ticket,total;
  for (int i = limit; i >= 0; i--) {
    EMA_Fast[i] = iMA(NULL, 0, EMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE, i);
    EMA_Slow[i] = iMA(NULL, 0, EMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE, i);
    BB_Upper[i] = iBands(NULL, 0, BB_Period, BB_Deviation, 0, PRICE_CLOSE, MODE_UPPER, i);
    BB_Lower[i] = iBands(NULL, 0, BB_Period, BB_Deviation, 0, PRICE_CLOSE, MODE_LOWER, i);
    AO[i] = iAO(NULL, 0, AO_Period);
  }
total=OrdersTotal();
   if(total<1)
     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
        }
  // Check trading conditions
  if (EMA_Fast[1] > EMA_Slow[1] && EMA_Fast[2] < EMA_Slow[2]) // Bullish crossover
  {
    if (High[1] > BB_Upper[1]) // Price touches upper band
    {
      if (AO[1] > AO[2]) // AO is rising
      {
        // Buy signal
       ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss * Point(), Ask + TakeProfit * Point(), "Buy", MagicNumber);
       if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
      }
    }
  }

  if (EMA_Fast[1] < EMA_Slow[1] && EMA_Fast[2] > EMA_Slow[2]) // Bearish crossover
  {
    if (Low[1] < BB_Lower[1]) // Price touches lower band
    {
      if (AO[1] < AO[2]) // AO is falling
      {
        // Sell signal
        ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss * Point(), Bid - TakeProfit * Point(), "Sell", MagicNumber);
        if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());
      }
    }
  }

  return;
}
 

Where? Look at the error message again in the log and see which line and character position is reported for the error.

 

Please don't post randomly in any section. MT4/mql4 has it's own section on the forum.

I have moved your topic to the correct section, so please don't create another topic.

 
Rajibul Hasan: Hi guys, I am trying to develop an EA based on few indicator But When I attached to a chart got this massage "Critical Error While Running Expert, Array Out Of Range" Please help me to fix this error thank you in advance.  Here is my code:

You are mixing Indicator code with EA code.

Is this an Indicator or an EA?

 
  1. void OnTick() {

    Only EAs use OnTick, yet you try to have buffers which are only in indicators.

  2.   int counted_bars = IndicatorCounted();

    Only indicators can use that, yet you have EA code.

  3.     AO[i] = iAO(NULL, 0, AO_Period);

    Invalid MT4 call. MT5 code.

  4.         ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss * Point(), Bid - TakeProfit * Point(), "Sell”, MagicNumber);

    MT4 EA only calls.

Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

Do not post code that will not even compile. You didn't get your “Critical Error” with that posted code.

 
Fernando Carreiro #:

Where? Look at the error message again in the log and see which line and character position is reported for the error.

for (int i = limit; i >= 0; i--) {
    EMA_Fast[i] = iMA(NULL, 0, EMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE, i);
    EMA_Slow[i] = iMA(NULL, 0, EMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE, i);
    BB_Upper[i] = iBands(NULL, 0, BB_Period, BB_Deviation, 0, PRICE_CLOSE, MODE_UPPER, i);
    BB_Lower[i] = iBands(NULL, 0, BB_Period, BB_Deviation, 0, PRICE_CLOSE, MODE_LOWER, i);
    AO[i] = iAO(NULL, 0, AO_Period);
  }

Thank You. I got the error here

 
William Roeder #:
  1. Only EAs use OnTick, yet you try to have buffers which are only in indicators.

  2. Only indicators can use that, yet you have EA code.

  3. Invalid MT4 call. MT5 code.

  4. MT4 EA only calls.

Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

Do not post code that will not even compile. You didn't get your “Critical Error” with that posted code.

I understand thank you for your reply.

Reason: