Three stoplosses, in one strategy

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #121320 quote
    pascaltmn
    Participant
    New

    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

    #121336 quote
    robertogozzi
    Moderator
    Master

    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
    pascaltmn thanked this post
    #121338 quote
    robertogozzi
    Moderator
    Master

    Sorry, line 12 should be replaced by:

    IF close > (EntryPrice * 1.01) THEN
    pascaltmn thanked this post
    #121391 quote
    pascaltmn
    Participant
    New

    That worked. Thank you, Roberto!

    #121401 quote
    pascaltmn
    Participant
    New

    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?

    #121402 quote
    robertogozzi
    Moderator
    Master

    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

    pascaltmn thanked this post
    #121414 quote
    pascaltmn
    Participant
    New

    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?

    #121415 quote
    robertogozzi
    Moderator
    Master

    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]

    #121426 quote
    pascaltmn
    Participant
    New

    Made the addition. Still not working, unfortunately.

    #121430 quote
    robertogozzi
    Moderator
    Master

    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"
    pascaltmn thanked this post
    x-4.jpg x-4.jpg
    #121437 quote
    pascaltmn
    Participant
    New

    That worked! Thank you so much, Roberto!!!

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Three stoplosses, in one strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
pascaltmn @pascaltmn Participant
Summary

This topic contains 10 replies,
has 2 voices, and was last updated by pascaltmn
5 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/06/2020
Status: Active
Attachments: 1 files
Logo Logo
Loading...