history of MT5 anyone can explain me ?

 

Hi i have  this code  for  extract the history of the day 

double CalculateDayTotal(string symbol = NULL)
{
    // Ottieni mezzanotte di oggi (inizio del giorno)
    datetime today = DateOfDay(TimeCurrent());
    datetime now = TimeCurrent();
//Print("today  ",today);
//Print("now      ",now);
    //PrintFormat("CalculateDayTotal: seleziono deal da %s a %s", 
     //  TimeToString(today, TIME_DATE|TIME_SECONDS), TimeToString(now, TIME_DATE|TIME_SECONDS));

    // Seleziona la cronologia dei deal dal giorno di oggi fino ad ora
    if(!HistorySelect(today, now))
    {
        Print("CalculateDayTotal: ERRORE HistorySelect");
        return 0.0;
    }

    double totalVolume = 0.0;
    int deals = HistoryDealsTotal();

    //PrintFormat("CalculateDayTotal: deals trovati = %d", deals);

    for(int i = 0; i < deals; i++)
    {
        ulong deal_ticket = HistoryDealGetTicket(i);
       // Print ("I TICKET ",deal_ticket);
        if(deal_ticket == 0) 
            continue; // sicurezza

        datetime deal_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);

        if(deal_time < today) 
        {
          //  PrintFormat("Deal #%llu escluso (data precedente): %s", deal_ticket, TimeToString(deal_time, TIME_DATE|TIME_SECONDS));
            continue; // escludi deal precedenti a oggi
        }

        string deal_symbol = HistoryDealGetString(deal_ticket, DEAL_SYMBOL);

        // Filtro per simbolo se specificato (NULL = tutti)
        if(symbol != NULL && deal_symbol != symbol)
        {
          //  PrintFormat("Deal #%llu escluso (simbolo %s non corrisponde a %s)", deal_ticket, deal_symbol, symbol);
            continue;
        }

        // Considera solo deal di tipo acquisto o vendita (BUY/SELL)
        ENUM_DEAL_TYPE deal_type = (ENUM_DEAL_TYPE)HistoryDealGetInteger(deal_ticket, DEAL_TYPE);
        if(deal_type != DEAL_TYPE_BUY && deal_type != DEAL_TYPE_SELL)
        {
        //    PrintFormat("Deal #%llu escluso (tipo deal %d non BUY/SELL)", deal_ticket, deal_type);
            continue;
        }

        double deal_volume = HistoryDealGetDouble(deal_ticket, DEAL_VOLUME);

       // PrintFormat("Deal #%llu incluso - Symbol: %s, Type: %d, Volume: %.2f, Time: %s", 
       //     deal_ticket, deal_symbol, deal_type, deal_volume, TimeToString(deal_time, TIME_DATE|TIME_SECONDS));

        totalVolume += deal_volume;
    }

    //PrintFormat("CalculateDayTotal: totale volumi = %.2f", totalVolume);
    return totalVolume;
}

initially i suppose it work good  but , hey murphy law  is  behind  the corner  ahhahah ,  return me  always  worng  total of the day , but i try to export   in xml  for find a ticket or date  but with my surprise i notice that Metatrader not create a classical report  with ticket side  etc... but mix them all , i dont know  why complicate  a simply things  but , more complicate is much better  ahahahaha , example:

i find operation date 2025.05.26 09:37:31

2025.05.26 09:37:31     8421178 EURUSD  sell    0.1      1,14107                 1,14103        2025.05.26 09:40:04      1,14147        - 0,70  0,00    - 4,00 

and this

2025.05.26 09:37:31     8421178 EURUSD  sell    0.1 / 0.1       market                  2025.05.26 09:37:31     filled   

and this

2025.05.26 09:37:31     8165316 EURUSD  sell    in      0.1      1,14107        8421178 - 0,70  0,00    0,00    0,00    1 156,73


always the same  trade  but one trade  Metatrader  split in 3 or many line why this complication ?o_O, but the strange effect is  when i call  history not return a correct value anyone have some idea ?  thanks

 
Stefano Cerbioni:

MetaTrader records each trade as multiple events (deals), including entry, exit, commissions, swaps, etc., so a single trade can appear as several lines. If you simply sum all BUY or SELL deals without proper filtering, it's normal to get incorrect totals.

To avoid duplicates or irrelevant entries, it's best to also filter by DEAL_ENTRY == DEAL_ENTRY_IN, which ensures you only count actual market entries. This excludes commissions and partial closures, giving you a more accurate volume.

 
Stefano Cerbioni:

Metatrader not create a classical report  with ticket side  etc... but mix them all

https://www.mql5.com/en/code/47816

MT4Orders QuickReport
MT4Orders QuickReport
  • www.mql5.com
Fast JavaScript version of Report library from fxsaber for MT4-style trading commands implemented via MT4Orders or Virtual. Works up to 10 times faster, NTML file size is smaller, can upload and display up to 5.4 million report lines.