MQL4 Code to Average all the Previous Pending Orders and add with certain TakeProfit??

 

i have taken many orders at a certain time, i need a code to generate the average for all those Pending Orders Price and modify all those orders with that average value with some TakeProfit added to it. i want to implement this in Expert Advisors??....someone please help me...!!!

 

It's all here: https://docs.mql4.com/trading

Use this to get the number of orders, market and pending . .

OrdersTotal( )

setup a loop going from the number of orders-1 to 1 in steps of 1 and use this to select the order

for(int pos=OrdersTotal()-1;pos>=0;pos--)
   {
    if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)==true)
       {
       .
       .
       .
       .
       .    

       }
       else Print("OrderSelect returned the error of ",GetLastError());
   }

filter the orders so you only have pending orders using this

if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)==true)
       {
       .
       .
       .
       if (OrderType>1 && OrderType<6)
          {
          .
          .
          }

use this to get the pending orders price and sum them as you go, keep count of how many pending orders

 if (OrderType>1 && OrderType<6)
          {
          OrderPrice = OrderOpenPrice( );


          OrderPriceSum = OrderPriceSum + OrderPrice;
          OrderCount++;
          
          }
 
Or combine with magic number and pair so it is compatible with others
double  OrderPriceSum   = 0;
int     OrderCount      = 0;
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
&&  OrderMagicNumber()  == magic.number             // my magic number
&&  OrderSymbol()       == Symbol()                 // and my pair.
&&  OrderType()         > OP_SELL                   // Pending only
){
    OrderPriceSum += OrderOpenPrice();    OrderCount++;
}
 
kmnatarajan:
hi...thanks....i have done that ....but what about averaging??.... avg= (sum/count)+0.0005 doesn't work for 4 digit platform...why is it so?
No mind readers here. Post YOUR code.
 
No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
 
kmnatarajan:

this s the code i created.....is there anything wrong in it??.

Yes . . . .

 

You don't need two loops . . . . the second is not only not needed but also wrong.

double count = 0,sum=0,avg=0;
for (int k=OrdersTotal()-1; k >= 0; k--)
   if (OrderSelect(k, SELECT_BY_POS, MODE_TRADES))
      {
      if (OrderSymbol() == Symbol())
        if (OrderMagicNumber() == 1)
        {
        count++;
        
        sum += OrderOpenPrice();        //    <-----------------------
        
        }
      }
 avg=(sum/count)+0.0005;     //   why the   + 0.0005 ?