Error 129 on OrderClose()

 

Hi guys

I have a serious problem I cannot fix:

When I try to close a SELL operation by EA, I get always 129 error.

I post the code:

Alert("Attempting to close the ticket " + ticket);

RefreshRates();

if(OrderClose(ticket,0.03,NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),40,CLR_NONE))

{

buffer = buffer+(ticket+"="+"1"+","); // I need this line to communicate with my Java server

}else{

Alert("Error! Closing not allowed following the error ",GetLastError());

buffer = buffer+(ticket+"="+"-1"+",");

}

I try to execute this code several times before quitting.

Ads you can see, i put a very large slippage to make sure it's not a problem of tiny restriction.

PLEASE HELP ME

 

Please use this to post code . . . it makes it easier to read.

 
Can you please show more of your code . . for example, where are you doing the OrderSelect() ?
 

Let me try...

Alert("Attempting to close the ticket " + ticket);

RefreshRates();

if(OrderClose(ticket,0.03,NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),40,CLR_NONE))

{

	buffer = buffer+(ticket+"="+"1"+","); // I need this line to communicate with my Java server

}else{

	Alert("Error! Closing not allowed following the error ",GetLastError());

	buffer = buffer+(ticket+"="+"-1"+",");// I need this line to communicate with my Java server

}

Yeah, it works.

Can you help me?

 
I repeat my question . . where is your OrderSelect ?
 
RaptorUK:
Can you please show more of your code . . for example, where are you doing the OrderSelect() ?

Of course... just a moment:
//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

   int       lastTicket = -1;
   double    takeProfit = 0.0012;
   string    file_extension   = ".csv";
   string    buffer;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


   void currentValueWriter() {
      
      //SWITCH IL PERIODO DEL FILE DELLA STRINGA   
      string period_string_file;
   
      period_string_file = "1";

      string strline2 = DoubleToStr(iOpen  (Symbol(), PERIOD_M1, 1), 4);
      string strline3 = DoubleToStr(iHigh  (Symbol(), PERIOD_M1, 1), 4);
      string strline4 = DoubleToStr(iLow   (Symbol(), PERIOD_M1, 1), 4);
      string strline  = DoubleToStr(iClose (Symbol(), PERIOD_M1, 1), 4);
      string strline5 = DoubleToStr(iVolume(Symbol(), PERIOD_M1, 1), 4);
      string strline6 = DoubleToStr(Bid, 4);
      string strline7 = TimeToStr  (TimeCurrent(),TIME_SECONDS);
      //Alert(strline7);

      double strline_intero=StrToDouble(strline);
      double strline_intero2=StrToDouble(strline2);
      double strline_intero3=StrToDouble(strline3);
      double strline_intero4=StrToDouble(strline4);
      double strline_intero5=StrToDouble(strline5);
      double strline_intero6=StrToDouble(strline6);

      int history_data_close  = strline_intero*10000;
      int history_data_open   = strline_intero2*10000;
      int history_data_high   = strline_intero3*10000;
      int history_data_low    = strline_intero4*10000;
      int history_data_volume = strline_intero5;
      int history_data_bid    = strline_intero6*10000;
   
      string filename = Symbol() + "_curr" + file_extension;

      int handle = FileOpen(filename, FILE_CSV|FILE_WRITE, "\t"); 
   
      FileWrite(handle, history_data_open+","+history_data_high+","+history_data_low+","+history_data_bid+","+history_data_volume+","+strline7);
      
      FileClose(handle);
   } // end method currentValueWriter()
   
   

int start()
  {
  
  // Viene scritto su file il valore corrente
      currentValueWriter();
  
      
      string   filename =  "a_"+Symbol()+".csv";
      
      int      handle   =  FileOpen(filename,FILE_CSV|FILE_READ,";");
    
      string   input        =  FileReadString(handle);
      
      FileClose(handle);
      
      overrideInputFile();
      
      int index = 0;
      int newindex = 0;
      int k = 0;
      
      buffer = "";
      
      while( index < StringLen(input) && newindex >= 0){
         
         newindex = StringFind(input,",",index);
         string s = StringSubstr(input,index,newindex);
         index = newindex+1;
         
         Alert("Stringa letta : " + s);
      
      
         int subindex      =  StringFind(s, "=", 0);
         string id      =  StringSubstr(s,0,subindex);
         string sNumber =  StringSubstr(s,subindex+1,StringLen(s));
         int number     =  StrToInteger(sNumber);
         Alert(id);
         Alert(number);
         // Viene gestita la posizione letta dal file      
         int lastTicket;
         if(number >= 1) {
         //if(currentPosition == 0){
            //lastTicket = ApriPosizioneEuro();        
         //}else if(currentPosition == -1){
            //ChiudiPosizioneDollaro(lastTicket);
               lastTicket = ApriPosizioneEuro(id);
         //} // end if - else
         } else if(number <= -1) {  
         //if(currentPosition == 1){   
            //ChiudiPosizioneEuro(lastTicket);
            //lastTicket = ApriPosizioneDollaro();
         //}else if(currentPosition == 0){
               lastTicket = ApriPosizioneDollaro(id); 
            //} // end if - else  
         } else if(number == 0){
         
            Alert("Richiesta di chiusura del ticket: "+id);
            int currentPosition;
            int total=OrdersTotal(); 
            if(total >= 1) {
               
               // Viene richiesto al sistema l'elenco degli ordini correnti
               OrderSelect(id, SELECT_BY_TICKET); 
               int ticket = OrderTicket();
                
               //double price = OrderOpenPrice();
         
               if(OrderType() == OP_BUY) {
                  
                  currentPosition = 1;        
               } else {
                  currentPosition = -1;        
               } // end if - else
         
               if(currentPosition == 1) {
                  ChiudiPosizioneEuro(StrToInteger(id));
               } else if(currentPosition == -1) {
                  ChiudiPosizioneDollaro(StrToInteger(id));
               } // end if - else
               
              
            } // end if - else
         }
      
      }
      
      //buffer = "g";
      //Alert("writing output to file...");
      if(StringLen(buffer) > 0){
      
         string output = StringSubstr(buffer,0,StringLen(buffer)-1);
         writeSimpleResponse(output);
      //}else{
      //   writeSimpleResponse("");
      }
      //Alert("stop expert");
      return(0);   
  }
  
  int ApriPosizioneEuro(string id) {
      RefreshRates();
      int ticket = OrderSend(Symbol(),OP_BUY,0.03,Ask,25,0,0,"commento",0,0,CLR_NONE);
      Alert("Posizione Euro aperta: ticket ",ticket);
      if(ticket < 0) {
         Alert("Errore! posizione non aperta per errore ",GetLastError());
         //writeResponse(id,ticket);
      } // end if
      buffer = buffer+(id+"="+ticket+",");
      return (ticket);
   } // end metodo ApriPosizioneEuro()



   int ApriPosizioneDollaro(string id) {
      RefreshRates();
      int ticket = OrderSend(Symbol(),OP_SELL,0.03,Bid,5,0,0,"commento",0,0,CLR_NONE);
      Alert("Posizione Dollaro aperta: ticket ",ticket);
      if(ticket < 0){
         Alert("Errore! posizione non aperta per errore ",GetLastError());
         //writeResponse(id,ticket);
      } // end if
      buffer = buffer+(id+"="+ticket+",");
      return (ticket);
   } // end metodo ApriPosizioneDollaro()



   void ChiudiPosizioneEuro(int ticket) {
   
      Alert("Attempting to close the ticket " + ticket);
      RefreshRates();
   
   
      if(OrderClose(ticket,0.03,NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS)),40,CLR_NONE))

      {
      
         buffer = buffer+(ticket+"="+"1"+",");
         //writeResponse(ticket,1);
      
      }else{
      
         Alert("Errore! posizione non chiusa per errore ",GetLastError());
         buffer = buffer+(ticket+"="+"-1"+",");
         //writeResponse(ticket,-1);
      }
            
   } // end metodo ChiudiPosizioneEuro(int)



   void ChiudiPosizioneDollaro(int ticket){
      Alert("Attempting to close the ticket " + ticket);
      RefreshRates();
   
   
      if(OrderClose(ticket,0.03,NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),40,CLR_NONE))
      {
      
         buffer = buffer+(ticket+"="+"1"+",");
         //writeResponse(ticket,1);
      
      }else{
      
         Alert("Errore! posizione non chiusa per errore ",GetLastError());
         buffer = buffer+(ticket+"="+"-1"+",");
         //writeResponse(ticket,-1);
      }
   } // end metodo ChiudiPosizioneDollaro(int)
   
   void overrideInputFile(){
   
      //Alert("Override del file di input...");
   
      string filename = "a_" + Symbol() + ".csv";

      int handle = FileOpen(filename, FILE_CSV|FILE_WRITE, "\t"); 
   
      FileWrite(handle, "");
      
      FileClose(handle);

      //Alert("Fine override del file di input");
   }
   

 

I suspect that you may be trying to close a position on the Euro using the Dollar Ask price or a position on the Dollar using the Euro Ask price.

Add more Print statements to your code, for example, Print("Current chart is ", Symbol());

 
RaptorUK:

I suspect that you may be trying to close a position on the Euro using the Dollar Ask price or a position on the Dollar using the Euro Ask price.

Add more Print statements to your code, for example, Print("Current chart is ", Symbol());


It wouldn't work in closing Euro position, too. Do I mistake? But it works for Euro (buy) positions...

 
What chart do you have this EA on ?
 
RaptorUK:
What chart do you have this EA on ?


EUR / USD

5 minutes

There's an indicator I use just to give me historical data, no interference with the operations.

 

OK, you have, possibly, many problems . . for example, this can't work . . .

string id      =  StringSubstr(s,0,subindex);   //  <-----   id  is declared as a string


     // Viene richiesto al sistema l'elenco degli ordini correnti

    OrderSelect(id, SELECT_BY_TICKET);  //       <-----   id is a string,  OrderSelect wants an  int . . .

    int ticket = OrderTicket();
                
         
    if(currentPosition == 1) {
        ChiudiPosizioneEuro(StrToInteger(id));     //  <-----   id is a string and is probably a currency pair . . . ChiudiPosizioneEuro  wants a ticket as an int
        } else if(currentPosition == -1) {
            ChiudiPosizioneDollaro(StrToInteger(id));
Reason: