Invalid price???

 

I'm now back to programming MT4 - after all the changes the NFA/CFTC put in our way, and now a very odd thing is happening with build 402 -- every so often (enough that this needs to be addressed) I get 129 errors (Invalid Price) - here's a print showing the price used and the current bid/ask:

02:42:59 IQIv1-Production EURUSD,M5: Order close failure; ticket (2094674633): Invalid Price(129): 1.42826000 Bid:1.42826000 Ask:1.42847000

As you can see, the price used (immediately to the right of the (129)) and the Bid are identical. This was a long trade I was trying to close out, so Bid should have been adequate. I also use spread*4 for slippage - so, that's not the issue. I never got this error before - and, haven't found any recent articles about anyone who has experienced this same behavior ... hints?

 
Maybe you needed to do a RefreshRates before trying to close the order ?
 
RaptorUK:
Maybe you needed to do a RefreshRates before trying to close the order ?


Raptor - thanks, tried that also... as you can see - the price variable I use contains the exact same value as Bid; No floating point issue...

I chose to use a variable because when I was coding for a newer broker, the Ask and Bid on opens and closes seemed to count substantially - by using the variable (set either to Bid or Ask depending on the operation) I stopped a lot of requotes (#138s) that I seem to start getting a lot of lately also.

 

Sorry, just to be clear . . . . if you store values for your bid and ask variables then your code does something for a couple of seconds the bid and ask may change . . then you try to close your order and you have invalid values stored in your variables . . . it's impossible to say if your code is doing this without seeing the code.

You can test it by adding a RefreshRates(); and Print("Bid = ",Bid," Ask = ", Ask); just before you try to close the order, if your Bid and Ask variables are out of date you will see the difference . .

 
RaptorUK:

Sorry, just to be clear . . . . if you store values for your bid and ask variables then your code does something for a couple of seconds the bid and ask may change . . then you try to close your order and you have invalid values stored in your variables . . . it's impossible to say if your code is doing this without seeing the code.

You can test it by adding a RefreshRates(); and Print("Bid = ",Bid," Ask = ", Ask); just before you try to close the order, if your Bid and Ask variables are out of date you will see the difference . .


Thanks, Raptor - but, again, I printed out the "price" variable, and the "Bid" and the "Ask" -- see above. The "price" variable is identical to "Bid" digit for digit, non-normalized - the price value is set one instruction prior to ordersend() and the error is the next subsequent instruction that prints out the values -- and, they're equal.

My code looks like this:

//+------------------------------------------------------------------+
//| OpenOrder - Places new orders on market |
//+------------------------------------------------------------------+
void OpenOrder(int Action, int Type)
{
int ticket = 0;
double price = 0.00;
double stopPrice = 0.00;
double tpPrice = 0.00;

if (Action==OP_BUY)
{
price = Ask;
stopPrice = Bid-(extDefaultStop*pip());
tpPrice = Bid+(extDefaultTarget*pip());
}

if (Action==OP_SELL)
{
price = Bid;
stopPrice = Ask+(extDefaultStop*pip());
tpPrice = Ask-(extDefaultTarget*pip());
}

ticket=OrderSend(Symbol(),
Action,
LotSize(),
price,
extSlipFactor*100,
0.00,
0.00,
"Auto",0,0,0);

if (ticket<1)
Print(OpCode(Action)+" failure @"+DoubleToStr(price,Digits)+"; error ("+GetLastError()+"): "+DoubleToStr(LotSize(),2));
else
{
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);

tdAction=Action;
tdPrice=OrderOpenPrice();

OrderModify(ticket,tdPrice,stopPrice,tpPrice,0);
}
}

Any hints?

 
DennisJorgenson:


Thanks, Raptor - but, again, I printed out the "price" variable, and the "Bid" and the "Ask" -- see above. The "price" variable is identical to "Bid" digit for digit, non-normalized - the price value is set one instruction prior to ordersend() and the error is the next subsequent instruction that prints out the values -- and, they're equal.

My code looks like this:


Any hints?

I thought you were having problems closing an order ? why are you showing the code to open an order ?

If you enter start() on a tick, store values of Bid and Ask, sleep 10 seconds then print these variables and also print Bid and Ask you will get the same values for the stored values and the Bid and Ask . . . from what I understand you need a new tick or you use RefreshRates to update Bid and Ask.

I'm not talking about Normailizing . . I'm talking about getting fresh values for Bid and Ask . . .

 
RaptorUK:

I thought you were having problems closing an order ? why are you showing the code to open an order ?

If you enter start() on a tick, store values of Bid and Ask, sleep 10 seconds then print these variables and also print Bid and Ask you will get the same values for the stored values and the Bid and Ask . . . from what I understand you need a new tick or you use RefreshRates to update Bid and Ask.

I'm not talking about Normailizing . . I'm talking about getting fresh values for Bid and Ask . . .


Doh! My bad -- here's the code for the close... ;-) Now, I have tried it with both the normalize and without ---

//+------------------------------------------------------------------+
//| CloseOrder |
//+------------------------------------------------------------------+
void CloseOrder(int Ticket)
{
bool success;
double price;
int error;
string errmsg="Order close failure; ticket ("+Ticket+"): ";

OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES);

if (OrderType()==OP_BUY) price = Bid;
if (OrderType()==OP_SELL) price = Ask;

success=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(price,Digits),(extSlipFactor)*200,Red);

if (!success)
{
error = GetLastError();

switch (error)
{
case 129: errmsg=errmsg+"Invalid Price(129): "+price+" Bid:"+Bid+" Ask:"+Ask;
break;
case 138: errmsg=errmsg+"Requote(138): "+DoubleToStr(price,Digits);
break;
default: errmsg=errmsg+"Error:"+error;
}

Print(errmsg);
}
}

 

If you get this error consistently you should try this . . .

RefreshRates();     //     <-----------------------  add this

if (OrderType()==OP_BUY) price = Bid;
if (OrderType()==OP_SELL) price = Ask;

https://docs.mql4.com/windows/RefreshRates

 
  1. if (OrderType()==OP_BUY) price = Bid;
    if (OrderType()==OP_SELL) price = Ask;
    
    success=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(price,Digits),(extSlipFactor)*200,Red);
    You do not need to use normalize, ever. Bid/Ask are already normalized and you could just use
    success=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),(extSlipFactor)*200,Red);


  2. Always test return codes (orderSelect) and Print
 
Simon Gniadkowski:

Sorry, just to be clear . . . . if you store values for your bid and ask variables then your code does something for a couple of seconds the bid and ask may change . . then you try to close your order and you have invalid values stored in your variables . . . it's impossible to say if your code is doing this without seeing the code.

You can test it by adding a RefreshRates(); and Print("Bid = ",Bid," Ask = ", Ask); just before you try to close the order, if your Bid and Ask variables are out of date you will see the difference . .

Thank you so much dear friend . it was so effective . thank you again .

Reason: