Convert function average price MQL4 to MQL5

30 views Asked by At

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?

1

There are 1 answers

1
Mark SdS On

Apparently, after reading some posts in this thread, orderCommission is available only after a deal is done.

Maybe you can get an indication of it by calculating the average in your trades history?