Download MetaTrader 5

OrderClose(), requotes and time between orders - page 2

To add comments, please log in or register
Enable MQL5 Storage to store your source codes. It is free!
whroeder1
14489
whroeder1 2015.10.30 22:10  
And OrderClosePrice get's it's value from them. No change since 2012
Keith Watford
Moderator
9619
Keith Watford 2015.10.30 23:07  
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. 

qjol
3243
qjol 2015.11.01 05:40  

i can't confirm because my test was

if(OrderSelect(xxx, SELECT_BY_TICKET))
   {
   Alert(DoubleToString(OrderClosePrice(),Digits) + " " + DoubleToString(Ask,Digits));
   Sleep(60000);
   RefreshRates();
   Alert(DoubleToString(OrderClosePrice(),Digits) + " " + DoubleToString(Ask,Digits));
   }
Point Zero
795
Point Zero 2015.11.01 15:50  

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 price
                    if(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);
}
Keith Watford
Moderator
9619
Keith Watford 2015.11.01 22:26  

I ran this test

  if(OrderSelect(10427308,SELECT_BY_TICKET))
     {
     RefreshRates();
     Print("Initial OrderClosePrice = ",DoubleToStr(OrderClosePrice(),Digits)," Ask = ",DoubleToStr(Ask,Digits));
     }
  Sleep(60000);
  RefreshRates();
  Print("After RR() OrderClosePrice = ",DoubleToStr(OrderClosePrice(),Digits)," Ask = ",DoubleToStr(Ask,Digits));
  if(OrderSelect(10427308,SELECT_BY_TICKET))
     Print("After Re-select OrderClosePrice = ",DoubleToStr(OrderClosePrice(),Digits)," Ask = ",DoubleToStr(Ask,Digits));
  Sleep(60000);
  Print("After Sleep OrderClosePrice = ",DoubleToStr(OrderClosePrice(),Digits)," Ask = ",DoubleToStr(Ask,Digits));
  if(OrderSelect(10427308,SELECT_BY_TICKET))
     Print("After Re-select OrderClosePrice = ",DoubleToStr(OrderClosePrice(),Digits)," Ask = ",DoubleToStr(Ask,Digits));
  RefreshRates();
  Print("After RR() OrderClosePrice = ",DoubleToStr(OrderClosePrice(),Digits)," Ask = ",DoubleToStr(Ask,Digits));
  
  

 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. 

qjol
3243
qjol 2015.11.02 06:23  
@flaab int closed = false ?
Point Zero
795
Point Zero 2015.11.04 09:04  

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;

12
To add comments, please log in or register