MQL4: Error 129 on OrderSend and OrderClose

 

Hi,

I'm writing a script that insert and delete orders.
With pending orders it's all right but when I search to insert a market order or close one of it I'll frequently recive error 129 (Invalid price)
I read on the online documentation that kind of error can happen if there has not been the requested open price in the price thread or it has not been normalized according to the amount of digits after decimal point.

so I modify my code like this:

string CloseOrder(string& instr[]){
   int ticket=StrToInteger(instr[1]);
   double volume= StrToDouble(instr[3]);
   int slippage=StrToInteger(instr[4]);
   if(StringFind(instr[2],"BUY")>-1){
      if(!OrderClose(ticket,volume,Bid,slippage,Red))
      {
         return "Error: " + GetLastError();
      }else{
         return "Order: " + IntegerToString(ticket) + "succesfully close.";
      }
   }else{
      if(!OrderClose(ticket,volume,Ask,slippage,Red))
      {
         return "Error: " + GetLastError();
      }else{
         return "Order: " + IntegerToString(ticket) + "succesfully close.";
      }
   }
}

string SendMarketOrder(string& instr[]){
   string symbol=instr[1];
   int cmd=StrToInteger(instr[2]);
   double volume= StrToDouble(instr[3]);
   int slippage=StrToInteger(instr[4]);
   double stoploss;
   double takeprofit;
   int ticket;
   switch(cmd){
      case 0://BUY
         stoploss=NormalizeDouble(Bid-StrToDouble(instr[5])*Point,Digits);
         takeprofit= NormalizeDouble(Ask+StrToDouble(instr[6])*Point,Digits);
         ticket = OrderSend(symbol,cmd,volume,Ask,slippage,stoploss,takeprofit,NULL,0,0,clrNONE);
         break;
      case 1://SELL
         stoploss=NormalizeDouble(Ask+StrToDouble(instr[5])*Point,Digits);
         takeprofit= NormalizeDouble(Bid-StrToDouble(instr[6])*Point,Digits);
         ticket = OrderSend(symbol,cmd,volume,Bid,slippage,stoploss,takeprofit,NULL,0,0,clrNONE);
         break;
   }
   if(ticket<0)
   {
      return "Error: " + GetLastError();
   }else{
      return "Ticket: " + IntegerToString(ticket);
   }
}

But I'll still recive error 129! :-(

Please, can someone help me to understand what it's wrong in my code?

Best regards
Dema

 
Please edit and use SRC.
 
string CloseOrder(string& instr[]){
int ticket=StrToInteger(instr[1]);
double volume= StrToDouble(instr[3]);
int slippage=StrToInteger(instr[4]);
if(StringFind(instr[2],"BUY")>-1){
if(!OrderClose(ticket,volume,Bid,slippage,Red))
{
return "Error: " + GetLastError();
}else{
return "Order: " + IntegerToString(ticket) + "succesfully close.";
}
}else{
if(!OrderClose(ticket,volume,Ask,slippage,Red))
{
return "Error: " + GetLastError();
}else{
return "Order: " + IntegerToString(ticket) + "succesfully close.";
}
}
}

Is the symbol of the trade that you are trying to close the same as the chart symbol?

If not, you are trying to close a trade of one symbol with the Bid or Ask of a different one.

The same applies to your OrderSends

 
GumRai:

Is the symbol of the trade that you are trying to close the same as the chart symbol?

If not, you are trying to close a trade of one symbol with the Bid or Ask of a different one.

The same applies to your OrderSends

Hi GumRai,

yes, the chart symbol is EURUSD and the symbol of the order that I try to close is EURUSD

 
deysmacro:
Please edit and use SRC.

Sorry :-)

I've change my post with the SRC tags around my snippet

 
dema:

Hi GumRai,

yes, the chart symbol is EURUSD and the symbol of the order that I try to close is EURUSD


Well, it's a bit difficult to tell as you are referencing various cells in an array.

May I suggest that you simplify things at least for the OrderClose. There is no need to determine whether it is a buy or sell trade.

OrderSelect the ticket, check that it has not closed

Then close it using OrderLots and OrderClosePrice

 
GumRai:


Well, it's a bit difficult to tell as you are referencing various cells in an array.

May I suggest that you simplify things at least for the OrderClose. There is no need to determine whether it is a buy or sell trade.

OrderSelect the ticket, check that it has not closed

Then close it using OrderLots and OrderClosePrice


No no. Better dema show us the full code, then we will know straight away the problem is.

dema, please show us the full code and we will help you if it is not too hard.

 
GumRai:


Well, it's a bit difficult to tell as you are referencing various cells in an array.

May I suggest that you simplify things at least for the OrderClose. There is no need to determine whether it is a buy or sell trade.

OrderSelect the ticket, check that it has not closed

Then close it using OrderLots and OrderClosePrice

Thank's for the answer!

I'll try changing my function like this:

string CloseOrder(string& instr[]){
   int ticket=StrToInteger(instr[1]);
   int slippage=StrToInteger(instr[4]);
   if(OrderSelect(ticket, SELECT_BY_TICKET))
     {
       if(OrderClose(ticket,OrderLots(),OrderClosePrice(),slippage,Red))
       {
           return "Order: " + IntegerToString(ticket) + "succesfully close.";
        }else{
           return "OrderClose Error: " + GetLastError();
        }
     }
   else
     return "OrderSelect Error: " + GetLastError();     
}

For the SendOrder trouble, have you some suggest for me?

 
deysmacro:

No no. Better dema show us the full code, then we will know straight away the problem is.

dema, please show us the full code and we will help you if it is not too hard.

The other part of my code is insignificant for this trouble, because I'll receive from an external source the instructions for execute operations.

If you ask me what you need to understand the scenario I'll search to explain you

 
When you get the error, print out ALL your variables plus Bid and Ask and you'll find out why.
 

I resolve my trouble simply use the function RefreshRates() before use Ask or Bid values.

I think they are initialized when my script start to run, so when I use it, if I don't refresh with RefreshRates(), are obsolete.

thanks

Reason: