CloseOrder() 4051 error

 

Hello,

backtest return me 4051 error during CloseOrder() execution inside my OrderCheck() fuction.

int ticket[3] = {-1,-1,-1};   is a global variable

How can I fix it?

The code is attached.


Thanks

Giovanni

Files:
my_EA.mq4  3 kb
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. if (Bid >= TPticket1[0]){  
    On EURUSD for example Bid is 0.17075 and ticket numbers are 138807893. What is the meaning of dog >= car?
  3.  if ( OrderClose(ticket[0],volume,Bid,0,clrCoral) ) {    
    What is volume. What if the ticket is a sell. Perhaps you should use Trade Functions
 
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2. On EURUSD for example Bid is 0.17075 and ticket numbers are 138807893. What is the meaning of dog >= car?
  3. What is volume. What if the ticket is a sell. Perhaps you should use Trade Functions

Thanks for reply,

1. I had edit my first post attaching the file.

2. TPticket[] contains a price (see below code)

3. Volume is declared in OrderCheck() fuction as:

 double volume = 1;

 

The issue is that ticket[0] = -1 but should be a valid number 

I attach you also Journal log file.


 // BUY Ordine 1


   ticket[0] = OrderSend(Symbol(),OP_BUY,1,Ask,0,valSL,0,"Buy ordine #1",0,scadenza,clrCrimson); 
   if (ticket[0] < 0) stampa("Errore in apertura Buy ordine #1 ", GetLastError());
         
     TPticket1[0] = (iHigh(NULL,0,1) - iLow(NULL,0,1)) + iHigh(NULL,0,1);
Files:
Journal.PNG  34 kb
 
elfunambolo:

1. I had edit my first post attaching the file.

2. TPticket[] contains a price (see below code)

3. Volume is declared in OrderCheck() fuction as:  double volume = 1;


The issue is that ticket[0] = -1 but should be a valid number

 // BUY Ordine 1


   ticket[0] = OrderSend(Symbol(),OP_BUY,1,Ask,0,valSL,0,"Buy ordine #1",0,scadenza,clrCrimson); 
   if (ticket[0] < 0) stampa("Errore in apertura Buy ordine #1 ", GetLastError());
         
     TPticket1[0] = (iHigh(NULL,0,1) - iLow(NULL,0,1)) + iHigh(NULL,0,1);
elfunambolo: backtest return me 4051 error during CloseOrder() execution inside my OrderCheck() fuction.
  1. Instead of editing your code using SRC, as asked, you deleted it and attached the file. Fine. But you didn't use SRC for your last post. If you can't follow simple directions as that, how do you expect to code? Edit your last post.
  2. I misread ticket[] vs TPticket[]. Since the two are related, they really should be an array of a struct containing a ticket number and a TP price.
  3. Fine but you don't use the variable in your OrderSend's. Change one or the other and your code breaks. Use OrderLots in your OrderClose so that works even when you change other code.
  4. You asked why you get 4051. You've answered your own question: "The issue is that ticket[0] = -1 but should be a valid number." Why should it be? Once you close an order, you set it to -1, so you can't close it again. If the ticket is -1 don't call OrderClose.
  5. Why are you using function calls instead of predefined variables High[] and Low[]
 
whroeder1:
  1. Instead of editing your code using SRC, as asked, you deleted it and attached the file. Fine. But you didn't use SRC for your last post. If you can't follow simple directions as that, how do you expect to code? Edit your last post.
  2. I misread ticket[] vs TPticket[]. Since the two are related, they really should be an array of a struct containing a ticket number and a TP price.
  3. Fine but you don't use the variable in your OrderSend's. Change one or the other and your code breaks. Use OrderLots in your OrderClose so that works even when you change other code.
  4. You asked why you get 4051. You've answered your own question: "The issue is that ticket[0] = -1 but should be a valid number." Why should it be? Once you close an order, you set it to -1, so you can't close it again. If the ticket is -1 don't call OrderClose.
  5. Why are you using function calls instead of predefined variables High[] and Low[]


Ok, sorry but I'm new in computer language programming.

I don't have more error 4051, I create #3 orders and I delete it based on some conditions. I replaced array variable with a int global variables (ticket1, ticket2 and ticket3).

Now I want to use TralingStop1() fuction to modify SL of order #2 and #3 after closed the order#1, but I have error #1 in OrderModify() : Errore in OrderModify per Trailing Stop dell'ordine #3 1

 I see that means "No error returned, but the result is unknown" but I don't know which is wrong in my code.


Thanks for help

Giovanni



void OnTick()
{
          if (condizione1() && check){            // && condizione2()&& condizione3() && condizione4() && condizione5() && condizione6() && condizione7() && condizione8() 
                                           
          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;
                       
           }
     
        }
        
}


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 = (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 (Traling 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;                                     
          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) TralingStop2();  // Stop loss modify dell’ordine 3 , sotto il minimo della candela che tocca la SMA 21
      
          }            
              
  
   
   //  Quando il prezzo raggiunge la BB superiore si chiude l'ordine #3


   if (Bid >= iBands(NULL,0,21,2,1,PRICE_CLOSE,MODE_UPPER,1)){ 
   
      stampa( " Bid è >= BB superiore e l'ordine #3 è : ",  ticket3);
      
      if (ticket2 > 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 Traling Stop dell'ordine #2 ", GetLastError());
                
             }
          
          }
                 
       }
       
         
    }  
  
    else {                 // ordine chiuso 
     
       ticket2 = -1;     
     
     }
  
  
   if (ticket3 > 0){  // seleziona l'ordine #3 se è presente
  
    if ( OrderSelect(ticket3,SELECT_BY_TICKET)){                  // verifica se l'ordine è aperto
                  
          if (OrderStopLoss() != OrderOpenPrice()){               // 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 Traling Stop dell'ordine #3 ", GetLastError());
                
             }
          
          }
                 
       }
       
         
    }  
  
    else {                 // ordine chiuso 
     
       ticket3 = -1;     
     
     }
   
  
  }

 
elfunambolo I see that means "No error returned, but the result is unknown" but I don't know which is wrong in my code.
Nothing. The request was sent but got no reply (probably a network disconnection.) I return and wait for a new tick, then see if there is a new order or not.
 
whroeder1:
Nothing. The request was sent but got no reply (probably a network disconnection.) I return and wait for a new tick, then see if there is a new order or not.


Hello,

I added a condition to avoid to generate  two triplet of orders in the same candle.

No I don't want to send my triplet of orders in the same candle in which one SL happened, how can I implement this condition? 


int start(){
          
          if (condizione1() && oldTime != Time[0] && check){            // && condizione2()&& condizione3() && condizione4() && condizione5() && condizione6() && condizione7() && condizione8() 
                        
          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;
                  
   // double volume = calcoloVolume(rischio / 3,pips);
   
   // stampa (" Volume = ", string(volume));
   
   // 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,1,PRICE_CLOSE,MODE_UPPER,1)){ 
   
      stampa( " Bid è >= BB superiore e l'ordine #3 è : ",  ticket3);
      
      if (ticket2 > 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;     
     
     }
  
  }

Reason: