Move Stop To Break Even When Get to a Pivot Level

 

So I want to move my stop to break even when my trade hits the pivot line

However it seems doesn't work. Any suggestions ? Thanks in advance.

extern int magic           = 20051016; //magic number

int start()
  {

//+------------------------------------------------------------------+
//| getting the pivot point level                                    |
//+------------------------------------------------------------------+

double M1 = iCustom(NULL,0,"AIME M-Pivots",9,0,15,"Gray","Gray",8,0,1,0);

for (int b=OrdersTotal()-1; b >= 0; b--) // going through order loop
   {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))                  // selecting opened orders
         if(OrderMagicNumber() == magic)                            // selecting order by magic number             
            if(OrderType() == OP_SELL)                              // selecting order by type (I am selecting only the sell orders for now
              if(OrderOpenPrice()-Ask >= OrderOpenPrice()- M1)      // M1 is always below the order level
                  if(OrderOpenPrice()>OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
   }

 return(0);
}
  
 
rori4:

So I want to move my stop to break even when my trade hits the pivot line

However it seems doesn't work. Any suggestions ? Thanks in advance.

You need to check the return values from your trading functions . . . then report the error when they fail along with any relevant variables, please read this: What are Function return values ? How do I use them ? then you will know why it didn't work and then you can fix it instead of guessing or asking other people to fix it for you.
 

How can this be true for a SELL unless the stoploss has already been modified?

if(OrderOpenPrice()>OrderStopLoss())

If it has already been modified, you would be moving the stoploss further away from the current price.

 

Thanks a lot for both of you replays :) The error was this

if(OrderOpenPrice()>OrderStopLoss())

It should have been

if(OrderOpenPrice()<OrderStopLoss())

From now on I will check my return values :)

 
rori4:

Thanks a lot for both of you replays :) The error was this

It should have been

From now on I will check my return values :)

Reason: