Array out range error

 

I have this type of error (Array out range error) when I backtesting this EA. How can I identify the point of issue?

Thanks

Giovanni


// Variabili globali


bool check = true;

extern string ParametriValori = "Inserimento volume degli ordini";

extern double volume =   1;

extern string ParametriCandele = "Inserimento parametri candele di set-up";

extern string AltezzaCandele = "Inserimento range altezza candele";
extern double rangeAltezzaCandele =            20;            // in %

extern string OmbreCandele =   "Inserimento range ombre candele";
extern double rangeOmbreCandele =              20;             // in %

extern string AltezzaCandelePrecedenti = "Inserimento range altezza candele precedenti";
extern double rangeAltezzaCandelePrecedenti =  15;            // in %


extern int    candelePrecedenti =              7;             // candele precedenti da considerare
extern int    numeroCandeleRSI =               7;

extern int cancellazioneScadenza =   1800;     // cancellazione a 30 minuti

extern string TrailingStops = "TrailingStops";
extern bool   filterTS =    true;

extern string opzione3 = "rischio";
extern int    rischio =  3;         // %


int ticket1,ticket2,ticket3;
double TPticket1;
double rsi[];
datetime orarioRiferimento = 0;
double minCandelaChiusuraTicket2;
datetime oldTime;

  

int OnInit()
{
   
   oldTime = Time[0];
   
   check = true;
   
   ticket1 = ticket2 = ticket3 = 0;
   
     
   if (IsTesting()) {
   
      Print ("Testing mode!");   
   
   }


   return(INIT_SUCCEEDED);
}


void OnDeinit(const int reason)
{

 Print("Fine sessione. Ciao, Ciao!");

}


int start(){
          
          if (condizione1() && condizione2() && oldTime != Time[0] && check){            
                        
          oldTime = Time[0];
          
          esecuzioneStrategia();
          
          Print( " #3 ordini creati " );
                
          check = false;  
              
          }          
          
  
     if (!check) {     // revisione degli ordini                           
     
        if (OrdersTotal() > 0) controlloOrdini();
     
           else {     // non ci sono ordini in piattaforma
          
              check = true;
                       
           }
     
        }
        
     return(0);  
}


void esecuzioneStrategia(){

   int CandelaLow = iLowest(NULL,0,MODE_LOW,2,1);     
   double valSL   = (1 + iLow(NULL,0,CandelaLow) - 0.0005 - (MODE_SPREAD / 10) );   //Stop loss si posiziona sotto il minimo delle candele (1,2) -5 pips – spread actual
   
   datetime scadenza = orarioRiferimento + cancellazioneScadenza;
                  
   
   // BUY Ordine 1
   ticket1 = OrderSend(Symbol(),OP_BUY,volume,Ask,0,valSL,0," Buy ordine #1 ",0,scadenza,clrCrimson); 
   if (ticket1 < 0) stampa("Errore in apertura Buy ordine #1 ", GetLastError());
   
         
    // TPticket1 = (iHigh(NULL,0,1) - iLow(NULL,0,1)) + iHigh(NULL,0,1);
     
        TPticket1 = (High[1] - Low[1]) + High[1];
   
   // BUY Ordine 2
    ticket2 = OrderSend(Symbol(),OP_BUY,volume,Ask,0,valSL,0," Buy ordine #2 ",0,scadenza,clrCrimson); 
    if (ticket2 < 0) stampa("Errore in apertura Buy ordine #2 ", GetLastError());
 
    
   
   // BUY Ordine 3
   ticket3 = OrderSend(Symbol(),OP_BUY,volume,Ask,0,valSL,0," Buy ordine #3 ",0,scadenza,clrCrimson); 
   if (ticket3 < 0) stampa("Errore in apertura Buy ordine #3 ", GetLastError());
    
     
}


void controlloOrdini(){

   
   // calcoloVolume(rischio / 3,pips);
    
      if (ticket1 > 0  && OrderSelect(ticket1,SELECT_BY_TICKET) && OrderCloseTime() != 0) ticket1 = -1;  //verifica se l'ordine #1 è chiuso
      if (ticket2 > 0  && OrderSelect(ticket2,SELECT_BY_TICKET) && OrderCloseTime() != 0) ticket2 = -1;  //verifica se l'ordine #2 è chiuso
      if (ticket3 > 0  && OrderSelect(ticket3,SELECT_BY_TICKET) && OrderCloseTime() != 0) ticket3 = -1;  //verifica se l'ordine #3 è chiuso
   
  
   // Quando il prezzo raggiunge la condizione di 2 volte l’ampiezza della candela 1 : Close ordine #1 e Stop loss modify a Break Even (Trailing Stop) per gli ordini #2 e #3
   
   if (Bid >= TPticket1){   
   
    stampa( " Bid >= 2 * ampiezza candela 1, l'ordine #1 è : ",  ticket1);
    
    if (ticket1 > 0) {
    
       if (OrderSelect(ticket1,SELECT_BY_TICKET) && OrderClose(ticket1,OrderLots(),Bid,0,clrCoral)) {        // Chiusura ordine #1
       
          ticket1 = -1;                                    
          return;      
      }
       
       else {
       
          stampa("Errore in OrderClose per l'ordine #1 nel caso di Bid >= 2 * ampiezza candela 1 ", GetLastError());
          return;
       }
   }
     
      if (filterTS) TrailingStop1();                           // Trailing Stop per gli ordini #2 e #3
                  
   }


  // Quando il prezzo raggiunge il valore della SMA 21


   if (Bid >= iMA(NULL,0,21,0,MODE_SMA,PRICE_CLOSE,1)){ 
   
      stampa( " Bid >= SMA21, l'ordine #2 è : ",  ticket2);
      
      if (ticket2 > 0) {
    
       if (OrderSelect(ticket2,SELECT_BY_TICKET) && OrderClose(ticket2,OrderLots(),Bid,0,clrCoral)) {        // Chiusura ordine #2
   
          ticket2 = -1; 
          minCandelaChiusuraTicket2 = Low[1];
           
          Print(minCandelaChiusuraTicket2);   
                                          
          return;      
      }
       
        else {
       
          stampa("Errore in OrderClose per l'ordine #2 nel caso di Bid >= SMA21 ", GetLastError());
          return;
        }
       }
       
       
       if (ticket1 > 0) {
    
       if (OrderSelect(ticket1,SELECT_BY_TICKET) && OrderClose(ticket1,OrderLots(),Bid,0,clrCoral)) {        // Chiusura ordine #1 se ancora aperto
       
          ticket1 = -1;                                    
          return;      
      }
       
       else {
       
          stampa("Errore in OrderClose per l'ordine #1 nel caso di Bid >= SMA21 ", GetLastError());
          return;
       }
   }
       
                 
        if (filterTS) TrailingStop2();  // Stop loss modify dell’ordine 3 al min della candela precedente la chiusura dell'ordine #2
      
          }            
              
  
   
   //  Quando il prezzo raggiunge la BB superiore si chiude l'ordine #3


   if (Bid >= iBands(NULL,0,21,2,0,PRICE_CLOSE,MODE_UPPER,0)){ 
   
      stampa( " Bid è >= BB superiore e l'ordine #3 è : ",  ticket3);
      
      if (ticket3 > 0) {
    
       if (OrderSelect(ticket3,SELECT_BY_TICKET) && OrderClose(ticket3,OrderLots(),Bid,0,clrCoral)) {        // Chiusura ordine #3
   
          ticket3 = -1;                                     
          return;      
      }
       
        else {
       
          stampa("Errore in OrderClose per l'ordine #3 nel caso di Bid >= BB superiore ", GetLastError());
          return;
        }
       }
      
      if ( OrderClose(ticket3,volume,Bid,0,clrCoral) ) {     // Chiusura ordine #3
       
          ticket3 = -1;                                      // inizializzo l'array
          return;      
       }
       
       else {
       
          stampa("Errore in OrderClose per l'ordine #3 nel caso di Bid >= BB superiore ", GetLastError());
          return;
       }
      
                     
   }
   
}




// Trailing Stop per gli ordini #2 e #3 (Stop loss modify a Break Even)
void TrailingStop1(){    

  
  if (ticket2 > 0){  // seleziona l'ordine #2 se è presente
  
    if (OrderSelect(ticket2,SELECT_BY_TICKET)){    // verifica se l'ordine è aperto
                  
          if (OrderStopLoss() != OrderOpenPrice()){   // verifica se lo SL è già stato spostato
          
             if (!OrderModify(ticket2,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),OrderExpiration(),clrViolet)){  // modifica ordine con SL uguale a Break even (prezzo d'entrata)
                
                stampa("Errore in OrderModify per Trailing Stop dell'ordine #2 ", GetLastError());
                
             }
          
          }
                 
       }
       
         
    }  
  
    else {                 // ordine chiuso 
     
       return;   
       
     
     }
  
  
   if (ticket3 > 0){  // seleziona l'ordine #3 se è presente
  
    if ( OrderSelect(ticket3,SELECT_BY_TICKET)){                  // verifica se l'ordine è aperto
                  
          if (OrderStopLoss() != OrderOpenPrice() && OrderStopLoss()!= minCandelaChiusuraTicket2){               // verifica se lo SL è già stato spostato
          
             if (!OrderModify(ticket3,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),OrderExpiration(),clrViolet)){  // modifica ordine con SL uguale a Break even (prezzo d'entrata)
                
                stampa("Errore in OrderModify per Trailing Stop dell'ordine #3 ", GetLastError());
                
             }
          
          }
                 
       }
       
         
    }  
  
    else {                 // ordine chiuso 
     
       return;     
     
     }
   
  
  }




// Stop loss modify dell’ordine 3 , Stop loss modify dell’ordine 3 al min della candela precedente la chiusura dell'ordine #2
void TrailingStop2(){      
  
  
  double valSL = 0;
  
      
    valSL = minCandelaChiusuraTicket2;   // SL = min candela precedente la chiusura dell'ordine 2      
    
   
  if (ticket3 > 0 && OrderSelect(ticket3,SELECT_BY_TICKET)){      // seleziona l'ordine #3 se è presente
    
           if (OrderStopLoss() != valSL){                         // verifica se lo SL è già stato spostato
          
             if (!OrderModify(ticket3,OrderOpenPrice(),valSL,OrderTakeProfit(),OrderExpiration(),clrViolet)){  // modifica ordine #3 
                
                stampa("Errore in OrderModify per Trailing Stop dell'ordine #3 ", GetLastError());
                
             }
          
          }
                 
       
       
         
    }  
  
    else {                 // ordine chiuso 
     
       ticket3 = -1;     
     
     }
  
  }





bool condizione1(){  //se la candela 1 chiude dentro la BB 21 (> 2dev inf) e la precedente (2) chiude fuori la BB 21 (< della 2dev inf)
   
   double   valoreCloseCandela1 =   Close[1];            //iClose(Symbol(),timeFrameRiferimento,1);  // calcolo prezzo chiusura ultima candela
   double   valoreCloseCandela2 =   Close[2];            //iClose(Symbol(),timeFrameRiferimento,2);  // calcolo prezzo chiusura penultima candela
   
   if (valoreCloseCandela1 > iBands(NULL,0,21,2,0,PRICE_CLOSE,MODE_LOWER,1) && valoreCloseCandela2 < iBands(NULL,0,21,2,0,PRICE_CLOSE,MODE_LOWER,2)){
   
      Print("Candela 1 ha chiuso dentro la BB 21 inf e la Candela 2 ha chiuso fuori la BB 21 inf");
   
     return(true); 
    } 
        
      else return(false);
                    
}


bool condizione2(){  //se le due candele (1,2) hanno la stessa altezza (high-low = +-range%) 

   double range = rangeAltezzaCandele;
   
   double   valoreHighCandela1 = High[1];            //iHigh(Symbol(),timeFrameRiferimento,1);  // calcolo high ultima candela
   double   valoreHighCandela2 = High[2];            //iHigh(Symbol(),timeFrameRiferimento,2);  // calcolo high penultima candela
   
   double   valoreLowCandela1  = Low[1];            //iLow(Symbol(),timeFrameRiferimento,1);  // calcolo low ultima candela
   double   valoreLowCandela2  = Low[2];                   //iLow(Symbol(),timeFrameRiferimento,2);  // calcolo low penultima candela
   
   
   if (MathAbs(1 - (valoreHighCandela1 - valoreLowCandela1 + 0.0001 ) / (valoreHighCandela2 - valoreLowCandela2 + 0.0001)) <= (range / 100)) {
   
      Print("Candela 1 e Candela 2 hanno un'ampiezza uguale a +-", string(range), "%");
   
      return (true);
   
   }
   
    return (false);         
}
 

It would help people to help you if you highlighted the code that causes the problem


int OnInit()
{
  
   oldTime = Time[0];



Never try to get any values from series arrays such as Time[] in OnInit as they may not be loaded yet and so return an array out of range error.

 
Keith Watford:

It would help people to help you if you highlighted the code that causes the problem


int OnInit()
{
  
   oldTime = Time[0];



Never try to get any values from series arrays such as Time[] in OnInit as they may not be loaded yet and so return an array out of range error.


I changed the code my error occurred again, I attach you a screenshot of my Journal Tab. How can I find the code that cause error?
 
elfunambolo:

I changed the code my error occurred again, I attach you a screenshot of my Journal Tab. How can I find the code that cause error?

The error is on Line 376 of your code, 42 characters in.
 
honest_knave:

The error is on Line 376 of your code, 42 characters in.


This is the line, inside condizione1() fuction

 double   valoreCloseCandela1 =   Close[1];     
 
elfunambolo:


This is the line, inside condizione1() fuction

You can add an extra line like this:

bool condizione1(){  //se la candela 1 chiude dentro la BB 21 (> 2dev inf) e la precedente (2) chiude fuori la BB 21 (< della 2dev inf)
   
 
   if(Bars < 3)  return(false);   

   double   valoreCloseCandela1 =   Close[1];            //iClose(Symbol(),timeFrameRiferimento,1);  // calcolo prezzo chiusura ultima candela
   double   valoreCloseCandela2 =   Close[2];            //iClose(Symbol(),timeFrameRiferimento,2);  // calcolo prezzo chiusura penultima candela
   

   //.................................. 

}


A similar thing for the condizione2() function....


Regards.

Reason: