My code below calculate average price in MQL4 and it run ok How to re-write it in MQL5.
void getAveragePrice()
{
double sum_lot_b = 0;
double sum_price_b = 0;
double sum_profit_b = 0;
double ave_price_b = 0;
double sum_lot_s = 0;
double sum_price_s = 0;
double sum_profit_s = 0;
double ave_price_s = 0;
int countBuy = 0;
int countSell = 0;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == _Symbol)
{
double _profit = OrderProfit() + OrderSwap() + OrderCommission();
if(OrderType() == OP_BUY)
{
sum_lot_b += OrderLots();
sum_price_b += OrderOpenPrice() * OrderLots();
sum_profit_b += _profit;
countBuy++;
}
if(OrderType() == OP_SELL)
{
sum_lot_s += OrderLots();
sum_price_s += OrderOpenPrice() * OrderLots();
sum_profit_s += _profit;
countSell++;
}
}
}
if(sum_price_b > DBL_EPSILON)
{
sum_price_b /= sum_lot_b;
ave_price_b = NormalizeDouble(((sum_profit_b)/(MathAbs(sum_lot_b*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits);
}
if(sum_price_s > DBL_EPSILON)
{
sum_price_s /= sum_lot_s;
ave_price_s = NormalizeDouble(((sum_profit_s)/(MathAbs(sum_lot_s*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits);
}
}
How to re-write in mql5.
How calculate profit in mql5.
double _profit = OrderProfit() + OrderSwap() + OrderCommission();
How to get order commission?
Apparently, after reading some posts in this thread,
orderCommissionis available only after a deal is done.Maybe you can get an indication of it by calculating the average in your trades history?