Total Profit of Closed Trades by Symbol and Magic Number in MQL5

 

Hello,

The fonction I've written bellow should count the total profit filtered by current symbol and magic number but not working...

please help , thank you in advance 

//+------------------------------------------------------------------+
//|                                             ProfitHistory EA.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\DealInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>

//+------------------------------------------------------------------+
//| Exported variables                                               |
//+------------------------------------------------------------------+
input int MagicNumber=2024;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   Comment(ProfitHist());
   Print(ProfitHist());
  }

//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+
double ProfitHist(ENUM_POSITION_TYPE type=-1)
  {
   double result=0;
   if(HistorySelect(0,TimeCurrent()))
     {
      int total=HistoryDealsTotal();
      for(int i=0;i<total;i++)
        {
         CTrade trade;
         trade.SetExpertMagicNumber(MagicNumber);

         ulong    tiket=HistoryDealGetTicket(i);
         datetime tm=datetime(HistoryDealGetInteger(tiket,DEAL_TIME));
         string   sy=HistoryDealGetString(tiket,DEAL_SYMBOL);
         double   pt=HistoryDealGetDouble(tiket,DEAL_PROFIT),
                  sw=HistoryDealGetDouble(tiket,DEAL_SWAP),
                  cs=HistoryDealGetDouble(tiket,DEAL_COMMISSION);
         long     mn=HistoryDealGetInteger(tiket,DEAL_MAGIC),
                  ty=HistoryDealGetInteger(tiket,DEAL_TYPE),
                  en=HistoryDealGetInteger(tiket,DEAL_ENTRY);

         if(en==DEAL_ENTRY_OUT)
           {
            if(sy==Symbol()&&mn==MagicNumber&&(ty==type||type==-1))
              {
               result+=pt+sw+cs;
              }
           }
        }
     }
   return(result);
  }
//+------------------------------------------------------------------+



.

The Optimal Method for Calculation of Total Position Volume by Specified Magic Number
The Optimal Method for Calculation of Total Position Volume by Specified Magic Number
  • www.mql5.com
The problem of calculation of the total position volume of the specified symbol and magic number is considered in this article. The proposed method requests only the minimum necessary part of the history of deals, finds the closest time when the total position was equal to zero, and performs the calculations with the recent deals. Working with global variables of the client terminal is also considered.
Files:
 
DEAL_TYPE is of ENUM_DEAL_TYPE not ENUM_POSITION_TYPE.
 
Yashar Seyyedin #:
DEAL_TYPE is of ENUM_DEAL_TYPE not ENUM_POSITION_TYPE.
thanks
and yes you're right, but the problem still exists 
 
hassan moussebbih #:
thanks
and yes you're right, but the problem still exists 

It is working correctly for me even without the above fix.

 
Yashar Seyyedin #:

It is working correctly for me even without the above fix.

🤦‍♂️
Indeed the code is correct, and I definitely made a mistake in reviewing the results.
Thanks again.
Reason: