Convert function average price MQL4 to MQL5

 

My code below calculate average price in MQL4 and it run ok on mt4, 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);
     }
}


 
vovanchau27: My code below calculate average price in MQL4 and it run ok on mt4, How to re-write it in MQL5.
   for(int i = OrdersTotal() - 1; i >= 0; i--)

In MT5, orders are pending, positions are open. Rewrite your code to use positions.

Position vs. Deal vs. Order - General - MQL5 programming forum (2019)
Difference between a deal and a trade? - General - MQL5 programming forum (2019)

 
William Roeder #:

In MT5, orders are pending, positions are open. Rewrite your code to use positions.

Position vs. Deal vs. Order - General - MQL5 programming forum (2019)
Difference between a deal and a trade? - General - MQL5 programming forum (2019)

yes, i know position, but i can not find how to get commission of opening order.

//MQL4
double _profit = OrderProfit() + OrderSwap() + OrderCommission();

// ->> MQL5
double _profit = PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) + commission;
how to get Order Commission?