Trailing stop help

 

Hello, I need help for my code

The trailing stop does not move(it's fixed when it is active), it should move once the price go to the take profit

here is my code for that

void Trailing_SL()
{ 
   for (int i = 0; i < OrdersTotal(); i++) 
    {
      bool res = OrderSelect(i,SELECT_BY_POS,MODE_TRADES); 
       int Local_ticket = OrderTicket();  
              
       if(OrderMagicNumber() == MAGICMA) 
         {
           if (OrderType() == OP_BUY) 
            {
            double Trailing_Start= (OrderOpenPrice()-OrderStopLoss())/2; //My SL is not fixed but it depends on the strategy
            double Trailing_Step= (OrderOpenPrice()-OrderStopLoss())/5; //My TP is not fixed but it depends on the strategy
             if (Bid - OrderOpenPrice() >= Trailing_Start ) 
               {
                if(Bid - OrderStopLoss() >= Trailing_Start ) 
                {
                 double New_TSL_Buy = (Bid - Trailing_Start ) + Trailing_Step ; //New TSL
                 bool Local_Res; //Indicativo para Fail Safe
                 Local_Res = OrderModify(Local_ticket,OrderOpenPrice(),New_TSL_Buy,OrderTakeProfit(),0); 
                 
                 if (Local_Res == false)//Si la orden no se modifico correctamente
                  {
                     Alert("Failed for order #", OrderTicket()); 
                     Alert("Error code: ", GetLastError()); 
                    }
                   }
                  }
                }
               else if(OrderType() == OP_SELL) //y si es compra
                  {
                   Trailing_Start= (OrderStopLoss()-OrderOpenPrice())/2; //My SL is not fixed but it depends on the strategy
                   Trailing_Step= (OrderStopLoss()-OrderOpenPrice())/5; //My TP is not fixed but it depends on the strategy
                    if ( OrderOpenPrice() - Ask >= Trailing_Start  )
                      {
                       if(OrderStopLoss() - Ask >= Trailing_Start ) 
                       {
                         double New_TSL_Sell = (Ask + Trailing_Start ) - Trailing_Step ; //New TSL
                         Local_Res = OrderModify(Local_ticket,OrderOpenPrice(),New_TSL_Sell,OrderTakeProfit(),0); 
                          
                          if (Local_Res == false) 
                           {
                              Alert("Failed for order #", OrderTicket()); 
                              Alert("Error code: ", GetLastError()); //Y la causa
                             }
                            }
                          }
                        }
                     } 
                  }  
                }    
   
Files:
Capture.PNG  22 kb
 
Tolotra Ny:

Hello, I need help for my code

The trailing stop does not move(it's fixed when it is active), it should move once the price go to the take profit

here is my code for that


What does it say as an error in the expert tab?

tell me, we can find where the error came from with the code change.

if (Local_Res == false)//Si la orden no se modifico correctamente
                  {
                     Print("Local_ticket: ",Local_ticket,
                     " OrderOpenPrice():" ,OrderOpenPrice(),
                     "New_TSL_Buy:",New_TSL_Buy,
                     " OrderTakeProfit()",OrderTakeProfit(),
                     " Error ",GetLastError());
                     Alert("Failed for order #", OrderTicket()); 
                     Alert("Error code: ", GetLastError()); 
                    }
 
Tolotra Ny:

Hello, I need help for my code

The trailing stop does not move(it's fixed when it is active), it should move once the price go to the take profit

here is my code for that

I wouldn't recommend this:


            double Trailing_Start= (OrderOpenPrice()-OrderStopLoss())/2; //My SL is not fixed but it depends on the strategy
            double Trailing_Step= (OrderOpenPrice()-OrderStopLoss())/5; //My TP is not fixed but it depends on the strategy


You are using DIVISION, which will generate non-linear values, which will at some point be out of the STEP values of the Symbol.. 

By using it like a simple division (which is how it is).. this will work sometimes, and not other times, like lottery. 


The better is to create a STEPPING degree, based on a better scale, which must be verified against the STEP values of the Symbol, to adjust for the Correct Stepping 


Also, you have to normalize the values, always, this is a must, normalize the resulting value (the new SL value) BEFORE sending it to OrderModify.

The absense of normalization is the most commom source of OrderModify returns failures. 

 
rrocchi:

I wouldn't recommend this:



You are using DIVISION, which will generate non-linear values, which will at some point be out of the STEP values of the Symbol.. 

By using it like a simple division (which is how it is).. this will work sometimes, and not other times, like lottery. 


The better is to create a STEPPING degree, based on a better scale, which must be verified against the STEP values of the Symbol, to adjust for the Correct Stepping 


Also, you have to normalize the values, always, this is a must, normalize the resulting value (the new SL value) BEFORE sending it to OrderModify.

The absense of normalization is the most commom source of OrderModify returns failures. 

thanks so much sir, it's fixed