How to calclate total profit

 

I want to know total profit of open orders (BUY position + SELL position)

I also want to include swap + comission.

I don't want to use AccountProfit, so I tried to calculate total (BUY+SELL orders).

I coded below but when I check with Print(Total_profit), the value is different from MT4 current profit value.

What is the problem?

   double Total_profit= NULL;int cnt = NULL;
   for(cnt=0;cnt<OrdersTotal();cnt++){
   OrderSelect(cnt,SELECT_BY_POS);
   if(OrderSymbol() == Symbol() && OrderMagicNumber()==magic)
   {if( OrderType() == OP_BUY || OrderType() == OP_SELL )
      {Total_profit= Total_profit+ OrderProfit() + OrderSwap() + OrderCommission();}    
   }
   }
 
kajironpu:

I want to know total profit of open orders (BUY position + SELL position)

I also want to include swap + comission.

I don't want to use AccountProfit, so I tried to calculate total (BUY+SELL orders).

I coded below but when I check with Print(Total_profit), the value is different from MT4 current profit value.

What is the problem?

For the account current PnL do not filter by Symbol nor by Magic. Also, compare cnt values with lower or equal than OrdersTotal()

   double Total_profit= NULL;int cnt = NULL;
   for(cnt=0;cnt<=OrdersTotal();cnt++){
   OrderSelect(cnt,SELECT_BY_POS);
   if(OrderSymbol() == Symbol() && OrderMagicNumber()==magic)
   {if( OrderType() == OP_BUY || OrderType() == OP_SELL )
      {Total_profit= Total_profit+ OrderProfit() + OrderSwap() + OrderCommission();}    
   }
   }

For the account realised PnL evaluate today's closed trades

double Total_profit= 0;

for( int cnt = 0; cnt <= OrdersHistoryTotal(); cnt++)
        if ( OrderSelect( cnt, SELECT_BY_POS, MODE_HISTORY ) )
                if( OrderType() < 2 && iTime(NULL, PERIOD_D1,0) <= OrderCloseTime() )
                        Total_profit += OrderProfit() + OrderSwap() + OrderCommission();
 
Fernando Morales:

For the account current PnL do not filter by Symbol nor by Magic. Also, compare cnt values with lower or equal than OrdersTotal()

For the account realised PnL evaluate today's closed trades

Hi, Fernando san

Thank you for your help.

If I put different EA in different chart, if I do not filter by MAGIC and Symbol, will it be correct value?

I mean I want to know current total profit which now I have positions  where I attached EA.
 
Kajironpu,
Try remove ordersymbol()==Symbol()  bool check, as magicnumber is the unique identification code for EA.
For example, you put EA in both usdjpy and eurjpy, the magicnumber of EA should be the same but ordersymbol parameter is different, this" and" condition checking might be the root cause
 
hiroshitiu:
Kajironpu,
Try remove ordersymbol()==Symbol()  bool check, as magicnumber is the unique identification code for EA.
For example, you put EA in both usdjpy and eurjpy, the magicnumber of EA should be the same but ordersymbol parameter is different, this" and" condition checking might be the root cause

Hi, hiroshitiu

I am confused.

For example,

In the same account, I run 2 EAs (different EA).

EA1: EURUSD magic=111 TF=M1   and order#1(winning) order#2(wining)   order#3 (losing)  / total profit $10

EA2: EURUSD magic=222  TF=M15    and order#1(losing)  order#2(losing)   / total profit $-20

I want to get profit for each.

Even tough this condition, I need to remove symbol and magic number filter?

I thought if I don't filter by magic and symbol, total profit would be combined all in my account.

I mean I want to get current profit of EA attached only.

 
kajironpu:

Hi, hiroshitiu

I am confused.

For example,

In the same account, I run 2 EAs (different EA).

EA1: EURUSD magic=111 TF=M1   and order#1(winning) order#2(wining)   order#3 (losing)  / total profit $10

EA2: EURUSD magic=222  TF=M15    and order#1(losing)  order#2(losing)   / total profit $-20

I want to get profit for each.

Even tough this condition, I need to remove symbol and magic number filter?

I thought if I don't filter by magic and symbol, total profit would be combined all in my account.

I mean I want to get current profit of EA attached only.

Kajironpu san,

Let me explain with your example above, and make it more generic.

Assume 7 open orders exist (2 manual released, 5 EA released

Order #1, EURUSD magic =111

Order #2, EURUSD magic =111

Order #3, EURAUD magic =111

Order #4, EURGBP magic =222

Order #5, EURJPY magic =222

Order #6, EURJPY magic =0 (manual)

Order #7, EURCHF magic =0 (manual)

To get the total current profit of EA released order (#1-#5) the solution as below, I cut the codes and brackets which not needed.

double Total_profit= NULL;
   for(int cnt=0;cnt<OrdersTotal();cnt++)
	if (OrderSelect(cnt,SELECT_BY_POS))
   		if(OrderMagicNumber()!=0)
			Total_profit+= OrderProfit() + OrderSwap() + OrderCommission();

   

As manual orders' magicnumber can be 0 ONLY, if you want to get all EA orders, the solution is to filter out the manual orders with magicnumber=0

Why OrderSymbol()==Symbol() does not work?

Because when the codes run in the market, the return value of "Symbol()" is the name of market that the EA/Script running

if you run the codes at EURUSD, it will not be able to read order #3-#7 because the condition "Symbol()==OrderSymbol()"("EURUSD" not equal to "EURAUD" for #3) cannot fulfill

 
kajironpu:

Hi, Fernando san

Thank you for your help.

If I put different EA in different chart, if I do not filter by MAGIC and Symbol, will it be correct value?

I mean I want to know current total profit which now I have positions  where I attached EA.

If you want to calculate the PnL of each EA, then you must filter my OrderMagicNumber(), never by Symbol. hiroshitiu posted the code to do this for the floating PnL.

For closed trades you have to use my second code example, which it just need the filter for OrderMagicNumber()

 
hiroshitiu:

Kajironpu san,

Let me explain with your example above, and make it more generic.

Assume 7 open orders exist (2 manual released, 5 EA released

Order #1, EURUSD magic =111

Order #2, EURUSD magic =111

Order #3, EURAUD magic =111

Order #4, EURGBP magic =222

Order #5, EURJPY magic =222

Order #6, EURJPY magic =0 (manual)

Order #7, EURCHF magic =0 (manual)

To get the total current profit of EA released order (#1-#5) the solution as below, I cut the codes and brackets which not needed.

As manual orders' magicnumber can be 0 ONLY, if you want to get all EA orders, the solution is to filter out the manual orders with magicnumber=0

Why OrderSymbol()==Symbol() does not work?

Because when the codes run in the market, the return value of "Symbol()" is the name of market that the EA/Script running

if you run the codes at EURUSD, it will not be able to read order #3-#7 because the condition "Symbol()==OrderSymbol()"("EURUSD" not equal to "EURAUD" for #3) cannot fulfill

Hi, hiroshitiu san Sorry for my late reply. Now I am clear that manual order MAGIC is 0 , wow I thought that it is same as EA order....

Thank you so much.

 
Fernando Morales:

If you want to calculate the PnL of each EA, then you must filter my OrderMagicNumber(), never by Symbol. hiroshitiu posted the code to do this for the floating PnL.

For closed trades you have to use my second code example, which it just need the filter for OrderMagicNumber()

Hi,Fernando

Thank you for your kind reply.

If I want to calculate the PnL of each EA, then I must filter my OrderMagicNumber(), never by Symbol....

I got it.  Your second code is very useful and I will try with it. Gracious.

Reason: