Correct way to Calculate total Profit/Loss of an account in mql4?
Correct way to Calculate total Profit/Loss of an account in mql4?
Loading saved threads...
Sam · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Sam
Original post: https://stackoverflow.com/questions/69690725
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am trying to calculate total Profit/Loss of the account but everything I have tried so far has only resulted in failure
its a demo account with deposit : 100K
Profit/Loss : (negative)-31009.63
No active trade
Here is what I have attempted till now :
AccountInfoDouble(ACCOUNT_PROFIT);
//it returned 0.0
Then I tried calculating Profit/Loss of all historic trades like this :
double tempproloss = 0;
for(int i=0; i<OrdersHistoryTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderType()<=5)
{
tempproloss = tempproloss+OrderProfit();
}
}
}
//This returned -6188.09 which is still not correct
Then I tried calculating Profit/Loss Of all orders including deposits etc , like this :
double tempproloss = 0;
for(int i=0;i<OrdersHistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
tempproloss = tempproloss+OrderProfit();
}
}
//It returned 29406.49 , no idea how it ended up with this number.
Then I got the Deposit value in a variable and then I tried Calculating Profit/Loss of all closed trades (Not including the deposit etc) and then Subtracted it from Deposit amount , like this :
double Deposits = 0;
for(int i=0;i<OrdersHistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderType() == 6)
{
if(OrderProfit()>0)
{
Deposits = Deposits+OrderProfit();
}
}
}
}
double tempproloss = 0;
for(int i=0;i<OrdersHistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderType()<6)
{
tempproloss = tempproloss+OrderProfit();
}
}
}
Print( Deposits -tempproloss );
// it Returned 106188.09 which wrong again
Been struggling with this for quite some time, but couldn't figure out a way to make it work , can someone please point out What am I missing ?
Thank you.
Quote
Report
krez · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: krez
Original post: https://stackoverflow.com/a/69697054
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Go to the Account History tab, right-click and select All History. Run your scripts again.
If the above gets you closer, include OrderCommission() and OrderSwap() if you need the exact PnL value.
AccountInfoDouble(ACCOUNT_PROFIT);
//it returned 0.0
This only shows profit for open orders.
Quote
Report
Aref Rahimipour · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Aref Rahimipour
Original post: https://stackoverflow.com/a/73392709
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
this code is working
void totalTp(){
tp = 0.0;
for (int i = 0; i < OrdersTotal(); i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_BUY || OrderType() == OP_SELL )
{
tp += OrderProfit();
}
}}
Comment(tp);
}
Quote
Report
Post Reply
Checking account access…