Profit calculation of closed orders "HELP" - page 7

 
Sergey Gritsay:
And where does it all exist? Do you write it somewhere in a variable or an array? Show me where you store it first, then you can see what to do next.
Not anywhere. You need to create it all competently and collect the profit data.
 
Natashe4ka:

OK, then let's break it down: there is a closing time of orders, there is a ticket or number of orders that coincide with the closing time. How do we combine and calculate the profit of these orders?
It seems to be easy, but I still don't understand it.

So far nobody can figure out which ones are highlighted in red. We need the exact criteria to find these orders. Therefore, try to explain, as they say, on the fingers, with pictures, etc.
 
Sergey Gritsay:
So far no one can figure out which ones are highlighted in red. We need the exact criteria to find these orders. So try to explain, as they say on the fingers, with pictures and so on.

?

Why can't anyone understand this, you probably mean yourself?

Everything has been clear for a long time now.

Madam just wants to do it on her own, without flaunting the details of her program.

 
Sergey Gritsay:
So far no one can understand which ones are highlighted in red. We need accurate criteria for finding these orders. For this reason, try to explain, as they say on the fingers, with pictures and so on

Yes, I have already given a 100% working condition and attached it as ready-made code. You should also note that the code uses a method that is not affected by terminal restarts, and nothing will be lost when the terminal is switched on and will continue to work. But here they invent a wheel which will only work in the tester.

Sergey, also note that in the original TS code when you close a series of 5 positions, the order in the middle of closing is also deleted, this in turn will not lead to anything good in real trading. In general, everything is clear to everyone, except for TS.

 
I think I understand what Natalia wants to get
//+------------------------------------------------------------------+
//|                                                   Natashe4ka.mq4 |
//|                                                   Sergey Gritsay |
//|                         https://www.mql5.com/ru/users/sergey1294 |
//+------------------------------------------------------------------+
#property copyright "Sergey Gritsay"
#property link      "https://www.mql5.com/ru/users/sergey1294"
#property version   "1.00"
#property strict

input int Magic=1;//Identification number
double Profit=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   color ProfitColor=0;

// START //
   LastProfitCL_1(Profit,TimeCurrent(),-1);
  
   if(Profit<0)ProfitColor=clrRed;
   else if(Profit>0)ProfitColor=clrLimeGreen;
   else ProfitColor=clrDarkGray;
  
   ObjectCreate("Last Profit",OBJ_LABEL,0,0,0);
   ObjectSet("Last Profit",OBJPROP_CORNER,1);
   ObjectSet("Last Profit",OBJPROP_XDISTANCE,5);
   ObjectSet("Last Profit",OBJPROP_YDISTANCE,15);
   ObjectSetText("Last Profit",StringConcatenate("Last Profit: ",DoubleToStr(Profit,2)),10,"Arial",ProfitColor);
// END START //

  }
//+------------------------------------------------------------------+
//Вариант 1
//+------------------------------------------------------------------+
bool LastProfitCL_1(double &LastProfit,//сюда записываем профит
                  datetime timecurent,// текущее время
                  int op=-1//"op" позиция (-1 любая позиция)                
                  )
  {
   double profit=0;
   int cnt=0;
   datetime timecurents=0;
   int total=OrdersHistoryTotal();

   for(int i=total-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(OrderType()>1)continue;// исключим удаленные отложенные ордера
      if(OrderCloseTime()!=timecurent)continue;
      if(OrderType()==op || op==-1)
        {
         profit+=OrderProfit()+OrderCommission()+OrderSwap();
         cnt++;
        }
     }
   if(cnt!=0)
     {
      LastProfit=profit;
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|   Вариант 2                                                               |
//+------------------------------------------------------------------+
bool LastProfitCL_2(double &LastProfit,//сюда записываем профит
                  datetime timecurent,// текущее время
                  int op=-1//"op" позиция (-1 любая позиция)                
                  )
  {
   double profit=0;
   int cnt=0;
   datetime timecurents=0;
   int total=OrdersHistoryTotal();
   if(OrderSelect(total-1,SELECT_BY_POS,MODE_HISTORY))
     {
      timecurents=OrderCloseTime();
     }
   for(int i=total-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(OrderCloseTime()<timecurents)continue;
      if(OrderType()>1)continue;// исключим удаленные отложенные ордера
      if(OrderType()==op || op==-1)
        {
         profit+=OrderProfit()+OrderCommission()+OrderSwap();
         cnt++;
        }
     }
   if(cnt!=0)
     {
      LastProfit=profit;
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
....
 

Corrected the second version of the function

bool LastProfitCL_2(double &LastProfit,//сюда записываем профит
                    datetime timecurent,// текущее время
                    int op=-1//"op" позиция (-1 любая позиция)                
                    )
  {
   double profit=0;
   int cnt=0;
   datetime timecurents=0;
   int total=OrdersHistoryTotal();
   for(int i=total-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
      if(OrderType()>1)continue;// исключим удаленные отложенные ордера
      if(OrderType()!=op && op!=-1)continue;  
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(timecurents==0)timecurents=OrderCloseTime(); // запомним время последнего закрытого ордера
      if(OrderCloseTime()<timecurents)continue;

      profit+=OrderProfit()+OrderCommission()+OrderSwap();
      cnt++;
     }
   if(cnt!=0)
     {
      LastProfit=profit;
      return(true);
     }
   return(false);
  }

..........

 
Sergey Gritsay:

Corrected the second version of the function

Thank you for your help.
Variant 1 does not work correctly
Variant 2 works correctly.
My variant also works correctly, but if the orders are carried over to another day or more, the value is not clear Profit 190, while it should be 4.27.

See the screenshot #3https://www.mql5.com/ru/forum/162930/page3

//+----------------------------------------------------------------------------+
//     Возвращает суммарный профит в валюте депозита серии закрытых ордеров    |
//+----------------------------------------------------------------------------+
double LastProfitCL(int op=-1){ //"op" позиция (-1 любая позиция)
  double LastProfit=0;
  datetime t=0;
   for(i=OrdersHistoryTotal()-1;i>=0;i--)
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && (OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderSymbol()==Symbol() && OrderMagicNumber()==magic) {
       if (OrderSymbol()!=Symbol()||OrderMagicNumber()!=magic) continue;
       if (t<OrderCloseTime()) {t=OrderCloseTime();}
       if ((op<0||OrderType()==op) && t==OrderCloseTime()) {LastProfit+=OrderProfit()+OrderCommission()+OrderSwap();}
       }
  return(LastProfit);
   }
 
As it turns out, the problem is not the number of orders but the transfer of orders to another day or more.
 
Natashe4ka:
As it turns out, the problem is not the number of orders but the transfer of orders to another day or more.
Do you close in series or what? What is the transfer if you close all orders in one cycle?
 
Vitalie Postolache:
So do you close in a series or what? What is the carry-over if you close all orders in one cycle?

Orders are closed by series, but if there are no conditions for closing, the orders are gathered for a day or two, etc.
Although, the number of orders also affects the profit value.
If there are more than 3 orders, the value is calculated for the last closed order only.

Reason: