Text on the chart

 

Can anyone tell me how to report the profit as a sum of multiple orders that have the same stop loss and/or take profit on the chart as text? I'll give you the program I developed, but it doesn't work for me. On the contrary, it works in the tester, I didn't understand anything

void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result)
{//1
    // Seleziona tutti gli ordini storici fino al momento corrente (ultime 24 ore)
    datetime fromTime = TimeCurrent() - 24 * 3600;
    datetime toTime = TimeCurrent();

    if (!HistorySelect(fromTime, toTime)) {
        Print("Errore nella selezione della cronologia degli ordini.");
        return;
    }

    int totalOrders = HistoryDealsTotal();

    if (totalOrders == 0) {
        Print("Nessun ordine storico trovato.");
        return;
    }

    // Itera sugli ordini storici
    for (int i = 0; i < totalOrders; i++) 
    {//2
        ulong ticket = HistoryDealGetTicket(i); // Ottieni il ticket dell'ordine storico

        if (ticket)
                  
         {//3
            ulong ID=ChartID();
            Alert("ID Chart:",ID);
            string comment = HistoryDealGetString(ticket, DEAL_COMMENT);
            ENUM_DEAL_TYPE dealType = (ENUM_DEAL_TYPE)HistoryDealGetInteger(ticket, DEAL_TYPE);
            bool sopra = (dealType == DEAL_TYPE_BUY); // True se è un ordine buy, altrimenti sell
            double price = iClose(_Symbol, PERIOD_M1, 0) + 20 * _Point; // Usa l'ultimo prezzo di chiusura + offset
            double sl = HistoryDealGetDouble(ticket, DEAL_SL);
            double tp = HistoryDealGetDouble(ticket, DEAL_TP);
            double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
            long entry = HistoryDealGetInteger(ticket, DEAL_ENTRY);
            datetime time = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);

            // Verifica se è stato chiuso con SL o TP
            if ((StringFind(comment, "sl") >= 0 || StringFind(comment, "tp") >= 0) && (sl > 0 || tp > 0)) 
            {//4
                ENUM_DEAL_REASON closeReason = (ENUM_DEAL_REASON)HistoryDealGetInteger(ticket, DEAL_REASON);

                if (closeReason == DEAL_REASON_SL || closeReason == DEAL_REASON_TP) 
                {//5
                    
                                    profit += HistoryDealGetDouble(ticket,DEAL_COMMISSION)
                                    +HistoryDealGetDouble(ticket,DEAL_SWAP)
                                    +HistoryDealGetDouble(ticket,DEAL_PROFIT);
                                    Print("Ordine con SL e TP uguali trovato. Ticket: ", ticket, " Profitto aggiornato: ", profit);
                             
                    }//5
                }//4

                // Crea l'oggetto testo per visualizzare il profitto sulla chart
                string name = "TradeHistory_Deal_" + IntegerToString(ticket);

                // Elimina eventuale oggetto esistente con lo stesso nome
                if (ObjectFind(0, name) != -1) 
                {
                    ObjectDelete(0, name);
                }

                // Crea l'oggetto testo
                if (ObjectCreate(0, name, OBJ_TEXT, 0, time, price)) 
                {
                    ObjectSetString(0, name, OBJPROP_FONT, "Arial");
                    ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 14);
                    ObjectSetInteger(0, name, OBJPROP_BACK, true); // Mostra in sottofondo
                    ObjectSetInteger(0, name, OBJPROP_COLOR, clrYellow);
                    ObjectSetString(0, name, OBJPROP_TEXT, "Profit: " + DoubleToString(profit, 2));
                    ChartRedraw();
                } 
                else 
                {
                    Print("Errore nella creazione dell'oggetto testo per il ticket: ", ticket);
                }
            }//3
        }//2
    }//1
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

In future please attach your source code file as a valid MQL code file and not as a simples text file.

For now, I have included it in your post as an embed source code block (Alt-S). You can also use that method in the future.