Select the most recently closed order from History ?

To add comments, please log in or register
Toufik
147
Toufik  

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.

Mounir Cheikh
2129
Mounir Cheikh  

Hi,

For Mql4 you can use :

ticket = OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY);
William Roeder
19988
William Roeder  
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

nicholi shen
2391
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();
      }
   }
   return OrderSelect(ticket,SELECT_BY_TICKET);
}
Toufik
147
Toufik  
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.

nicholi shen
2391
nicholi shen  
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.

Toufik
147
Toufik  
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 ?

honest_knave
Moderator
2308
honest_knave  
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.

Toufik
147
Toufik  

Fantastic!

Thank you nicholishen.

Thank you honest_knave

Lucky Minayo
681
Lucky Minayo  
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
To add comments, please log in or register