Profit calculation of closed orders "HELP" - page 4

 
Renat Akhtyamov:

No, this line is removed. This is a profit calculation, it won't work that way.

Where is the close command in the code - there

//+----------------------------------------------------------------------------+
//     Возвращает суммарный профит в валюте депозита серии закрытых ордеров    |
//+----------------------------------------------------------------------------+
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){if(OrderCloseTime()>=t)LastProfit+=OrderProfit()+OrderCommission()+OrderSwap();}
       }
  return(LastProfit);
   }

and in the closing condition

{CloseOrder(OrderTicket());t=TimeCurrent();} 

{DeleteAll();t=TimeCurrent();}

1 doesn't count, 2 counts correctly, 3 doesn't count 0.

 
Natashe4ka:
//+----------------------------------------------------------------------------+
//     Возвращает суммарный профит в валюте депозита серии закрытых ордеров    |
//+----------------------------------------------------------------------------+
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){if(OrderCloseTime()>=t)LastProfit+=OrderProfit()+OrderCommission()+OrderSwap();}
       }
  return(LastProfit);
   }

and in the closing condition

{CloseOrder(OrderTicket());t=TimeCurrent();} 

{DeleteAll();t=TimeCurrent();}

1 does not count, 2 counts correctly, 3 does not count 0

and that's it.

{t=TimeCurrent();CloseOrder(OrderTicket());}
{DeleteAll();}

And one more thing.

Are your orders closed according to the ticket?

So you need to remember the start time to close the series earlier

That is, where a decision to close a series has just been formed, it does not matter whether it will actually close or not

 
Natashe4ka:
//+----------------------------------------------------------------------------+

I understand your insistence, but I gave you a working version, with a good function of closing a series of positions for real trading, not working in the tester.

It counts and remembers everything, even if the terminal is restarted.

Here's a test, just run it in the tester, and compare the results with the history of trades

PS. Forgot to write in the code - here is the fix

Files:
e-info.mq4  10 kb
 
Vitaly Muzichenko:

I understand your insistence, but I gave you a working version, with a good function of closing a series of positions for real trading, not working in the tester.

It counts and remembers everything, even if the terminal is restarted.

Here's a test, just run it in the tester, and compare the results with the history of trades

How can I apply this to my code?

The value ofLastProfitCL determines myfurther action

 
Renat Akhtyamov:

but otherwise

{t=TimeCurrent();CloseOrder(OrderTicket());}
{DeleteAll();}

One more thing.

Are your orders closing on a ticket?

So you need to remember the time of the series closing even earlier

That is, where a decision to close a series has just been formed, it does not matter whether there will be a real closing or not

If the order is closed by the stop, then{t=TimeCurrent();CloseOrder(OrderTicket();}{DeleteAll();} will already miss the value of the profit
 
Natashe4ka:

How do I apply this to my code?

I haveLastProfitCL value whichdetermines further action

So it's clear, just apply it as you see fit.

  double LastProfit = LastProfitCL(-1, (datetime)GlobalVariableGet(GetGlobalVariableName("LastProfit")));
  
  Comment(" Последний: ",DoubleToString(LastProfit,1) ," Профит: ", DoubleToString(Profit,1) );

Declare"LastProfit" once and use it where needed

 
Natashe4ka:
If the order is closed by a stop, then{t=TimeCurrent();CloseOrder(OrderTicket();}{DeleteAll();} will already miss the value of the profit

Yes, it will.

You can check every tick to see how many live orders are left.

If it is the same, then you should remember the time of the previous tick.

Then you will get what you need.

But stops usually don't happen in automatons

 
Natashe4ka:
If it closes on a stop,{t=TimeCurrent();CloseOrder(OrderTicket();}{DeleteAll();} will already miss the profit value

It will only count those closed last in time, and does not depend on the closing amount.

In general, the task is not completely clear, so no further discussion.

//---------------------------------------------------------

Here you see the description, I don't understand the problem any more:

Files:
e_info.mq4  10 kb
 
Vitaly Muzichenko:

Here it's all laid out, I don't understand the task any more:

OK, thank you.
 

This is also correct if "cnt" is set to zero, but then the value is again incomprehensible as in screenshot 3 (should be a profit value of 4.27)

double LastProfitCL(int op=-1){ //"op" позиция (-1 любая позиция)
  int cnt=0;
  double LastProfit=0;
   for(i=0;i<OrdersHistoryTotal();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 ((op<0 || OrderType()==op) && cnt==0) {LastProfit+=OrderProfit()+OrderCommission()+OrderSwap(); cnt++;} else {cnt=0;}
       }
  return(LastProfit);
   }

Reason: