Critical Error in EA (MT4) - Array out of range

 

Hi guys,

Im writing this EA, checked forum and article topics, but still get this nasty error. Can  you help me? 

if(incremental_trail==true )
 {
 for(int i=0;i<OrdersTotal();i++ )
  {
  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  if(OrderMagicNumber()==magic)
   {
   if(OrderType()==OP_BUY && newtrade==true) 
    {
incB++;
ArrayResize(tktB,incB);
ArrayResize(stoplossB,incB);
ArrayResize(openB,incB);
OrderSelect(incB,SELECT_BY_POS,MODE_TRADES);
tktB[incB]=OrderTicket();
stoplossB[incB]=OrderStopLoss();
openB[incB]=OrderOpenPrice();
//-----------------
if(incB>1 && stoplossB[incB]!=stoplossB[incB-1])
 {
  bool modify = OrderModify(tktB[incB-1],OrderOpenPrice(),NormalizeDouble(stoplossB[incB],Digits),OrderTakeProfit(),0,clrYellow);
  if(modify==true)Print("Incremental trail !!");return;
 }
 if(stoplossB[incB]==openB[incB]+be_size*point && incB>1 && stoplossB[incB-1]!=openB[incB]+be_size*point )
     {
  bool modify = OrderModify(tktB[incB-1],OrderOpenPrice(),NormalizeDouble(stoplossB[incB],Digits),OrderTakeProfit(),0,clrYellow);
  if(modify==true)Print("Incremental trail 2 !!"); return;
     } 
    }
if(OrderType()==OP_SELL && newtrade==true)
    {
incS++;
//---------
ArrayResize(tktS,incS);
ArrayResize(stoplossS,incS);
ArrayResize(openS,incS);
OrderSelect(incS,SELECT_BY_POS,MODE_TRADES);
tktS[incS]=OrderTicket();
stoplossS[incS]=OrderStopLoss();
openS[incS]=OrderOpenPrice();
//---------
if(incS>1 && stoplossS[incS]!=stoplossS[incS-1])
 {
   bool modify = OrderModify(tktS[incS-1],OrderOpenPrice(),NormalizeDouble(stoplossS[incS],Digits),OrderTakeProfit(),0,clrYellow);
  if(modify==true)Print("Incremental trail !!"); return;
 }
 if(stoplossS[incS]==openS[incS]-be_size*point && incS>1 && stoplossS[incS-1]!=openS[incS]-be_size*point)
     {
  bool modify = OrderModify(tktS[incS-1],OrderOpenPrice(),NormalizeDouble(stoplossS[incS],Digits),OrderTakeProfit(),0,clrYellow);
  if(modify==true)Print("Incremental trail 2 !!"); return;
     } 
    }
   }
  }
 }
}
 
Stanislav Ivanov: still get this nasty error. Can  you help me?
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. ArrayResize(tktB,incB);
    ArrayResize(stoplossB,incB);
    ArrayResize(openB,incB);
    OrderSelect(incB,SELECT_BY_POS,MODE_TRADES);
    tktB[incB]=OrderTicket();
    stoplossB[incB]=OrderStopLoss();
    openB[incB]=OrderOpenPrice();
    You size your arrays to incB and then try to store into a non-existent index. If the size is incB the element indexs are [0 .. incB-1]

  3. You already have an order selected, why are you selecting a different one?

  4. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

 
Thank you very much It did work and trades ! 
 
Stanislav Ivanov:
Thank you very much It did work and trades ! 

Here are a few tips:

You don't need to compare a bool to a bool because it's redundant. 

if(my_bool == true)
/// is the same as 
if(true == true) //redundant expression
///you would just say 
if(true)
///therefore you can simplify your expressions by just using...
if(my_bool)
///or
if(!my_bool)


You're completely ignoring the OrderSelect function's return when you should only be calling order-functions when it returns true. Also, you're repeating your code when you can simplify the expression in order to avoid over-nesting your expressions. Here's an example of how to fix:

if (incremental_trail)
    for (int i = 0; i < OrdersTotal(); i++)
        if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber()==magic && newtrade)
            if (OrderType() == OP_BUY)

 

You don't need to manually manage the size of dynamic arrays because there are collections that automatically do so in the standard library. 

Example:

 

#include<Arrays\ArrayInt.mqh>
void print_tickets()
{
    CArrayInt tickets;
    for(int i=OrdersTotal()-1;i>=0;i--)
       if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol() == _Symbol)
          tickets.Add(OrderTicket());
       
    for(int i=0;i < tickets.Total();i++)
        Print(tickets[i]);
}


Finally, the return statements in your code block don't make much sense and may cause you to have inconsistent results. You may want to reconsider your logic regarding those. 

Reason: