How to get the Total Order number for a particular magicNum?

 
I have two different EA robots, each has its own NumMagin separately.
ulong    magicNum = 123477;//Magic Number
      trade.SetExpertMagicNumber(magicNum);

I want to know the total separate value of each EA, which runs individually.
I'm using a void function, which looks for this history of each operation, and separated it using the HistoryDealGetInteger (ticket, DEAL_MAGIC) == magicNum ... but even so it only shows the grand total of everything that happened in MT5 operations, and in In fact, what I need is the total of each EA running separately and individually, because I placed each EA a different maginNum.


Example:

EA 01 - Wheel at Indice M1 operates crossing of medias
  MaginNum = 01 --- Total Operations Today = 01

EA 02 - Wheel on the M1 Index operates break
  MaginNum = 02 --- Total Operations Today = 05


Now if I call the HistoryDealsTotal function it returns the sum of the total of all EAs, in this case it would be = 06, but I want each EA to show its corresponding total number individually.
Wrong Result, HistoryDealsTotal () = 06

What I need is the total for each EA:
 TotalEA 01 = 01

 TotalEA 02 = 05


Function code, below:


void HistoryToday(){
   
    ///--- SELECT CURRENT HISTORY TODAY
   datetime time_start = iTime(_Symbol, PERIOD_D1, 0);

   if(HistorySelect(time_start, TimeCurrent()) )
     {
      hTodayProfitTotal = 0.0;
      hTodayTotal = 0;
      hTodayPositive = 0;
      
      //// --- LOOP 
      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
        {
         const ulong ticket = HistoryDealGetTicket(i);
		
	/// Separate via magicNum EA
         if(HistoryDealGetString(ticket, DEAL_SYMBOL) == _Symbol && HistoryDealGetInteger(ticket, DEAL_MAGIC) == magicNum)
           {
               double results = HistoryDealGetDouble(ticket,DEAL_PROFIT);
               
                hTodayTotal = HistoryDealsTotal();
               
                
            if(HistoryDealGetDouble(ticket, DEAL_PROFIT) > 0.0) {

               hTodayPositive ++; 
               hTodayProfitTotal += results;

               }else{

               hTodayProfitTotal += results;

               } 
            }
         }
     }else{
      Print("Error retrieving operations history ...");
     }
     
      /// --- RESULTADO 
      printf("-- Total: "+hTodayTotal+" --- Positive Operations: "+hTodayPositive+" --- Balance: "+hTodayProfitTotal);
      
  }
 

Example:

//+------------------------------------------------------------------+
//|                     Total transactions today by Magic number.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//+------------------------------------------------------------------+
//| Structure History Deals                                          |
//+------------------------------------------------------------------+
struct STRUCT_HISTORY_DEALS
  {
   long              magic;                  // Magic number
   int               count_buy;              // Number of Buy deals
   int               count_sell;             // Number of Sell deals
   //--- Constructor
                     STRUCT_HISTORY_DEALS()
     {
      magic                      = 0;
      count_buy                  = 0;
      count_sell                 = 0;
     }
  };
STRUCT_HISTORY_DEALS SHistoryDeals[];
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   RequestTradeHistory(SHistoryDeals);
   int size=ArraySize(SHistoryDeals);
   for(int i=0; i<size; i++)
     {
      Print(IntegerToString(SHistoryDeals[i].magic),
            ", BUY: ",IntegerToString(SHistoryDeals[i].count_buy),
            ", SELL: ",IntegerToString(SHistoryDeals[i].count_sell));
     }
  }
//+------------------------------------------------------------------+
//| Request trade history                                            |
//+------------------------------------------------------------------+
void RequestTradeHistory(STRUCT_HISTORY_DEALS &history_deals[])
  {
//--- request trade history
   datetime from_date=iTime(Symbol(),PERIOD_D1,0);
   datetime to_date=TimeCurrent()+86400;
   HistorySelect(from_date,to_date);
   uint total_deals=HistoryDealsTotal();
   ulong ticket_history_deal=0;
   if(total_deals>0)
     {
      ArrayFree(history_deals);
      //--- for all deals
      for(uint i=0; i<total_deals; i++)
        {
         //--- try to get deals ticket_history_deal
         if((ticket_history_deal=HistoryDealGetTicket(i))>0)
           {
            long     deal_ticket       = HistoryDealGetInteger(ticket_history_deal,DEAL_TICKET);
            long     deal_type         = HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);
            if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
              {
               long     deal_entry        = HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);
               if(deal_entry==DEAL_ENTRY_IN)
                 {
                  long     deal_magic        = HistoryDealGetInteger(ticket_history_deal,DEAL_MAGIC);
                  //---
                  int size=ArraySize(history_deals);
                  bool find=false;
                  for(int j=0; j<size; j++)
                    {
                     if(history_deals[j].magic==deal_magic)
                       {
                        if(deal_type==DEAL_TYPE_BUY)
                           history_deals[j].count_buy=history_deals[j].count_buy+1;
                        if(deal_type==DEAL_TYPE_SELL)
                           history_deals[j].count_sell=history_deals[j].count_sell+1;
                        continue;
                       }
                    }
                  if(!find)
                    {
                     ArrayResize(history_deals,size+1);
                     history_deals[size].magic=deal_magic;
                     if(deal_type==DEAL_TYPE_BUY)
                        history_deals[size].count_buy=1;
                     if(deal_type==DEAL_TYPE_SELL)
                        history_deals[size].count_sell=1;
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+


Secret: you need to use custom structure 'STRUCT_HISTORY_DEALS'.  The structure for each 'Magic number' contains the number of BUY and SELL deals