function to calculate average price

 

I am newbie to MQL5. Can someone help me to create a mql5 function like the one in mql4 below. In addition to modify all open positions takeProfit to AVPrice + TakeProfit for buy or   AVPrice - TakeProfit  for sell.

 Thanks in advance.

 


//----------------------- CALCULATE AVERAGE OPENING PRICE

  double AVPrice(int type)

  {

   int total=OrderCount(Symbol(), type);

   double AveragePrice=0;

   double Count=0;

   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

         continue;

      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)

         if(OrderType()==type)

           {

            AveragePrice=AveragePrice+OrderOpenPrice()*OrderLots();

            Count=Count + OrderLots();

           }

     }

   if(total > 0)

      AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);

    return (AveragePrice);

  } 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants - Documentation on MQL5
 
xlitang:

I am newbie to MQL5. Can someone help me to create a mql5 function like the one in mql4 below. In addition to modify all open positions takeProfit to AVPrice + TakeProfit for buy or   AVPrice - TakeProfit  for sell.

 Thanks in advance.

 


//----------------------- CALCULATE AVERAGE OPENING PRICE

  double AVPrice(int type)

  {

   int total=OrderCount(Symbol(), type);

   double AveragePrice=0;

   double Count=0;

   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

         continue;

      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)

         if(OrderType()==type)

           {

            AveragePrice=AveragePrice+OrderOpenPrice()*OrderLots();

            Count=Count + OrderLots();

           }

     }

   if(total > 0)

      AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);

    return (AveragePrice);

  } 

You don't need to caculate average price because there always one position of a symbol in MT5.

 You just need to use PositionGetDouble(POSITION_PRICE_OPEN) to get the open price of your position.

 
xlitang:

I am newbie to MQL5. Can someone help me to create a mql5 function like the one in mql4 below. In addition to modify all open positions takeProfit to AVPrice + TakeProfit for buy or   AVPrice - TakeProfit  for sell.

 Thanks in advance.

...

!. For the next time, please use SRC button when posting code in forum. 

 

2. Xlitang, Luenbo is correct, there is only one position in MT5. Maybe you should try this : open position for 1 lot, say buy 1 lot of EURUSD, and mark it with horizontal line. 5 pips (or 10, 13, or whatever pips) later open buy again for 3 lot  and now look carefully because now you only have one position.

If you still wanted to calculate average price lot like your mql4 code, you should go though deal history from the last closed position using Trade Functions.