Obtain profit from last closed deal (closed)

 

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!

 
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.

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

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

 

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.
 

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

sometime others prefere using from top to down :

for (int i = l_deals-1, i >= 0, i--)
 
zemo:
Amine, thank you for you answer this solved another code that was breaking my head... lol  the tip with "-1" as very useful
 for (int i = 0; i < l_deals; i++)

or

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

(upper one is prettier :P)

And I prefer using 0..(deals-1) to fill an array build with structs. So, MyDealsArray[0] is always the last (closed) deal.
 
Arthur Albano:

or

(upper one is prettier :P)

And I prefer using 0..(deals-1) to fill an array build with structs. So, MyDealsArray[0] is always the last (closed) deal.

<=

;-)