Profit calculation of closed orders "HELP"

 

The profit of the closed orders is considered correctly, but after closing it adds the value to the previous one (5; 5+5=10; 10+5=15)
How to make each closed profit of a series be a new one, instead of the previous one (5; 5; 5) ?
In a series of closed orders there can be buy and sell, or only buy and only sell orders

color ProfitColor;

// START //
   if(LastProfitCL(-1)<0) ProfitColor=Red;
   if(LastProfitCL(-1)>0) ProfitColor=LimeGreen;
   if(LastProfitCL(-1)==0)ProfitColor=DarkGray;
   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(LastProfitCL(-1),2)),10,"Arial",ProfitColor);
// END START //

//+----------------------------------------------------------------------------+
//|    Возвращает суммарный профит в валюте депозита серии закрытых ордеров    |
//+----------------------------------------------------------------------------+
double LastProfitCL(int op=-1){ //"op" позиция (-1 любая позиция)
  double LastProfit=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 (OrderType()!= op || OrderSymbol()!= Symbol() || OrderMagicNumber()!= magic) continue;
       if (op<0 || OrderType()==op) {LastProfit+=OrderProfit()+OrderCommission()+OrderSwap();}
       }
  return(LastProfit);
   }
 

We need to add a condition for dividing closed orders into series

Tell us the algorithm or division principle

 
Natashe4ka:   ... Profit of closed orders ... adds the value to the previous one (5; 5+5=10; 10+5=15)

How to make each closed profit of a series be a new one, not the previous one (5; 5; 5) ?? .....

Instead of arithmetic summation, we should add in a character string with a space or semicolon and space, whichever is better
 
STARIJ:
Instead of arithmetic summation, add it to a character string with a space or semicolon and space, whichever you prefer

Good idea.

Or we could put it in an array.

 
Natashe4ka:

The profit of the closed orders is considered correctly, but after closing it adds the value to the previous one (5; 5+5=10; 10+5=15)
How to make each closed profit of a series be a new one, instead of the previous one (5; 5; 5) ?
In a series of closed orders there can be buy and sell, or only buy and only sell orders

color ProfitColor;

// START //
   if(LastProfitCL(-1)<0) ProfitColor=Red;
   if(LastProfitCL(-1)>0) ProfitColor=LimeGreen;
   if(LastProfitCL(-1)==0)ProfitColor=DarkGray;
   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(LastProfitCL(-1),2)),10,"Arial",ProfitColor);
// END START //

//+----------------------------------------------------------------------------+
//|    Возвращает суммарный профит в валюте депозита серии закрытых ордеров    |
//+----------------------------------------------------------------------------+
double LastProfitCL(int op=-1){ //"op" позиция (-1 любая позиция)
  double LastProfit=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()) continue;
       if (OrderMagicNumber()!= magic) continue;        
       if (OrderType()!= op) continue;
       if (op<0 || OrderType()==op) {LastProfit+=OrderProfit()+OrderCommission()+OrderSwap();}
       }
  return(LastProfit);
   }
You can enter a global variable, and write the current profit in it. After closing the order, subtract the previous amount, and what is left will be the profit and remember it
 

It's complicated, is there anything simpler?
And examples can you give, because your brain is boiling?

 
Natashe4ka:
This is a bit complicated; is there anything simpler?

No, it will not be easier. You always count ALL orders, including those which were counted during the previous times you used theLastProfitCL function.And since there are more and more orders, the amount of profit increases too.

We have to work out the condition according to which the loop will be broken when the new orders are completed.

 
Vitalie Postolache:

We need to work out a condition whereby the loop will be interrupted when the new orders run out.

There, that's the right way to think about it))

 
Natashe4ka:

There, that's the right way to think about it))

So?

Where's the condition?

 
Renat Akhtyamov:

So?
Where is the condition?

something like this:

double cnt=0;

if (op<0||OrderType()==op) {lastprof+=OrderProfit()+OrderCommission()+OrderSwap(); cnt++}  

and you have to somehow subtract cnt new from past
cnt 1,2,3 past
cnt 4,5,6 new

 
Natashe4ka:

something like this:

double cnt=0;

if (op<0||OrderType()==op) {lastprof+=OrderProfit()+OrderCommission()+OrderSwap(); cnt++}  

and you have to somehow subtract cnt new from past
cnt 1,2,3 past
cnt 4,5,6 new

No, I don't mean the way you already wrote it.

Tell me in words - profit calculation for closed orders you are interested in

for example:

- for the day

- loss-making

- last order

or something else?

Describe the series

Reason: