Trailing Stop

Timothy Smith
233
Timothy Smith  

Hi MQL4 Experts,

I'm trying to code a function that moves my buy order to breakeven after the price moves X amount AGAINST me.

Here'a the code. Can you please point out where I'm going wrong.

Cheers, Tim


void MoveToBE()
{
   for(int b=OrdersTotal()-1; b >= 0; b--)
        {
        
        double ATR = NormalizeDouble(iATR(NULL,PERIOD_D1,4,1),5);
      
      double ATRBE = (ATR/3.75)*1.1;
      double ATRBESTOP = NormalizeDouble(ATRBE,5);
      
        if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()== MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
            if(OrderOpenPrice()-Ask>ATRBESTOP)
                     if(OrderOpenPrice()<OrderStopLoss())    
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss,OrderTakeProfit(),0,CLR_NONE))
                           Print("error modifying sell order ",GetLastError()); 
    }
       
}     
Keith Watford
Moderator
31760
Keith Watford  
Timothy Smith:

Hi MQL4 Experts,

I'm trying to code a function that moves my buy order to breakeven after the price moves X amount AGAINST me.

Here'a the code. Can you please point out where I'm going wrong.

Cheers, Tim


if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss,OrderTakeProfit(),0,CLR_NONE))

You are not modifying anything. SL and TP is exactly the same as prior to you trying to modify the order

Timothy Smith
233
Timothy Smith  

Thanks for your input Keith,

I'd like to modify the buy order back to the open price. Do I replace TP and SL with OpenOrderPrice()?

Cheers, Tim

Keith Watford
Moderator
31760
Keith Watford  
Timothy Smith:

Thanks for your input Keith,

I'd like to modify the buy order back to the open price. Do I replace TP and SL with OpenOrderPrice()?

Cheers, Tim

Timothy Smith:

I'm trying to code a function that moves my buy order to breakeven after the price moves X amount AGAINST me.


You would have to modify the TP

William Roeder
25313
William Roeder  
            if(OrderType()==OP_BUY)
            if(OrderOpenPrice()-Ask>ATRBESTOP)
                     if(OrderOpenPrice()<OrderStopLoss())    
                        if(!OrderModify(…
  1. You buy at the Ask and sell at the Bid. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it.

  2. If the market has moved below the OOP, then the OSL has not been hit. It can't possibly be above the OOP.

  3. If the market has moved below the OOP, moving to BE would be above the market.

    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.

Timothy Smith
233
Timothy Smith  
Thanks for your help guys. I've tried the suggested changed but unfortunately, I still can't get it to work. Do you have any other suggestions to try? Cheers, Tim
Kenneth Parling
21897
Kenneth Parling  
Timothy Smith:
Thanks for your help guys. I've tried the suggested changed but unfortunately, I still can't get it to work. Do you have any other suggestions to try? Cheers, Tim
I'm trying to code a function that moves my buy order to breakeven after the price moves X amount AGAINST me.

Wouldn't you want to set break even when your position has moved in plus....never heard of anyone doing the opposite....

Timothy Smith
233
Timothy Smith  
That's the whole point!. The strategy has a consistently downward trending equity curve over six months, so I thought, heck I'll code it to do the exact opposite! Happy for further suggestions regarding the contrarian BE stop!