How to get average price without calculating the last open order?

 
double avg()
{
   double averageprice=0,size=0,out=0;

   for(int i = 0; i < OrdersTotal(); i++)
   //for(int i = 0; i <=OrdersTotal()-1; i++)
   //for(int i = OrdersTotal()-1; i >= 0; i--)
   //for(int i = OrdersTotal()-2; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol()==Symbol())
         {
            averageprice += OrderOpenPrice() * OrderLots();
            size += OrderLots();
         }
      }
   }
   if(averageprice>0) 
      out=NormalizeDouble(averageprice/size,Digits);
   
   return out;  
}

I have tried change the for loop but seems it not exclude the last open order but the first open order instead

 
The Village: I have tried change the for loop but seems it not exclude the last open order but the first open order instead
  1. You haven't tried anything. Your loop selects all orders (of Symbol). It does not exclude the last, it does not exclude the first.

  2.    //for(int i = OrdersTotal()-1; i >= 0; i--)

    This remove the last opened order, not the last opened order of symbol.

  3. I have no idea why you want to exclude the last. The result will be meaningless.

  4. If you really want that, change the ordering of the loop from last to first and exclude the initial selected order.

       double averageprice=-1,size=0,out=0;
       for(int i = OrdersTotal() - 1; i >= 0; --i) if(
          OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
       && OrderSymbol()==Symbol()
       ){
         if(averageprice < 0){ averageprice=0; continue; } // Exclude the latest order.
         ⋮
       

 
William Roeder:
  1. You haven't tried anything. Your loop selects all orders (of Symbol). It does not exclude the last, it does not exclude the first.

  2. This remove the last opened order, not the last opened order of symbol.

  3. I have no idea why you want to exclude the last. The result will be meaningless.

  4. If you really want that, change the ordering of the loop from last to first and exclude the initial selected order.


thank you for you comment.

3. the actual idea is to get the average price that also exclude the breakeven opened position and also the last opened order.

The solution you suggest did exclude the last opened order but it will fixed the value to the 1st opened position. 

I tried to add the condition for it to exclude the breakeven opened position but there is something wrong with my condition to exclude the breakeven opened position?

appreciate all of your suggestion.


double AverageBuyPrice()
{
   double lots=0,LotsPrice = 0,average = 0;
   int temp=-1,count=0;
   for (int i = OrdersTotal() - 1; i >= 0; --i)
   {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue;
      if(OrderType()!=OP_BUY|| OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber)continue;
      {
         if(temp<0){temp=0;continue;} //exclude last opened order
         
         if(MarketInfo(OrderSymbol(),MODE_BID)- OrderOpenPrice() < 4 * MarketInfo(OrderSymbol(), MODE_POINT))
         //if(OrderStopLoss()==0) // exclude breakeven opened order
         {
            count++;
            lots = lots + OrderLots();
            LotsPrice = LotsPrice + OrderLots()*OrderOpenPrice();
         }
      }
   }
   if(count>0)
   average = NormalizeDouble(LotsPrice/lots,Digits);

   return(average);
}
Reason: