Calculate Average price of Open Positions for Hedge account

 

hi All,

I need a help in calculating average price of open positions, considering both buy positions and sell positions.

currently i am using two separate functions, buy average price and Sell average price.

i use  reverse martingale system to add positions.

double CalculateBuyTakeprofit()
  {
   double result=0;
   int total = Total.Total_Buy;  // returns total buy positions
   if(total==1)
     {
      result = BuyLastEntry + Takeprofit * Point();
     }
   else if(total>1)
     {
      double average = 0;
      double total_lots = 0;
      ArrayResize(BuyTradesLots,total); // for number of lots
      ArrayResize(BuyTradesEntries,total); // for prices
      for(int i=0; i<total; i++)
        {
         total_lots+=BuyTradesLots[i];
         average+=BuyTradesEntries[i]*BuyTradesLots[i];
        }
      double averagePrice = average/total_lots;
      result = averagePrice + Takeprofit * Point();
     }
   return result;
  }
//+------------------------------------------------------------------+
double CalculateSellTakeprofit()
  {
   double result=0;
   int total = Total.Total_Sell; // return sell positions
   if(total==1)
     {
      result = SellLastEntry - Takeprofit * Point();
     }
   else if(total>1)
     {
      double average = 0;
      double total_lots = 0;
      ArrayResize(SellTradesLots,total); // for number of lots 
      ArrayResize(SellTradesEntries,total); // for different prices 
      for(int i=0; i<total; i++)
        {
         total_lots += SellTradesLots[i];
         average += SellTradesEntries[i]*SellTradesLots[i];
        }
      double averagePrice = average/total_lots;
      result = averagePrice - Takeprofit * Point();
     }
   return result;
  }

  please help me out for the same,

thanks in advance

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Price Constants - Indicator Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
Keith Watford:
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.

thanks for reply,

 
gamblerappa #:

thanks for reply,

You have finish this INdi?

 
gamblerappa: I need a help in calculating average price of open positions, considering both buy positions and sell positions.

Lots Weighted Average Price = ∑  op(i) × lot(i) ÷ ∑ lot(i). This is the break even price of all orders combined.
          OrderOpenPrice question . - MQL4 programming forum (2017)
          looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum (2016)

When combining buys and sells, use +Lots for buys and -Lots for sells.

Reason: