HistoryDealGetDouble(ticket, DEAL_PROFIT) always = 0.0?

 

Hi all,

My funtion:

double ProfitCheck(long const magic_number)
{
   double profit=0.0;
   HistorySelect(0, TimeCurrent());
   int trades_total = HistoryDealsTotal();
   
   for(int i=0; i < trades_total; i++)
     {
      long ticket = HistoryDealGetTicket(i);
      long type   = HistoryDealGetInteger(ticket, DEAL_TYPE);

      //--- Initial deposit is not considered
      if((i == 0)&&(type == DEAL_TYPE_BALANCE)) continue;
      
      if((type != DEAL_TYPE_BUY)&&(type != DEAL_TYPE_SELL)) continue;

      if( magic_number != HistoryDealGetInteger(ticket, DEAL_MAGIC)) continue;

      //--- Calculation of profit
      Print(   "Ticket: "        + ticket + 
               " type: "         + type + 
               " DEAL_SYMBOL: "  + HistoryDealGetString(ticket,DEAL_SYMBOL) + 
               " DEAL_VOLUME: "  + HistoryDealGetDouble(ticket, DEAL_VOLUME) +
               " DEAL_PROFIT: "  + HistoryDealGetDouble(ticket, DEAL_PROFIT) +
               " DEAL_COMMISSION: " + HistoryDealGetDouble(ticket, DEAL_COMMISSION) +
               " DEAL_SWAP: "    + HistoryDealGetDouble(ticket, DEAL_SWAP));
      
      profit =+ HistoryDealGetDouble(ticket, DEAL_PROFIT) +
               HistoryDealGetDouble(ticket, DEAL_COMMISSION) +
               HistoryDealGetDouble(ticket, DEAL_SWAP);
               
     }
   return(profit);        
}

I backtest, but it's always =0.


Please help me!

Thanks!

 

remove it

 if((type != DEAL_TYPE_BUY)&&(type != DEAL_TYPE_SELL)) continue;
 
Nguyen Nga:

remove it

Thanks! But it's not work

I want to calculate the total profit for all market deal.

Please help me!

 
Don't use Magic Number filter for testing. Only for live trading. Regards.
 

It is very strange that the classes work differently when tested and run in production mode. All the same, we are operating here with money, and not something else.

 
Hung Dinh Van :

Thanks! But it's not work

I want to calculate the total profit for all market deal.

Please help me!

You need to look for market exit trades (DEAl_ENTRY_INOUT and DEAL_ENTRY_OUT).

 

falto cargar el historial

  datetime from_date=0;  

  datetime to_date=TimeCurrent();

  HistorySelect(from_date,to_date);

 

Hello everybody, see at my solution which works fine as a function to display the profit of last order closed.


double LastResult(uint nbHist, ulong magic)

   {

     double orderResult = 0;

     if (nbHist > 0)

      {

         for (int i = nbHist - 1; i >= 0; i--)    

          {

              ulong ticket = HistoryDealGetTicket(i);

              HistoryDealGetTicket(ticket);

         

              if (HistoryDealGetDouble(ticket,DEAL_PROFIT))

              {

                if ((magic > 0 && HistoryDealGetInteger(ticket,DEAL_MAGIC) != magic) || HistoryDealGetString(ticket,DEAL_SYMBOL) != _Symbol)

                      continue;

                orderResult = HistoryDealGetDouble(ticket, DEAL_PROFIT)+

                              HistoryDealGetDouble(ticket, DEAL_COMMISSION) +

                              HistoryDealGetDouble(ticket, DEAL_SWAP);

                break; // Exit the loop once the last matching trade is found           

              }

          }

      }

      return(orderResult); 

   }

/* don't forget put these includes in the main structure :

#include <Trade\DealInfo.mqh>

#include <Trade\HistoryOrderInfo.mqh>

*/