Issue with Accessing Profit and Loss Values of Closed Trades in MQL5

 

Hello dear community members,

I'm facing an issue while writing an MQL5 script to access the profit and loss values of closed trades. In short, I've been trying to use the relevant functions to retrieve the profit and loss values, but I always end up getting 0.0 as the profit or loss value.

I've tested this code so far:

// This function is called when a new tick arrives

#include <Trade\Trade.mqh>
CTrade Trade;


// 
double addProfit = 0.0;

void OnTick()
{
  
  
  double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  double Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  // Check if we have an open trade
  if (PositionsTotal() == 0)
  {
    int ticket = Trade.Buy(0.01, _Symbol, Ask, Ask - (100 * _Point), Ask + (100 * _Point));
    
    ulong lastTicket = Trade.RequestPosition();
    double lastProfit = HistoryDealGetDouble(lastTicket, DEAL_PROFIT);
    Print("Profit added to addProfit: ", lastProfit);
    
   }
   
  int totalDeals = HistoryDealsTotal();
  
  for (int i = 0; i < 4; i++)
  {
   Print(HistoryDealGetTicket(i));
    ulong dealTicket = HistoryDealGetTicket(i);
    double dealProfit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
    double dealCommission = HistoryDealGetDouble(dealTicket, DEAL_COMMISSION);
    
    Print("Deall Ticket: ", dealTicket, " | Profit: ", dealProfit, " | Commission: ", dealCommission);
  }
}

  

If anyone has experience in this area and can offer assistance, please guide me. I would appreciate any insights or solutions you can provide.

Thank you all in advance for your help and support.

Best regards.

 
mansour afshar:

Hello dear community members,

I'm facing an issue while writing an MQL5 script to access the profit and loss values of closed trades. In short, I've been trying to use the relevant functions to retrieve the profit and loss values, but I always end up getting 0.0 as the profit or loss value.

I've tested this code so far:

If anyone has experience in this area and can offer assistance, please guide me. I would appreciate any insights or solutions you can provide.

Thank you all in advance for your help and support.

Best regards.

use "HistorySelect(0,TimeCurrent())" before you start calling the history.

 
Issam Kadhi #:

use "HistorySelect(0,TimeCurrent())" before you start calling the history.

Thank you, my issue has been resolved