Download MetaTrader 5

Move Stop to Breakeven code

To add comments, please log in or register
Track all payments in your profile. It is easy!
MisterDog
381
MisterDog 2012.07.02 06:08 

I'd like to learn more about open orders -- getting information and modifying. Moving the stop to breakeven should be a good project. I found plenty of discussion on the subject but no finished code illustrating the details of how it's really done. Here's a few references if anyone wants to see what's out there:

And here's a rough draft of what I have in mind:

void stopBreakeven()
{
   // some condition that will trigger the "move stop to breakeven" function
   OrderSelect() // to select the order
   OrderTicket() // to get the ticket number
   OrderOpenPrice() // to get the current order open price
   OrderStopLoss( )  // to get the current stop loss price
   Bid or Ask // to get the current market price
   OrderModify() // moved the stop
   GetLastError() // report errors
   Sleep() // sleep if errors
   RefreshRates() // Refresh the rates
   // Try again to move the stop
   
   // Do this for both buy and sell
   return;
}
MisterDog
381
MisterDog 2012.07.03 01:52  

This seems to work well...

// when an order is closed stopMoved is set to 0

void stopBreakeven()
{ 
   int err;
   if ( OrderType() == OP_BUY 
   && (OrderProfit()*Point*10)+0.0010
   && stopMoved == 0 )
   {
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop up to break-even +1

      err = GetLastError();
      Print("OrderModify Buy Error:",err);
      
      if (err == 0)
          stopMoved = 1;   
   }
   
   if ( OrderType() == OP_SELL 
   && (OrderProfit()*Point*10)+0.0010 
   && stopMoved == 0 )
   {
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop down to break-even +1

      err = GetLastError();
      Print("OrderModify Sell Error:",err);

      if (err == 0)
         stopMoved = 1;
   }
   return;
} // end stopBreakeven
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2012.07.03 06:55  
MisterDog:

This seems to work well...

Really ?

This is not a bool . . . what does it do ?

OrderProfit()*Point*10)+0.0010

Why do you print the error even if there isn't one ?

   OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop up to break-even +1

      err = GetLastError();
      Print("OrderModify Buy Error:",err);

Why not make stopmoved a bool ? and use the function to return it ?

Does you code ensure that you do not try to set the same SL a second or more times ? if not you will get error 1

MisterDog
381
MisterDog 2012.07.03 10:37  

Oops, the "OrderProfit()*Point*10)+0.0010" is intended to make sure there are 10 pips profit before moving the stop. But you're right that piece of code is not going to do it.

Okay -- I'll try to make these other changes as well.

whroeder1
14532
whroeder1 2012.07.04 13:11  
MisterDog:

Oops, the "OrderProfit()*Point*10)+0.0010" is intended to make sure there are 10 pips profit before moving the stop. But you're right that piece of code is not going to do it.

OrderProfit is a Account Currency (dollar) amount.

Don't hard code numbers (Point*10) makes your EA incompatible with 4 digit broker AND the 0.0010 makes it incompatible with the xxxJPY pairs and metals.
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                             OptInitialization();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    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)
       Alert("OrderModify failed: ", GetLastError());
     */
Might try (buy) OrderClosePrice() - OrderOpenPrice() >= 10*pips2dbl
tasaoirse
34
tasaoirse 2017.03.18 15:59  
MisterDog:

This seems to work well...


does this code works "to take 1 pip as takeprofit"??


the code below..


void stopBreakeven()
{ 
   int err;
   if ( OrderType() == OP_BUY 
   && (OrderProfit()*Point*10)+0.0010
   && stopMoved == 0 )
   {
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop up to break-even +1

      err = GetLastError();
      Print("OrderModify Buy Error:",err);
      
      if (err == 0)
          stopMoved = 1;   
   }
   
   if ( OrderType() == OP_SELL 
   && (OrderProfit()*Point*10)+0.0010 
   && stopMoved == 0 )
   {
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop down to break-even +1

      err = GetLastError();
      Print("OrderModify Sell Error:",err);

      if (err == 0)
         stopMoved = 1;
   }
   return;
} // end stopBreakeven
To add comments, please log in or register