Hello,
I am trying to build a long-only strategy that incorporates three stop-losses in one.
- First of all, right when the trade is opened, the stop-loss should be 2% below the entry.
Next, if the price moves > 1% from the entry, the stop needs to become the highest value of the following two:
- The entry (moving the stop to breakeven)
- The lowest low in the last 5 candles (a trailing stop)
If the highest value of the two is hit, the strategy should immediately exit the long.
I’ve read through all the available posts about stop-losses on the forum and tried several code snippets, but I couldn’t get it to work…
I would appreciate your help a lot.
Thank you,
Pascal
Try this (not tested):
IF Not OnMarket THEN
NewSL = 0
ENDIF
IF close CROSSES OVER average[200,0](close) AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
EntryPrice = close
MySL = close * 0.02 //2%
MyTP = MySL * 3 //TP = 3 times Stop Loss
ENDIF
SET Stop Loss MySL
SET Target Profit MyTP
IF (close - TradePrice) > (EntryPrice * 1.01) THEN
NewSL = max(NewSL,max(EntryPrice, lowest[5](low)))
ENDIF
IF NewSL > 0 THEN
SELL AT NewSL STOP
ENDIF
Sorry, line 12 should be replaced by:
IF close > (EntryPrice * 1.01) THEN
That worked. Thank you, Roberto!
Hmm, I tried to replicate the same code snippet including shorting, and I can’t make it work…
Here’s what I have:
// SL reset
IF Not OnMarket THEN
NewLongSL = 0
ENDIF
IF Not OnMarket THEN
NewShortSL = 0
ENDIF
// Conditions To Enter and Exit Long
IF expansion AND bull AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
EntryPriceLong = close
MyLongSL = close * 0.02
ENDIF
SET Stop Loss MyLongSL
IF close > (EntryPriceLong * 1.01) THEN
NewLongSL = max(NewLongSL,max(EntryPriceLong, lowest[5](low)))
ENDIF
IF NewLongSL > 0 THEN
SELL AT NewLongSL STOP
ENDIF
// Conditions To Enter and Exit Short
IF expansion AND bear AND Not OnMarket THEN
SELLSHORT 1 CONTRACT AT MARKET
EntryPriceShort = close
MyShortSL = close * 0.02
ENDIF
SET Stop Loss MyShortSL
IF close < (EntryPriceShort * 0.99) THEN
NewShortSL = min(NewShortSL,min(EntryPriceShort, highest[5](high)))
ENDIF
IF NewShortSL > 0 THEN
EXITSHORT AT NewShortSL STOP
ENDIF
Do you have any ideas about what the issue could be?
1. line 18 must be moved just before line 16, otherwise it will ALWAYS be overridden by line 34
2. line 34 must be moved just before line 32, otherwise il will ALWAYS override line 18
Thank you, just made the above changes.
However, unfortunately, the trailing stop for the short side is still not working.
Here is the code with the changes you suggested:
// SL reset
IF Not OnMarket THEN
NewLongSL = 0
ENDIF
IF Not OnMarket THEN
NewShortSL = 0
ENDIF
// Conditions To Enter and Exit Long
IF expansion AND bull AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
EntryPriceLong = close
MyLongSL = close * 0.02
SET Stop Loss MyLongSL
ENDIF
IF close > (EntryPriceLong * 1.01) THEN
NewLongSL = max(NewLongSL,max(EntryPriceLong, lowest[5](low)))
ENDIF
IF NewLongSL > 0 THEN
SELL AT NewLongSL STOP
ENDIF
// Conditions To Enter and Exit Short
IF expansion AND bear AND Not OnMarket THEN
SELLSHORT 1 CONTRACT AT MARKET
EntryPriceShort = close
MyShortSL = close * 0.02
SET Stop Loss MyShortSL
ENDIF
IF close < (EntryPriceShort * 0.99) THEN
NewShortSL = min(NewShortSL,min(EntryPriceShort, highest[5](high)))
ENDIF
IF NewShortSL > 0 THEN
EXITSHORT AT NewShortSL STOP
ENDIF
Is there anything else that I am missing?
Try replacing line 19 and 35 with:
[scode]
IF close > (EntryPriceLong * 1.01) and LongOnMarket THEN
IF close < (EntryPriceShort * 0.99) and ShortOnMarket THEN
[/scode]
Made the addition. Still not working, unfortunately.
The error concerns trailing SHORT stop loss is in line 8, which should be a high number when reset:
NewShortSL = 999999
if it’s zero, then there will never be a price less than that (line 36).
You can add these lines (less, or more, according to your needs) to spot errors by monitoring values candle by candle (see pic):
graphonprice TradePrice - MyLongSL coloured(0,255,0,255) AS "SL long"
graphonprice TradePrice + MyShortSL coloured(255,0,0,255) AS "SL short"
graphonprice NewShortSL coloured(0,0,0,255) AS "Short TS"
graphonprice NewLongSL coloured(0,0,0,64) AS "Long TS"
graphonprice EntryPriceShort * 0.99 coloured(255,0,0,64) AS "Start TS Short"
graphonprice EntryPriceLong * 1.01 coloured(0,255,0,64) AS "Start TS Long"
That worked! Thank you so much, Roberto!!!