Stop Loss that moves every time a certain amount of pips is reached.

 

Hi all,


I'm fairly new to mql4 so I'm having a hard time with this and any help would be greatly appreciated.  I'm looking to move my stoploss only when the price reaches x amount of pips and then again when that same amount of pips is reached again.

So example:  buy at .67000, price reaches .68000, move the stoploss to .67000 so breakeven.  Then the stoploss does not move again until price hits .69000 then it moves to .68000.  So the stoploss is only moved every 100 pips and remain there until the next 100 pips is reached.  I hope this makes sense.


The trouble I am having is I don't know if there is anyway to get this to calculate automatically in a for loop or another way.  As I said I'm new to mql4 and the only way I seem to be able to figure it out is to put about 50 if states together.  Here is the only way I seem to be able to do it.  Example would be for a buy order:



   OrderSelect(OrderNumber,SELECT_BY_TICKET);

   if(Ask == OrderOpenPrice() + 0.01000)  //Price hits 100 pips from open price
   {
   OrderModify(OrderNumber,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0)  //moving stop loss to break even when 100 pips is met.
   }
   else if(Ask == OrderOpenPrice() + 0.02000) //Price now hits 200 pips from open price
   {
      OrderModify(OrderNumber,OrderOpenPrice(),OrderOpenPrice() + 0.02000,OrderTakeProfit(),0) //move stop loss to 100 pips profit only when price is 200 pips from order open price.
   }


The OrderNumber, 0.01000 and 0.02000 are just for example purposes.  Just showing that the if statement is the only way I know how to do this.


If this is the only way I'm good with adding a bunch of if statements, but wasn't sure if there was a much for efficient way that I'm missing.  As I said any help would be greatly appreciated.


Thanks in advance

 

Not tested but should give you some ideas

   double step=0.0100;

   if(OrderSelect(OrderNumber,SELECT_BY_TICKET))
      if(OrderType()==OP_BUY && OrderCloseTime()==0)
         {
         if(OrderOpenPrice()-OrderStopLoss()>Point/2)
            {
            if(OrderClosePrice()-OrderOpenPrice()>=step)
               if(!OrderModify(OrderNumber,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0))  //moving stop loss to break even when 100 pips is met.
                  Print("Error Modifying #",OrderTicket()," Error Code ",GetLastError());
            }
         else
            {
            if(OrderOpenPrice()-OrderStopLoss()>=step)
               {
               if(!OrderModify(OrderNumber,OrderOpenPrice(),NormalizeDouble(OrderStopLoss()+step,Digits),OrderTakeProfit(),0))  //moving stop loss by step.
                  Print("Error Modifying #",OrderTicket()," Error Code ",GetLastError());
               }
            }
         }
 
  1.  if(Ask == OrderOpenPrice() + 0.01000

    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07

  2.    else if(Ask == OrderOpenPrice() + 0.02000) //Price now hits 200 pips from open price
       {
          OrderModify(OrderNumber,OrderOpenPrice(),OrderOpenPrice() + 0.02000,

    This is attempting to move the SL to the Ask.

    You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

    On some ECN type brokers the value might be zero (broker doesn't know.) Use a minimum of two (2) PIPs.

  3. + 0.02000) //Price now hits 200 pips from open price

    PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum 2014.08.03

    Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points. Your 0.0200 breaks on JPY pairs.
              How to manage JPY pairs with parameters? - MQL4 programming forum 2017.02.09
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum 2018.01.15

  4.    OrderSelect(OrderNumber,SELECT_BY_TICKET);

    you select by ticket, but don't check if it has already been closed (or still pending).

  5. Posted code currently assumes a buy order, but You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  6. ctrading:   buy at .67000, price reaches .68000, move the stoploss to .67000 so breakeven.  Then the stoploss does not move again until price hits .69000 then it moves to .68000. 
    double newSL = Bid - 100*pip;
    double minSL = OrderStopLoss() < OrderOpenPrice()
                 ? OrderOpenPrice()
                 : OrderStopLoss() + 100*pip;
    if(newSL > minSL) …
    
 
Thank you both for the help and tips.  I do appreciate you taking the time.
Reason: