Three stoplosses, in one strategy
Forums › ProRealTime English forum › ProOrder support › Three stoplosses, in one strategy
- This topic has 10 replies, 2 voices, and was last updated 4 years ago by pascaltmn.
Tagged: loss, stop, stop loss, stoploss, trailing.trailing stop
-
-
03/06/2020 at 9:52 AM #121320
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
03/06/2020 at 11:51 AM #121336Try this (not tested):
1234567891011121314151617IF Not OnMarket THENNewSL = 0ENDIFIF close CROSSES OVER average[200,0](close) AND Not OnMarket THENBUY 1 CONTRACT AT MARKETEntryPrice = closeMySL = close * 0.02 //2%MyTP = MySL * 3 //TP = 3 times Stop LossENDIFSET Stop Loss MySLSET Target Profit MyTPIF (close - TradePrice) > (EntryPrice * 1.01) THENNewSL = max(NewSL,max(EntryPrice, lowest[5](low)))ENDIFIF NewSL > 0 THENSELL AT NewSL STOPENDIF1 user thanked author for this post.
03/06/2020 at 12:10 PM #121338Sorry, line 12 should be replaced by:
1IF close > (EntryPrice * 1.01) THEN1 user thanked author for this post.
03/06/2020 at 5:45 PM #12139103/06/2020 at 6:52 PM #121401Hmm, I tried to replicate the same code snippet including shorting, and I can’t make it work…
Here’s what I have:
Stoploss logic for long and short side1234567891011121314151617181920212223242526272829303132333435363738394041// SL resetIF Not OnMarket THENNewLongSL = 0ENDIFIF Not OnMarket THENNewShortSL = 0ENDIF// Conditions To Enter and Exit LongIF expansion AND bull AND Not OnMarket THENBUY 1 CONTRACT AT MARKETEntryPriceLong = closeMyLongSL = close * 0.02ENDIFSET Stop Loss MyLongSLIF close > (EntryPriceLong * 1.01) THENNewLongSL = max(NewLongSL,max(EntryPriceLong, lowest[5](low)))ENDIFIF NewLongSL > 0 THENSELL AT NewLongSL STOPENDIF// Conditions To Enter and Exit ShortIF expansion AND bear AND Not OnMarket THENSELLSHORT 1 CONTRACT AT MARKETEntryPriceShort = closeMyShortSL = close * 0.02ENDIFSET Stop Loss MyShortSLIF close < (EntryPriceShort * 0.99) THENNewShortSL = min(NewShortSL,min(EntryPriceShort, highest[5](high)))ENDIFIF NewShortSL > 0 THENEXITSHORT AT NewShortSL STOPENDIFDo you have any ideas about what the issue could be?
03/06/2020 at 7:10 PM #1214021. 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
1 user thanked author for this post.
03/06/2020 at 9:30 PM #121414Thank 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:
Updated long/short entry and exit rules1234567891011121314151617181920212223242526272829303132333435363738394041// SL resetIF Not OnMarket THENNewLongSL = 0ENDIFIF Not OnMarket THENNewShortSL = 0ENDIF// Conditions To Enter and Exit LongIF expansion AND bull AND Not OnMarket THENBUY 1 CONTRACT AT MARKETEntryPriceLong = closeMyLongSL = close * 0.02SET Stop Loss MyLongSLENDIFIF close > (EntryPriceLong * 1.01) THENNewLongSL = max(NewLongSL,max(EntryPriceLong, lowest[5](low)))ENDIFIF NewLongSL > 0 THENSELL AT NewLongSL STOPENDIF// Conditions To Enter and Exit ShortIF expansion AND bear AND Not OnMarket THENSELLSHORT 1 CONTRACT AT MARKETEntryPriceShort = closeMyShortSL = close * 0.02SET Stop Loss MyShortSLENDIFIF close < (EntryPriceShort * 0.99) THENNewShortSL = min(NewShortSL,min(EntryPriceShort, highest[5](high)))ENDIFIF NewShortSL > 0 THENEXITSHORT AT NewShortSL STOPENDIFIs there anything else that I am missing?
03/06/2020 at 9:56 PM #121415Try replacing line 19 and 35 with:
12IF close > (EntryPriceLong * 1.01) and LongOnMarket THENIF close < (EntryPriceShort * 0.99) and ShortOnMarket THEN03/07/2020 at 7:17 AM #12142603/07/2020 at 10:36 AM #121430The error concerns trailing SHORT stop loss is in line 8, which should be a high number when reset:
1NewShortSL = 999999if 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):
123456graphonprice 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"1 user thanked author for this post.
03/07/2020 at 2:19 PM #121437 -
AuthorPosts
Find exclusive trading pro-tools on