Download MetaTrader 5

Move Stop To Break Even When Get to a Pivot Level

To add comments, please log in or register
Interested in the Market activity? Come and track!
Rangel Stoilov
199
Rangel Stoilov 2013.12.20 17:54 

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);
}
  
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.20 19:03  
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.
Keith Watford
Moderator
9624
Keith Watford 2013.12.20 22:00  

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.

Rangel Stoilov
199
Rangel Stoilov 2013.12.21 10:55  

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 :)

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.21 11:21  
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 :)

To add comments, please log in or register