Download MetaTrader 5

Obtain profit from last closed deal (closed)

To add comments, please log in or register
Have you developed a profitable strategy? Share it with millions of traders via Signals!
Arturo Lopez Perez
161437
Arturo Lopez Perez 2013.11.04 10:13 

Good morning,

I am building a function to know if the last closed deal by an expert advisor was profitable. So I am iterating deals until finding one that matches the magic number, type and deal comment desired. However, I am having trouble using the HistorySelect() function, as I don't want to use any start/end date (I don't know if the last trade was recent or not). This is what I have so far.

bool LastPositionWon(ENUM_DEAL_TYPE Type, string COMMENT)
{  
   HistorySelect(0, 0); // Problem here!
   int l_deals = HistoryDealsTotal();
   for (int i = 0; i < l_deals; i++)
   { 
      // Ticket
      ulong l_ticket =  HistoryDealGetTicket(i);  
      
      // Magic number 
      int l_magic = (int) HistoryDealGetInteger(l_ticket, DEAL_MAGIC);
      
      // Type
      ENUM_DEAL_TYPE l_type = (ENUM_DEAL_TYPE) HistoryDealGetInteger(l_ticket, DEAL_TYPE);
      
      // Comment
      string l_comment = HistoryDealGetString(l_ticket, DEAL_COMMENT);
      
      // Entry type
      ENUM_DEAL_ENTRY entry_type = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(l_ticket, DEAL_ENTRY);
      
      // Evaluate and return
      if(entry_type == DEAL_ENTRY_OUT && l_type == Type && l_comment == COMMENT && l_magic == MagicNumber)
      {
         double profit = HistoryDealGetDouble(l_ticket, DEAL_PROFIT);
         if(profit > 0)
         {
            return(true); 
         } else { 
            return(false);
         }
      }
   }
   return(true);     
}

I am pretty sure there is an easier way to do this. Could you please point me in the right direction?

Thanks in advance!

Alain Verleyen
Moderator
30707
Alain Verleyen 2013.11.04 10:36  
PzTrading:

Good morning,

I am building a function to know if the last closed deal by an expert advisor was profitable. So I am iterating deals until finding one that matches the magic number, type and deal comment desired. However, I am having trouble using the HistorySelect() function, as I don't want to use any start/end date (I don't know if the last trade was recent or not). This is what I have so far.

I am pretty sure there is an easier way to do this. Could you please point me in the right direction?

Thanks in advance!

Of course if you select from 0 to 0, you don't get nothing. You have to select from 0 to current date, then loop from last to first and break (or return) when you have found your last winning deal. Let me know if you need more details.

As a side note, entry_type can also be DEAL_ENTRY_INOUT. Unless you are sure your EA don't use reversing of position.

Arturo Lopez Perez
161437
Arturo Lopez Perez 2013.11.04 10:49  
angevoyageur:

Of course if you select from 0 to 0, you don't get nothing. You have to select from 0 to current date.

Hi Angevoyageur,

Selecting all history is something I wanted to avoid as it seems memory consuming, I thought there could be an easier way, perhaps picking the last position alltogether.

Thanks for pointing me in the right direction ;)

Alain Verleyen
Moderator
30707
Alain Verleyen 2013.11.04 10:52  
PzTrading:

Hi Angevoyageur,

selecting all history is something I wanted to avoid as it seems memory consuming, I thought there could be an easier way. 

Thanks for pointing me in the right direction ;)

Of course it's time consuming, but you have to do it at least 1 time. Don't do this at each tick of course.

Once you have your last winning deal, you also have a date to start your search. You can also use OnTrade or OnTradeTransaction to check your deals "on the fly".

Arturo Lopez Perez
161437
Arturo Lopez Perez 2013.11.04 11:05  

angevoyageur:

You can also use OnTrade or OnTradeTransaction to check your deals "on the fly".

I see, thanks. Although it would not work if the Ea is loaded and unloaded, so I prefer using the other method.
Amine Ahsous
9411
Amine Ahsous 2016.10.24 07:52  

I know it's too late to comment but i'm commenting anyways for people who're coming in future just like I came now

There is a little mistake in    

for (int i = 0; i < l_deals; i++)

It should be

   for (int i = 0; i < l_deals-1; i++)

l_deals-1 as i starts from 0 and deals = history orders :) 

Thanks

BTW your code helped me a lot

zemo
82
zemo 2017.02.09 23:53  
Amine Ahsous:

I know it's too late to comment but i'm commenting anyways for people who're coming in future just like I came now

There is a little mistake in    

for (int i = 0; i < l_deals; i++)

It should be

   for (int i = 0; i < l_deals-1; i++)

l_deals-1 as i starts from 0 and deals = history orders :) 

Thanks

BTW your code helped me a lot

Amine, thank you for you answer this solved another code that was breaking my head... lol  the tip with "-1" as very useful
Alain Verleyen
Moderator
30707
Alain Verleyen 2017.02.10 07:59  
zemo:
Amine, thank you for you answer this solved another code that was breaking my head... lol  the tip with "-1" as very useful
Except this "-1" tip is a bad advice, you will miss the last deal.
Irwan Adnan
886
Irwan Adnan 2017.02.12 05:25  

sometime others prefere using from top to down :

for (int i = l_deals-1, i >= 0, i--)
To add comments, please log in or register