Select the most recently closed order from History ?

 

Hi guys,

After being filtered by Symbol, MagicN, Negative or positive Profit, is it possible to select the most recently closed order, I mean the last one closed among the others.

Thanks for your help.

 

Hi,

For Mql4 you can use :

ticket = OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY);
 
Mounir Cheikh: For Mql4 you can use :
ticket = OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY);
No you can't.
  1. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

 
bool SelectMostRecentClosed(int magic_number=0)
{
   int ticket = -1;
   datetime close_time = 0;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
      && OrderSymbol()==_Symbol 
      && (magic_number==0 || OrderMagicNumber() == magic_number) 
      && OrderCloseTime() > close_time)
      {
         ticket = OrderTicket();
         close_time = OrderCloseTime();
      }
   }
   return OrderSelect(ticket,SELECT_BY_TICKET);
}
 
nicholishen:

Think with me, this is just "Closed order" not "the most recently closed"

Let's further explain it :

- Order 1 closed 60 mn ago

- Order 2 closed 10 mn ago

- Order 3 closed 8 mn ago

- Order 4 closed 1 mn ago // This is the one must be selected.

 
Toufik:

Think with me, this is just "Closed order" not "the most recently closed"

Let's further explain it :

- Order 1 closed 60 mn ago

- Order 2 closed 10 mn ago

- Order 3 closed 8 mn ago

- Order 4 closed 1 mn ago // This is the one must be selected.


Perhaps you are having issues with the English translation... 

This...

- Order 4 closed 1 mn ago // This is the one must be selected.

...is the most recently closed order.

It also is the order with the highest time-stamp value by close-time.

 
nicholishen:

Perhaps you are having issues with the English translation... 

This...

...is the most recently closed order.

It also is the order with the highest time-stamp value by close-time.

No, it's not a language issue

I can't see how Order4 can be selected in your code... 

    && OrderCloseTime() > close_time)

This selection includes all closed orders ins't it ?

 
Toufik:

No, it's not a language issue

I can't see how Order4 can be selected in your code... 

This selection includes all closed orders ins't it ?


nicholishen's code loops through all 4 orders. It compares the OrderCloseTime() with a variable called close_time.

If it is more recent than "close_time" it updates the value of close_time and saves the ticket number.

Add some print statements so you can watch it in action.

 

Fantastic!

Thank you nicholishen.

Thank you honest_knave

 
nicholi shen:
bool SelectMostRecentClosed(int magic_number=0)
{
   int ticket = -1;
   datetime close_time = 0;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
      && OrderSymbol()==_Symbol 
      && (magic_number==0 || OrderMagicNumber() == magic_number) 
      && OrderCloseTime() > close_time)
      {
         ticket = OrderTicket();
         close_time = OrderCloseTime();
       break; // 
      }
   }
   return OrderSelect(ticket,SELECT_BY_TICKET);
}
add this break, this means if it meets the last closed trade it exits the loop
 
Lucky Minayo:

This is a mistake. With the added "break" we don't check all the History list and possibly miss the correct Most Recent Order Closed!

Better keep the correct answer given by nicholi shen.

Reason: