Get profit of last two closed positions

 

I have the following code to get profit of last two closed positions i can get the profit of last position but not the second last one any ideas on how to get profit from last two positions in mql5

void OnTrade()
{
        static int previous_open_positions = 0;
        int current_open_positions = PositionsTotal();
        if(current_open_positions < previous_open_positions)             // a position just got closed:
        {
                previous_open_positions = current_open_positions;
                HistorySelect(TimeCurrent()-300, TimeCurrent()); // 5 minutes ago
                int All_Deals = HistoryDealsTotal();
                if(All_Deals < 1) Print("Some nasty shit error has occurred :s");
                // last deal (should be an DEAL_ENTRY_OUT type):
                ulong temp_Ticket = HistoryDealGetTicket(All_Deals-1); 
                // here check some validity factors of the position-closing deal 
                // (symbol, position ID, even MagicNumber if you care...)
                double LAST_TRADE_PROFIT = HistoryDealGetDouble(temp_Ticket , DEAL_PROFIT);
                Print("Last Trade Profit : ", DoubleToString(LAST_TRADE_PROFIT));
        }
        else if(current_open_positions > previous_open_positions)       // a position just got opened:
                previous_open_positions = current_open_positions; 
}
 
#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

input int inAmountLastOrders = 2;

void OnTrade()
{
  static int PrevTotal = 0;
  const int NewTotal = OrdersHistoryTotal();
  
  if (PrevTotal < NewTotal)
  {
    const int FirstIndex = NewTotal - inAmountLastOrders;
    
    for (int i = NewTotal - 1; i >= FirstIndex; i--)
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      {
        OrderPrint();
        Print("Last (" + (string)i + ") Trade Profit : ", DoubleToString(OrderProfit(), 2));
      }
    
    PrevTotal = NewTotal;
  }  
}
 
fxsaber:

Thanks mate it works

 
Does this give Last trade profit of current symbol or overall last traded pair profit?
 
True. The function has no filtering.