How can I set this code to OrderModify as a Trailing stop after the initial Stop which was placed using order Modify :required for ECN Brokers.

 

 I use an ECN broker so I have to place the order without stops and then use OrderModify to add the fixed Stop and Take Profit which is working well . 

But , I would like to add a trailing stop , I have tried to add the additional OrderModify code in Red but I can't get it to work . 

Any assistance as to how I can put the code in to get it to work would be great. 

 

 // Opening Orders
 

  while(true)                                 
     {
      if (Total==0 && Opn_B==true)                          // No New orders  +
        {                                                                    // Criteria for opening to Buy
         RefreshRates();                       
        Price= iClose(NULL,0,1);
         Last= Close[1];
               
         SL=Bid - New_Stop(StopLoss)*Point;    
         TP=Bid + New_Stop(TakeProfit)*Point;
         TS=Bid-Point*trailingstop;
         Alert(" Attempt to open Buy. Waiting for response..");
         Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,2,00,00);         //Buy order "0"  to place the initial order.        
           if(Ticket < 0)
        
            {                   
          Alert ("OrderSend failed",GetLastError());
          }                
        else
        {
        if(!OrderSelect(Ticket,SELECT_BY_TICKET))
        {
         Alert("OrderSelect failed: ",GetLastError());
         }  
         else
         {
           if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0))          //Folowed by the SP & TP (as required for ECN Brokers)
         {
         Alert("OrderModify Failed",GetLastError()); 
          }   
       
        if (trailingstop > 0)                                                                 //I am trying to add this so that it can Modify be a trailing stop,but I am unsure how to add it to the program ?

        {                                                                                             
        OrderSelect(Ticket,SELECT_BY_TICKET);
       
        if (Ask-OrderOpenPrice()>Point*trailingstop)
        {
        if (OrderStopLoss()<Ask-Point*trailingstop)
        {
        OrderModify(OrderTicket(),OrderOpenPrice(),TS,TP,0,Blue);
        return(0);
               }
            }
          }  
 

   else                  
          {
           
             if(Ticket >0)
             {
             Alert("Opened Orders Buy and SL and TP");
             Alert("down"  , downltt,Digits, "UpSClose"  ,uptrendshortclose,Digits, "UpBOpen"   ,uptrendbuyopen,Digits,"Prices"  ,Prices,Digits,"Price"   ,Price,Digits);
            Alert("Last Array" ,Last,Digits);
            Alert("StochMain",  stochmainl,Digits,"stochSigna", stochsignal,Digits); /*,"SL",SL,Digits,"TS",TS,Digits); */
           
                     
                        
        return;                                      // Need Return to stop orders continuously triggering at each tick
                  }
              }
            }                    
         }       
    
    
       continue;                           
      
         return;                              
        
        } 

 

  1. For large amounts of code, attach it
  2. Check your return codes (OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum
  3. Move your trailing code outside of the open order code. You must return and wait for new ticks.
    int start(){
       ModifyStops();
       OpenOrders();
       Comment(...);
    }

Reason: