Download MetaTrader 5

function to calculate average price

To add comments, please log in or register
Do you want to become a well-known developer? Execute the orders of traders in the Freelance service!
XIn Li
335
XIn Li 2013.01.09 17:37 

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
enbo lu
Moderator
1943
enbo lu 2013.01.10 05:09  
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.

phi nuts
2184
phi nuts 2013.01.10 16:42  
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.

To add comments, please log in or register