Download MetaTrader 5

MQL4: Error 129 on OrderSend and OrderClose

To add comments, please log in or register
Matthias De Paolis
491
Matthias De Paolis  

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

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
Please edit and use SRC.
Keith Watford
Moderator
10641
Keith Watford  
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

Matthias De Paolis
491
Matthias De Paolis  
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

Matthias De Paolis
491
Matthias De Paolis  
deysmacro:
Please edit and use SRC.

Sorry :-)

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

Keith Watford
Moderator
10641
Keith Watford  
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

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
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.

Matthias De Paolis
491
Matthias De Paolis  
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?

Matthias De Paolis
491
Matthias De Paolis  
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

whroeder1
15735
whroeder1  
When you get the error, print out ALL your variables plus Bid and Ask and you'll find out why.
Matthias De Paolis
491
Matthias De Paolis  

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

12
To add comments, please log in or register