Do you have better solution for Buy and sell average profit price function?

 

Hello All coders.

Have a nice day.

I tried to create buy and sell common takeprofit point by this codes.

It work at first well but when orders open so much its added order lot sizes are huge and take profit point are more away than actual profit point.

This mean All buy orders are in profit condition but codes are still run for takeprofit point .

So next opposit orders are more open and more take profit points away.

LATER Account balance cannot tolerate float and wipe out.

Please help me more reasonable takeprofit codes for buy and sell orders or suggest me.

 int  totalOrdensOpen = CountTrades(MagicNumber);//example 13 orders /count++


   double  BuyAveragePrice = 0;
   double SellAveragePrice = 0;
  
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
     
         
  double orderopenprice = OrderOpenPrice();
  int   orderticket = OrderTicket();
  int  ordertype = OrderType();
  double orderlots = OrderLots();
     
     
     
     
     
     
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber)
         continue;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

        {
         if(OrderType() == OP_BUY )

           {
             buyersum += (orderopenprice - 2*Point) * orderlots;

             buyerlots += orderlots;
             Print("buysum is "+ buyersum +"");
             Print("buy lot is "+ buyerlots +"");
           }
        }
        
         {
         if(OrderType() == OP_SELL )

           {
             sellersum += (orderopenprice + 2*Point) * orderlots;

             sellerlots += orderlots;
              Print("sellsum is "+ sellersum +"");
               Print("sell lot is "+ sellerlots +"");
           }
        }
        
        
     }

   if(totalOrdensOpen>0)
   {
    BuyAveragePrice = NormalizeDouble((buyersum - sellersum) / (buyerlots - sellerlots), Digits);
          Print("buy avg is"+BuyAveragePrice+"");       
    SellAveragePrice = NormalizeDouble((sellersum - buyersum) / (sellerlots - buyerlots), Digits) ;
         Print("Sell avg is"+SellAveragePrice+"");
   
   }
Files:
 

you can try to calculate the money you will take of profits.


here is the formula 


lots * tickvalue * point = xx currency


write your code on method onTick or onTimer to monitor you floting profits you will figureit out. instead of calcualte point

 

and for better coding habits  you probably need to change this style code 


 for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
     
         
  double orderopenprice = OrderOpenPrice();
  int   orderticket = OrderTicket();
  int  ordertype = OrderType();
  double orderlots = OrderLots();

...
}

to this

  double orderopenprice;
  int   orderticket;
  int  ordertype;
  double orderlots;
for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      orderopenprice = OrderOpenPrice();
  orderticket = OrderTicket();
  ordertype = OrderType();
  orderlots = OrderLots();
	}
         


do not including var into loop body


GOOD LUCK

 
BuyAveragePrice = NormalizeDouble((buyersum - sellersum) / (buyerlots - sellerlots), Digits);
          Print("buy avg is"+BuyAveragePrice+"");       

Average Buy price has nothing to do with Sell orders. Your formula is bogus.

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

Reason: