Download MetaTrader 5
To add comments, please log in or register
Have you installed MetaTrader 5? Find out more about it!
vaknineyal
57
vaknineyal 2014.06.08 09:10 

Hi All

I have problem to modify Take Profit and I need help;

I am openning 2 orders on the same direction, but when I modify them I can modify only one.

I open 2 Orders in the same price calculate the stoploss and then calculate take profit, one order TP should 1:1 RR and the second 1:2 RR.

I can modify one one order to 1:1 and the second dosent work, I tried to change them by OrderMagicNumber() or by OrderComment() both of them doesn't work for me.

any help will be wellcome.

here is the code:

//+------------------------------------------------------------------+
//| Open Long Position                                               |
//+------------------------------------------------------------------+ 

int OpenLongPosition()
   {
      int TicketBuy1;
      int TicketBuy2;
      
      LowPrice = Low[iLowest(Symbol(),0,MODE_LOWER,10)];
      
      if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == true)
         {
            if (OrderSymbol() == Symbol())
               {
                  if (OrderType() == OP_BUY)
                     {
                        CheckMoveSL();
                     }
                }
          }
       if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == false)
         {
            TicketBuy1 = OrderSend(Symbol(),OP_BUY,OrderLot,Ask,50,LowPrice,0,"Buy1",111,0,Green); 
            TicketBuy2 = OrderSend(Symbol(),OP_BUY,OrderLot,Ask,50,LowPrice,0,"Buy2",222,0,Green); 
            TPCalc();
         }
   return(0);   
   }                  



//+------------------------------------------------------------------+
//| Open Short Position                                              |
//+------------------------------------------------------------------+ 

int OpenShortPosition()
   {
      int TicketSell1;
      int TicketSell2;
      
      HighPrice = High[iHighest(Symbol(),0,MODE_UPPER,10)];
      
      if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == true)
         {
            if (OrderSymbol() == Symbol())
               {
                  if (OrderType() == OP_SELL)
                     {
                        CheckMoveSL();
                     }
                }
          }
       if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == false)
         {
            TicketSell1 = OrderSend(Symbol(),OP_SELL,OrderLot,Ask,50,HighPrice,0,"Sell1",111,0,Green); 
            TicketSell2 = OrderSend(Symbol(),OP_SELL,OrderLot,Ask,50,HighPrice,0,"Sell2",222,0,Green); 
            TPCalc();
         }
   return(0);   
     }
     

//+------------------------------------------------------------------+
//| Calculate Take Profit                                            |
//+------------------------------------------------------------------+      
int TPCalc() 
   {
     
     bool ModifyBuy1;
     bool ModifyBuy2;
     bool ModifySell1;
     bool ModifySell2;
     
     if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == true)
      {
         if (OrderSymbol() == Symbol())
            {
               if (OrderType() == OP_BUY)
                     {
                        Risk = OrderOpenPrice() - OrderStopLoss();
                        TakeProfit1 = OrderOpenPrice() + Risk;
                        TakeProfit2 = OrderOpenPrice() + (Risk*RiskFactor);
                        if (OrderComment() == "Buy1")
                           {
                              ModifyBuy1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit1,0,CLR_NONE);
                           } 
                        if (OrderComment() == "Buy2")
                           {
                              ModifyBuy2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit2,0,CLR_NONE);
                           }
                      }
               if (OrderType() == OP_SELL)
                     {
                        Risk = OrderStopLoss() - OrderOpenPrice();
                        TakeProfit1 = OrderOpenPrice() - Risk;
                        TakeProfit2 = OrderOpenPrice() - (Risk*RiskFactor);
                        if (OrderMagicNumber() == 111)
                           {
                              ModifySell1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit1,0,CLR_NONE);
                           }
                        if (OrderMagicNumber() == 222)
                           {
                              ModifySell2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit2,0,CLR_NONE);
                           }
                     }
             }
        }
   return(0);
   }
whroeder1
13610
whroeder1 2014.06.08 09:43  
vaknineyal:  I am openning 2 orders on the same direction, but when I modify them I can modify only one.
     if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == true) 

You are only selecting the oldest open order. How do you expect it to ever modify any other? What if there is an open order on another chart? Then you don't modify any.
Do a OrderSelect loop so you modify ALL your open orders

vaknineyal
57
vaknineyal 2014.06.08 10:01  

So far I tried this and its work I don't know if that the smart way to do that

int TPCalc() 
   {
     
     bool ModifyBuy1;
     bool ModifyBuy2;
     bool ModifySell1;
     bool ModifySell2;
     
     for (int i=1; i<=OrdersTotal(); i++)
      {
         if (OrderSelect(i-1,SELECT_BY_POS)== true)
            {
               if(OrderSymbol() != Symbol()) continue;
               if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                  {
                     Risk = OrderOpenPrice() - OrderStopLoss();
                     TakeProfit1 = OrderOpenPrice() + Risk;
                     TakeProfit2 = OrderOpenPrice() + (Risk*RiskFactor);
                     if(OrderMagicNumber() == 111)
                        {
                          ModifyBuy1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit1,0,CLR_NONE);
                          
                        }
                     else if (OrderMagicNumber() == 222)
                        {
                           ModifyBuy2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit2,0,CLR_NONE);
                        }
                   }
                if (OrderSymbol() == Symbol() && OrderType() == OP_SELL)
                  {
                     Risk = OrderStopLoss() - OrderOpenPrice();
                     TakeProfit1 = OrderOpenPrice() - Risk;
                     TakeProfit2 = OrderOpenPrice() - (Risk*RiskFactor);   
                     if(OrderMagicNumber() == 111)
                        {
                           ModifySell1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit1,0,CLR_NONE);
                        }
                      else if (OrderMagicNumber() == 222)
                        {
                           ModifySell2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit2,0,CLR_NONE);
                        }
                   }
              }   
                     
                     
                     
//     if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == true)
//      {
//         if (OrderSymbol() == Symbol())
//            {
//               if (OrderType() == OP_BUY)
//                     {
//                        Risk = OrderOpenPrice() - OrderStopLoss();
//                        TakeProfit1 = OrderOpenPrice() + Risk;
//                        TakeProfit2 = OrderOpenPrice() + (Risk*RiskFactor);
//                        if (OrderComment() == "Buy1")
//                           {
//                              ModifyBuy1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit1,0,CLR_NONE);
//                           } 
//                        if (OrderComment() == "Buy2")
//                           {
//                              ModifyBuy2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit2,0,CLR_NONE);
//                           }
//                      }
//               if (OrderType() == OP_SELL)
//                     {
//                        Risk = OrderStopLoss() - OrderOpenPrice();
//                        TakeProfit1 = OrderOpenPrice() - Risk;
//                        TakeProfit2 = OrderOpenPrice() - (Risk*RiskFactor);
//                        if (OrderMagicNumber() == 111)
//                           {
//                              ModifySell1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit1,0,CLR_NONE);
//                           }
//                        if (OrderMagicNumber() == 222)
//                           {
//                              ModifySell2 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit2,0,CLR_NONE);
//                           }
//                     }
//             }
        }
   return(0);
   }
/
To add comments, please log in or register