I hope you are fine. I am having problems processing requotes in my CloseOrder() function. My function tries to close trades in a loop. If the broker sends a requote, the Ea tries again and again until closed.
In theory it should work, however sometimes the broker sends back an error "Many requests" and does not accept any other transaction requests. This is my code:
* Closes desired orders
* @param int Type
* @param int Token
* @param int ticket
* @return bool
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;
closed = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Gold);
if(!closed) Print(ShortName +" (OrderClose Error) "+ ErrorDescription(GetLastError()) +". Trying again...");
if(Type == OP_BUYSTOP || Type == OP_SELLSTOP || Type == OP_BUYLIMIT || Type == OP_SELLLIMIT)
if(OrderMagicNumber() == MagicNumber)
Print(ShortName +" (OrderDelete Error) "+ ErrorDescription(GetLastError()));
My question is, how can I keep this loop alive without the broker rejecting my orders for "too many requests"?
Should I leave a sleep() period between the OrderClose() calls? If so, how much?
I don't know what is considered too many requests.
But when an OrderClose fails, you should re-select the order before trying again.
Especially after a re-quote, OrderClosePrice will not be current.
I'm not gonna argue with you on that because I never investigate it
but IMHO OrderSelect() olds only the index of order pool or order ticket, and changes that occur on OrderClosePrice() doesn't matter (my 2¢)
int index, // index or order ticket
int select, // flag
int pool=MODE_TRADES // mode
but of course you have nothing to lose by selecting the order again
but i think that OrderSelect() olds only the index of order pool or order ticket, and changes that occur on OrderClosePrice() doesn't matter (my 2¢)
from the documentation
function copies order data into program environment and all further calls of OrderClosePrice(), OrderCloseTime(), OrderComment(), OrderCommission(), OrderExpiration(), OrderLots(), OrderMagicNumber(), OrderOpenPrice(), OrderOpenTime(), OrderPrint(), OrderProfit(), OrderStopLoss(), OrderSwap(), OrderSymbol(), OrderTakeProfit(), OrderTicket(), OrderType() functions return the data, copied earlier.
it's not just about the apology, it's also for the sake of the forum, if some day, someone is gonna search the forum about this...
RefreshRates updates the pre-defined variables and arrays
OrderClosePrice is not one of these.