Download MetaTrader 5

OrderClose(), requotes and time between orders

To add comments, please log in or register
Point Zero
793
Point Zero  

Hello everyone,

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;
                 while(!closed)
                 {
                    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)
                 {
               if(!OrderDelete(OrderTicket(), Gold))
               {
                  Print(ShortName +" (OrderDelete Error) "+ ErrorDescription(GetLastError()));
                  return(false);
               }
            }
         }
      }
   }
   return(true);
}

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?

Thanks

qjol
3243
qjol  
i would try a Sleep() for a few seconds after 2-3 requotes and increasing it after 5-6 not to get "too many requests"
Point Zero
793
Point Zero  
qjol:
i would try a Sleep() for a few seconds after 2-3 requotes and increasing it after 5-6 not to get "too many requests"
Hi Q! Thanks for your message. Are there are official mql4 guidelines for this? What is considered "too many" request per unit of time? Thanks!
Keith Watford
Moderator
9641
Keith Watford  

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. 

qjol
3243
qjol  
GumRai:

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¢) 

bool  OrderSelect(
   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

Keith Watford
Moderator
9641
Keith Watford  
qjol:

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

The OrderSelect() 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 is possibly due to not re-selecting the order after a re-quote, that flaab gets so many additional re-quotes. 
qjol
3243
qjol  
@ GumRai you were right, just checked it out (with a script i ran), Order must be re-selected, my mistake, sorry.
Keith Watford
Moderator
9641
Keith Watford  
qjol:
@ GumRai you were right, just checked it out (with a script i ran), Order must be re-selected, my mistake, sorry.
No need to apologise. We all learn from each other.
qjol
3243
qjol  

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...

best Regards

whroeder1
14778
whroeder1  
GumRai: But when an OrderClose fails, you should re-select the order before trying again.
Especially after a re-quote, OrderClosePrice will not be current.
You must RefreshRates and then reselect the order for OrderClosePrice to be current.
Keith Watford
Moderator
9641
Keith Watford  
WHRoeder:
GumRai: But when an OrderClose fails, you should re-select the order before trying again.
Especially after a re-quote, OrderClosePrice will not be current.
You must RefreshRates and then reselect the order for OrderClosePrice to be current.

RefreshRates updates the pre-defined variables and arrays

OrderClosePrice is not one of these. 

12
To add comments, please log in or register