Why is that?

 

Hello.

The following function returns all properties correctly (DEAL_VOLUME, DEAL_TIME... etc), with exception of DEAL_PROFIT.

double GetDealProfit(int num,int way,bool isout)
{
   ulong ticket;
   ulong magic;
   string symbol;
   double DealProfit=0;
   
   if(HistorySelect(0,TimeCurrent()))
     {
      for(int i=HistoryDealsTotal()-1; i>=0; i--)
        {
         ticket=HistoryDealGetTicket(i);
         symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
         magic=HistoryDealGetInteger(ticket,DEAL_MAGIC);
               
         if(  ((isout && HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT) || (!isout && HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_IN))  
         && ((way==1 && HistoryDealGetInteger(ticket,DEAL_TYPE)==DEAL_TYPE_BUY) || (way==-1 && HistoryDealGetInteger(ticket,DEAL_TYPE)==DEAL_TYPE_SELL))
         && symbol==Symbol() && magic==MagicNumber)
           {
             if (--num==0)
             {
               DealProfit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
               break;
            }
           }
        }
     }
   
   return(DealProfit);
        
}
 
To find your mistake, throw out all unnecessary: 'isout', 'way', 'num' ...
 
Vladimir Karputov #:
To find your mistake, throw out all unnecessary: 'isout', 'way', 'num' ...

Hi Vladimir.

I tried that even before posting, and the problem persisted.

 
Drake # :

Hi Vladimir.

I tried that even before posting, and the problem persisted.

You haven't done anything. Throw away all unnecessary things.

 
//+------------------------------------------------------------------+
//| Profit for the period                                            |
//+------------------------------------------------------------------+
double ProfitForPeriod(const datetime from_date,const datetime to_date)
  {
   double result=0.0;
//--- request trade history
   HistorySelect(from_date,to_date);
//---
   uint     total=HistoryDealsTotal();
   ulong    ticket=0;
   long     position_id=0;
//--- for all deals
   for(uint i=0; i<total; i++) // for(uint i=0;i<total;i++) => i #0 - 2016, i #1045 - 2017
     {
      //--- try to get deals ticket
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         //--- get deals properties
         long deal_type          =HistoryDealGetInteger(ticket,DEAL_TYPE);
         long deal_entry         =HistoryDealGetInteger(ticket,DEAL_ENTRY);
         long deal_magic         =HistoryDealGetInteger(ticket,DEAL_MAGIC);
         double deal_commission  =HistoryDealGetDouble(ticket,DEAL_COMMISSION);
         double deal_swap        =HistoryDealGetDouble(ticket,DEAL_SWAP);
         double deal_profit      =HistoryDealGetDouble(ticket,DEAL_PROFIT);
         string deal_symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
         //--- only for current symbol and magic
         if(deal_magic==InpMagic && deal_symbol==m_symbol.Name())
            if(ENUM_DEAL_ENTRY(deal_entry)!=DEAL_ENTRY_IN)
               if(ENUM_DEAL_TYPE(deal_type)==DEAL_TYPE_BUY || ENUM_DEAL_TYPE(deal_type)==DEAL_TYPE_SELL)
                  result+=(deal_commission+deal_swap+deal_profit);
        }
     }
//---
   return(result);
  }

An example of working with a trading history:

 
Vladimir Karputov #:

You haven't done anything. Throw away all unnecessary things.

How can you affirm that I didn't do anything after I said that I did it? That's kinda rude, or maybe your crystal ball is broken.

Yes, I tried removing these "unnecessary things". Anyhow, that's not the point. My question was:

Why my function returns all the properties correctly (even with the "unnecessary things"), and the only property that is not returned correctly is the DEAL_PROFIT?

Do you think you're able to answer my question without showing me another solution / function? Thank you anyways.

  

 
Drake # :


  

I give advice for the third and last time: throw out all unnecessary things. You still haven't got the corrected code. You don't want to do anything.

 

Also throw out 'break'

//+------------------------------------------------------------------+
//| Get Deals Profit                                                 |
//+------------------------------------------------------------------+
double GetDealsProfit(void)
  {
   ulong ticket;
   double profit=0.0;
   if(HistorySelect(0,TimeCurrent()))
     {
      for(int i=HistoryDealsTotal()-1; i>=0; i--)
        {
         if((ticket=HistoryDealGetTicket(i))>0)
           {
            profit+=HistoryDealGetDouble(ticket,DEAL_PROFIT);
           }
        }
     }
   return(profit);
  }
 
Vladimir Karputov #:

An example of working with a trading history:

if(ENUM_DEAL_TYPE(deal_type)==DEAL_TYPE_BUY && ENUM_DEAL_TYPE(deal_type)==DEAL_TYPE_SELL)

If I am not wrong, this should be like this:


if(ENUM_DEAL_TYPE(deal_type)==DEAL_TYPE_BUY || ENUM_DEAL_TYPE(deal_type)==DEAL_TYPE_SELL)
 
Dominik Egert # :

If I am not wrong, this should be like this:


Yes! You are well done! Nobody else noticed the error!

Reason: