Hi,
How can I code a time-based stop loss, meaning if, for example, a short trade is going on for more than a specified length of time (ie. 20 hours), then it should move the stop loss to breakeven minus 2 pips?
Thanks.
Sascha
You can use the number of elapsed bars:
IF Not OnMarket THEN
MyProfit = 0
NewSL = 0
ENDIF
IF LongOnMartket THEN
MyProfit = close - TradePrice
ELSIF ShortOnMartket THEN
MyProfit = TradePrice - close
ENDIF
IF Barindex >= (TradeIndex + 10) AND MyProft > 0 AND NewSL = 0 THEN //Count 10 bars before setting Break Even
NewSL = TradePrice
ENDIF
IF NewSL THEN
IF LongOnMarket THEN
SELL AT NewSL STOP
ELSIF ShortOnMarket THEN
EXITSHORT AT NewSL STOP
ENDIF
ENDIF
or use the MTF support to exactly count hours:
TIMEFRAME(????,UpDateOnClose)
.
.
TIMEFRAME(1 hour,UpdateOnClose)
IF Not OnMarket THEN
MyProfit = 0
NewSL = 0
Count = 0
ELSE
Count = Count + 1
ENDIF
IF LongOnMartket THEN
MyProfit = close - TradePrice
ELSIF ShortOnMartket THEN
MyProfit = TradePrice - close
ENDIF
IF Count >= 20 AND MyProfit > 0 AND NewSL = 0 THEN //Count 20 hours before setting Break Even
NewSL = TradePrice
ENDIF
IF NewSL THEN
IF LongOnMarket THEN
SELL AT NewSL STOP
ELSIF ShortOnMarket THEN
EXITSHORT AT NewSL STOP
ENDIF
ENDIF
.
.
TIMEFRAME(default)
.
.