Download MetaTrader 5

Modifying the TP

To add comments, please log in or register
talal222
13
talal222  

hi all

am trying to find a way to modify the TP in several orders to average +20 pips

any help?

Arnold
233
Arnold  
talsabiri:

hi all

am trying to find a way to modify the TP in several orders to average +20 pips

any help?

What do you mean? if you want them to all average at 20 pips, then just make them 20 pips. Maybe you mean distributing the 20 pips to all open positions? If you mean the latter, the code below might help.. Gdluck


//first count the number of orders
for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==Magic)
        {
         if(Ordertype() == OP_BUY)
            {
            if(OrderTakeProfit() == 0)MyPositions++;
            MyAllocatedPips = MyAllocatedPips + OrderTakeProfit() - OrderOpenPrice()
            }
         if(Ordertype() == OP_SELL)
            {
            if(OrderTakeProfit() == 0)MyPositions++;
            MyAllocatedPips = MyAllocatedPips + OrderOpenPrice() - OrderTakeProfit()
            } 
         }
      }    
      
//calculate the number of pips to distribute to each remaining position       
PipsToAllocate = (20 - MyAllocatedPips)/MyPositions;

//modify the PipsToAllocate to the remaining positions 
for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==Magic)
        {
         if(Ordertype() == OP_BUY && OrderTakeProfit() == 0)
            {
            OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() + PipsToAllocate)
            }
         if(Ordertype() == OP_SELL && OrderTakeProfit() == 0)
            {
            OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() - PipsToAllocate)
            } 
         }
      }    


talal222
13
talal222  

Dear heyarn

thanks for the reply I have to codes one is calculating the average of the open price

the other is modifiyng the orders TP am using each one alone and manually change the average price to be average + 20 pips

it means the Break Even of all orders +20 pips but I could not substitute the average into the TP Ordermodify()

talal222
13
talal222  
//Calculatin the average of the open price for all orders 

   double counter=0;
   double average1=0;
   int cnt=0;
   
   for (int i=1; i<=OrdersTotal(); i++)       //Cycle for all orders..
     {                                        //displayed in the terminal
      if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one
        {      
        double Price=OrderOpenPrice();   
        
        counter = counter+Price;  
        cnt++;
        average1=counter/cnt;   
        
                     
        if (cnt== OrdersTotal())
        {
        
        MessageBox("Average is "+average1);
        
            }
}
}


//Order Modification code

f (OrdersTotal() > 0 )
  
  {
        
   }
//----
   int total,cnt;

            total = OrdersTotal();

            for(cnt=0;cnt<total;cnt++)
            {
            if(total > 0 )
            {
            double x=average1;
            
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
            if(OrderType()==OP_BUY) // long position is opened
            {
            
            
            OrderModify(OrderTicket(),OrderOpenPrice(),0,x+0.0020,0,Red);
            }
            }
            }
Viffer
614
Viffer  

You will need to NormaliseDouble() x. I suspect the division is giving you too many digits and the modify therefore fails.

If you use GetLastError() after your order modify it would tell you the error code and allow you to track down the problem.

V

arteefengfu
5
arteefengfu  
talsabiri:

THank you very much. You saved my life.
To add comments, please log in or register