# Calculating profit with a given price

35

Hi,

I'm trying to write a function in MQL4 which will calculate my total profit (could be negative, which would mean loss) at a given price.

In other words, if I have an open Buy order at price P1 then:

my profit at P2 would be ((P2-P1) * volume) - but that's in most simplistic terms, now I need to do that for all open orders, and account for Buys and Sells and also Account for Spread and the fact that Buys open with Ask and close with Bid and vice versa, Sells open with Bid and close with Asks.

I know this might be an easy exercise for a good programmer, but I'm just being lost in all these details and can't seem to get it right. Can someone help me out here? In other words, I need something similar to a sum of all OrderProfit() for my open orders but calculated at a given price instead of current.

Thanks!

358

RodionPronin:

Hi,

I'm trying to write a function in MQL4 which will calculate my total profit (could be negative, which would mean loss) at a given price.

In other words, if I have an open Buy order at price P1 then:

my profit at P2 would be ((P2-P1) * volume) - but that's in most simplistic terms, now I need to do that for all open orders, and account for Buys and Sells and also Account for Spread and the fact that Buys open with Ask and close with Bid and vice versa, Sells open with Bid and close with Asks.

I know this might be an easy exercise for a good programmer, but I'm just being lost in all these details and can't seem to get it right. Can someone help me out here? In other words, I need something similar to a sum of all OrderProfit() for my open orders but calculated at a given price instead of current.

Thanks!

```string CheckReport () {
static string   ProfitReport = "";
static int      TimeToReport = 0;
#define Daily           0
#define Weekly          1
#define Monthly         2
#define All             3

TimeToReport = 0;
}

if (TimeLocal() > TimeToReport) {
TimeToReport = TimeLocal() + 300;
double  Profit[10], Lots[10], Count[10];
ArrayInitialize(Profit,0);
ArrayInitialize(Lots,0.000001);
ArrayInitialize(Count,0.000001);

int Today     = TimeCurrent() - (TimeCurrent() % 86400);
int ThisWeek  = Today - TimeDayOfWeek(Today)*86400;
int ThisMonth = TimeMonth(TimeCurrent());
for (int i = 0; i < HistoryTotal(); i++) {
if ( OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==Symbol() && (OrderMagicNumber()== magic)  && OrderCloseTime() > 0 ) {
Count[All]      += 1;
Profit[All]     += OrderProfit() + OrderSwap() + OrderCommission();
Lots[All]       += OrderLots();
if (OrderCloseTime() >= Today) {
Count[Daily]    += 1;
Profit[Daily]   += OrderProfit() + OrderSwap() + OrderCommission();
Lots[Daily]     += OrderLots();
}
if (OrderCloseTime() >= ThisWeek) {
Count[Weekly]   += 1;
Profit[Weekly]  += OrderProfit() + OrderSwap() + OrderCommission();
Lots[Weekly]    += OrderLots();
}
if (TimeMonth(OrderCloseTime()) == ThisMonth) {
Count[Monthly]  += 1;
Profit[Monthly] += OrderProfit() + OrderSwap() + OrderCommission();
Lots[Monthly]   += OrderLots();
}
}
}
ProfitReport =  "\n\nProfit Report" +
"\nToday: \$"             + DoubleToStr(Profit[Daily],2) +
"\nThis Week: \$"         + DoubleToStr(Profit[Weekly],2) +
"\nThis Month: \$"        + DoubleToStr(Profit[Monthly],2) +
"\nAll Profits: \$"       + DoubleToStr(Profit[All],2) +
"\nAll Lots: "           + DoubleToStr(Lots[All],2)     + "  (Average \$"+DoubleToStr(Profit[All]/Lots[All],2) +" per lot)";
}
return (ProfitReport);
}
```

Would something like this help??

16076

RodionPronin: my profit at P2 would be ((P2-P1) * volume) - but that's in most simplistic terms, now I need to do that for all open orders, and account for Buys and Sells and also Account for Spread and the fact that Buys open with Ask and close with Bid and vice versa, Sells open with Bid and close with Asks.

I know this might be an easy exercise for a good programmer, but I'm just being lost in all these details and can't seem to get it right. Can someone help me out here? In other words, I need something similar to a sum of all OrderProfit() for my open orders but calculated at a given price instead of current.

1. Just sum individual orders: (P2 - OrderOpenPrice) *Direction(OrderType) * OrderLots * DeltaPerlot
```double   Direction(int op_xxx){ return( 1. - 2. * (op_xxx%2) );               }
```