how to calculate the closed profit in history list?

 
#property copyright "test"
#property link      "http://www.metaquotes.net"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   double profit=0;
//----   
    for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      {
        if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
        if(OrderType()>2)
               profit=profit+OrderProfit();
      }
      
   Comment("profit= ",profit);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

the comment is not same as the profit in history list.

why ?

 
int OrdersHistoryTotal( ) 
Returns the number of closed orders in the account history loaded into the terminal. The history list size depends on the current settings of the "Account history" tab of the terminal
 
sergery:

the comment is not same as the profit in history list.

why ?

      if(OrderType()>2)
               profit=profit+OrderProfit();
      }
Do you really expect any pending orders to be in the history and why would they have profit?
 
WHRoeder:
Do you really expect any pending orders to be in the history and why would they have profit?

let remove if(OrderType()>2)
 
#property copyright "test"
#property link      "http://www.metaquotes.net"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   double profit=0;
//----   
    for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      {
        if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
                    profit=profit+OrderProfit();
      }
      
   Comment("profit= ",profit);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

the comments do not reflect the pfofit correctly yet

 
    for(int pos=0; pos < OrdersHistoryTotal(); pos++) if (
        OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL){// Avoid cr/bal https://www.mql5.com/en/forum/126192
profit += OrderProfit() + OrderCommission...
 

Thank you WHRoeder !

Now I got what I need.

if(OrderType()>2)--- this code should be if(OrderType()<=1)

#property copyright "test"
#property link      "http://www.metaquotes.net"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   double profit=0;
//----   
    for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      {
        if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
        if(OrderType()<=1)
               profit=profit+OrderProfit()+OrderSwap()+OrderCommission();
      }
      
   Comment("profit= ",profit);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Reason: