Download MetaTrader 5
To add comments, please log in or register
What are the latest MQL5.community news? The Wall section will tell you!
DeanDeV
330
DeanDeV 2015.06.25 17:38 

Hey guys,

I am trying to make a trailing step. Essentially like a trailing stop, but instead of moving the stop loss every point in profit, it only moves the stop loss after X amount in profit. Below is my code (tries to modify as soon as order enters and I get a constant OrderModify error 1):

for(int z=OrdersTotal()-1; z>=0; z--)
  {
   if(!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue;
    if(OrderMagicNumber()==MagicNumber)
     if(OrderSymbol()==Symbol())
      if(OrderType()==OP_BUY)
       {
        for(int tstepb=0; tstepb>=0; tstepb++)
         {
          if(Bid-OrderOpenPrice()>=(WhenToTrail*Point)+(TrailingStep*tstepb*Point))
           if(OrderStopLoss()<Bid-(TrailingStep*Point))
            {
             bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStep*Point),OrderTakeProfit(),0,clrBlue);
            }
         }
       }
       
      if(OrderType()==OP_SELL)
       {
        for(int tsteps=0; tsteps>=0; tsteps++)
         {
          if(OrderOpenPrice()-Ask>=(WhenToTrail*Point)+(TrailingStep*tsteps*Point))
           if(OrderStopLoss()>Ask+(TrailingStep*Point) || OrderStopLoss()==0)
            {
             bool sellstep=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailingStep*Point),OrderTakeProfit(),0,clrRed);
            }
         }
       }
  }

 Am I going about this incorrectly..? Is my logic incorrect..?

Any help is appreciated! 

Keith Watford
Moderator
7899
Keith Watford 2015.06.26 04:52  
        for(int tstepb=0; tstepb>=0; tstepb++) //Why use a loop, especially a never-ending loop
         {
          if(Bid-OrderOpenPrice()>=(WhenToTrail*Point)+(TrailingStep*tstepb*Point))
           if(OrderStopLoss()<Bid-(TrailingStep*Point))
            {
             bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStep*Point),OrderTakeProfit(),0,clrBlue);
            }
         }

You are stuck in a never-ending loop and there is no need for the loop at all

 

You could consider something like

   double new_stop_loss;
   if(Bid-OrderOpenPrice()>=WhenToTrail*Point)
     {
      if(OrderOpenPrice()-OrderStopLoss()>Point)
         new_stop_loss=OrderOpenPrice();
      else
         new_stop_loss=Bid-WhenToTrail*Point;
      if(new_stop_loss-OrderStopLoss()>=(TrailingStep*Point))
        {
         bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),new_stop_loss,OrderTakeProfit(),0,clrBlue);
        }
     }

 That is for your buy orders, not compiled or tested.

Don't forget that for sell orders you will need to take into account orders that may not have an initial SL. 

DeanDeV
330
DeanDeV 2015.06.28 08:52  
GumRai:

You are stuck in a never-ending loop and there is no need for the loop at all

 

You could consider something like

 That is for your buy orders, not compiled or tested.

Don't forget that for sell orders you will need to take into account orders that may not have an initial SL. 

Thanks GumRai, will give this a try and get back to you. :) 
DeanDeV
330
DeanDeV 2015.06.28 19:21  
GumRai:

You are stuck in a never-ending loop and there is no need for the loop at all

 

You could consider something like

 That is for your buy orders, not compiled or tested.

Don't forget that for sell orders you will need to take into account orders that may not have an initial SL. 

Fantastic works like a charm! Thanks!
/
To add comments, please log in or register