Difficulty selecting the right history

 

Hi all,

i'm trying to use today's profit in an EA. What i have done so far works for Deals opened today;


   double TodaysProfit=0;
   HistorySelect(((TimeCurrent()/86400)*86400)+28800,TimeCurrent());
   int total=HistoryDealsTotal();
   for(int i=1;i<total;i++)
     {
      TodaysProfit=TodaysProfit+HistoryDealGetDouble(HistoryDealGetTicket(i),DEAL_PROFIT);
     }

However, this breaks when a Deal was opened yesterday, but closed today. This Deal will not get included in the profit calculation for today.

Any ideas on how to select for Deal closing time? HistorySelect() seems to only select for Deal opening time.


Another idea... the terminal does this calculation properly in the account history tab.

Maybe that value can somehow be accessed?


Thanks!
 
Warble68:

Hi all,

i'm trying to use today's profit in an EA. What i have done so far works for Deals opened today;


However, this breaks when a Deal was opened yesterday, but closed today. This Deal will not get included in the profit calculation for today.

Any ideas on how to select for Deal closing time? HistorySelect() seems to only select for Deal opening time.

You are confused. A deal has only 1 time, no "closing time" or "opening time", what you mean is deal at position close time and deal at position open time.

You have to select your today deals, check if there are out (close) deals. From these deals you can get a position ID, which will allow you to use HistorySelectByPosition() to retrieve all the deals of a position and calculate the profit.

Another idea... the terminal does this calculation properly in the account history tab.

Maybe that value can somehow be accessed?

No, you need to code it.

 
Alain Verleyen:

You are confused. A deal has only 1 time, no "closing time" or "opening time", what you mean is deal at position close time and deal at position open time.

You have to select your today deals, check if there are out (close) deals. From these deals you can get a position ID, which will allow you to use HistorySelectByPosition() to retrieve all the deals of a position and calculate the profit.

No, you need to code it.

 

Yes, you are right. I was confused about exactly how Deals work in MT5.

Was too lazy to try your suggestion to use HistorySelectByPosition() and tried this instead;


   HistorySelect(TimeCurrent()-500000,TimeCurrent());
   double accountprofit=0;
   int total=HistoryDealsTotal();
   for(int i=1;i<total;i++)
     {
      if(HistoryDealGetInteger(HistoryDealGetTicket(i),DEAL_TIME)>=((TimeCurrent()/86400)*86400)+28800)
         accountprofit=accountprofit+HistoryDealGetDouble(HistoryDealGetTicket(i),DEAL_PROFIT);
     }


This works as intended, but uses a little extra history data.

It also shows that DEAL_PROFIT only works if both the (in) and the (out) Deals are in the selected history.


Thanks for pointing me in the right direction!
 
Warble68:
 

Yes, you are right. I was confused about exactly how Deals work in MT5.

Was too lazy to try your suggestion to use HistorySelectByPosition() and tried this instead;



This works as intended, but uses a little extra history data.

It also shows that DEAL_PROFIT only works if both the (in) and the (out) Deals are in the selected history.


Thanks for pointing me in the right direction!

Well if you don't have position older than one (~) week, if it doesn't trade other symbosl, if magic number is not used and if a deal didn't trigger between TimeCurrent() and now... it will (almost see below) work...slowly.

   for(int i=1;i<total;i++)

Why are you skipping index 0 ?

If you was not lazy it would work better

 
Alain Verleyen:

Well if you don't have position older than one (~) week, if it doesn't trade other symbosl, if magic number is not used and if a deal didn't trigger between TimeCurrent() and now... it will (almost see below) work...slowly.

Why are you skipping index 0 ?

If you was not lazy it would work better


Thanks for catching this:


 for(int i=1;i<total;i++)


All those ifs do not apply in this case = lazy way :-)

 
Warble68:

Hi all,

Another idea... the terminal does this calculation properly in the account history tab.

Maybe that value can somehow be accessed?

...
#include <Trade\AccountInfo.mqh>
...
CAccountInfo m_account;
...
double accountBalance;
double ProfitLossOfEA = 0;
int OnInit()
{
...
accountBalance = m_account.Balance();
...
}
...
void OnTradeTransaction(const MqlTradeTransaction &trans,
        const MqlTradeRequest &request,
        const MqlTradeResult &result)
{
...
ProfitLossOfEA = m_account.Balance() - accountBalance;
...
}

I've used Balance() function of AccountInfo object to keep track of EA's P/L and it is generally simpler than to keep track of positions and deals. 

Reason: