WHRoeder: And OrderClosePrice get's it's value from them. No change since 2012
In your linked post
It turns out that OrderClosePrice is a copy of data and does not update on its own or on a RefreshRates. You have to explicitly do another OrderSelect (by test).
I am unable to test this now as the market is closed, but I have tested it before and OrderClosePrice was updated with re-selecting the order. No need for a RefreshRates.
gjol mentioned that he tested this with a script, maybe he can confirm whether he used RefreshRates or not.
Hi Everyone! Thanks for your replies. This is my updated function, which re-selects the order after a requote to try again. The function will iterate all market orders and try to close them over and over again until successful, selecting the order again to do so. This should work, could not test because it is weekend. Use the function if you feel so. Will let you know how it goes.
bool CloseOrder(int Type, int Token = EMPTY_VALUE, int ticket = EMPTY_VALUE)
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) &&
OrderSymbol() == Symbol() &&
Type == OrderType() &&
(ticket == EMPTY_VALUE || ticket == OrderTicket()))
{
if(Type == OP_BUY && Bid > OrderOpenPrice()-(Ask-Bid) && Token == OnlyInLoss) continue;
if(Type == OP_SELL && Ask < OrderOpenPrice()+(Ask-Bid) && Token == OnlyInLoss) continue;
if(Type == OP_BUY || Type == OP_SELL)
{
int closed = false;
while(!closed)
{
// Re-select to update close priceif(OrderSelect(OrderTicket(), SELECT_BY_TICKET))
{
closed = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Gold);
if(!closed) Print(ShortName +" (OrderClose Error) "+ ErrorDescription(GetLastError()) +". Trying again...");
} else {
Print(ShortName +" (OrderSelect Error) "+ ErrorDescription(GetLastError()));
closed = true; // ticket does not exist, do not fall into infinite loop
}
}
}if(Type == OP_BUYSTOP || Type == OP_SELLSTOP || Type == OP_BUYLIMIT || Type == OP_SELLLIMIT)
{
if(OrderMagicNumber() == MagicNumber)
{
if(!OrderDelete(OrderTicket(), Gold))
{
Print(ShortName +" (OrderDelete Error) "+ ErrorDescription(GetLastError()));
return(false);
}
}
}
}
}
return(true);
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
And OrderClosePrice get's it's value from them. No change since 2012
In your linked post
It turns out that OrderClosePrice is a copy of data and does not update on its own or on a RefreshRates. You have to explicitly do another OrderSelect (by test).
I am unable to test this now as the market is closed, but I have tested it before and OrderClosePrice was updated with re-selecting the order. No need for a RefreshRates.
gjol mentioned that he tested this with a script, maybe he can confirm whether he used RefreshRates or not.
i can't confirm because my test was
Hi Everyone! Thanks for your replies. This is my updated function, which re-selects the order after a requote to try again. The function will iterate all market orders and try to close them over and over again until successful, selecting the order again to do so. This should work, could not test because it is weekend. Use the function if you feel so. Will let you know how it goes.
I ran this test
and the results were
0 05:09:52.937 AB EURJPY,H4: Initial OrderClosePrice = 132.976 Ask = 132.976
0 05:10:53.296 AB EURJPY,H4: After RR() OrderClosePrice = 132.976 Ask = 132.962
0 05:10:53.296 AB EURJPY,H4: After Re-select OrderClosePrice = 132.962 Ask = 132.962
0 05:11:53.656 AB EURJPY,H4: After Sleep OrderClosePrice = 132.962 Ask = 132.962
0 05:11:53.656 AB EURJPY,H4: After Re-select OrderClosePrice = 132.982 Ask = 132.962
0 05:11:53.656 AB EURJPY,H4: After RR() OrderClosePrice = 132.982 Ask = 132.982
This shows that RefreshRates does not need to be called before re-selecting the order.
Hi @qjol! Oh my! Well it would still work because boolean types can be casted to int directly, but it is a major code typo xD Sorry.
Convert int closed = false; to bool closed = false;