Modified Nicolas' Trailing Stop code

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #73306 quote
    robertogozzi
    Moderator
    Master

    Sometimes it may happen that your target is not reached, but price went in your favour and you want to save some of the profits earned so far, that’s why the Trailing Stop exist!

    But in automated trading (at least until MTF will be supported) and mainly on higher TFs, Nicolas’code to support a good Trailing Stop is executed only when a bar closes, leaving room for price to pullback or even reverse, despite your trailing stop may have been triggered while the bar was forming, simply because when the bar closes price has retraced to a lower point < to your trailing stop treshold and the code is fooled into believing the price never reached that treshold!

    So, while awaiting MTF to come, I replaced CLOSE with HIGH and LOW, respectively, for Long and Short trades. So the code will compare TRADEPRICE with a higher/lower price than before, the price you would use manually when in front of your charts.

    When the bar closes and the code is executed, it will set a new SL according to those new prices in relation with TRADEPRICE and set a new SL where it wouldn’t have been set before, or closing immediately ifa price has fallen below TRADEPRICE!

    //************************************************************************
    //					trailing stop function
    trailingstart = 10   //10   trailing will start @trailinstart points profit
    trailingstep  = 5    //5    trailing step to move the "stoploss"
    //
    //reset the stoploss value
    IF NOT ONMARKET THEN
       newSL=0
    ENDIF
    //manage long positions
    IF LONGONMARKET THEN
       //first move (breakeven)
       IF newSL=0 AND HIGH-tradeprice(1)>=trailingstart*pipsize THEN                 //close --> HIGH
          newSL = tradeprice(1)+trailingstep*pipsize
    	  //  new coding
    	  IF newSL > close THEN                                                  //if current closing price is < new SL then exit IMMEDIATELY!
    	     SELL AT MARKET
    	  ENDIF
    	   //  end new coding
       ENDIF
       //next moves
       IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
          newSL = newSL+trailingstep*pipsize
    	  //  new coding
    	  IF newSL > close THEN                                                  //if current closing price is < new SL then exit IMMEDIATELY!
    	     SELL AT MARKET
    	  ENDIF
    	  //  end new coding
       ENDIF
    ENDIF
    //manage short positions
    IF SHORTONMARKET THEN
       //first move (breakeven)
       IF newSL=0 AND tradeprice(1)-LOW>=trailingstart*pipsize THEN                 //close --> LOW
          newSL = tradeprice(1)-trailingstep*pipsize
    	  //  new coding
          IF newSL < close THEN                                                     //if current closing price is > new SL then exit IMMEDIATELY!
             EXITSHORT AT MARKET
          ENDIF
          //  end new coding
       ENDIF
       //next moves
       IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
          newSL = newSL-trailingstep*pipsize
    	  //  new coding
          IF newSL < close THEN                                                    //if current closing price is > new SL then exit IMMEDIATELY!
             EXITSHORT AT MARKET
          ENDIF
          //  end new coding
       ENDIF
    ENDIF
    //stop order to exit the positions
    IF newSL>0 THEN
       SELL      AT newSL STOP
       EXITSHORT AT newSL STOP
    ENDIF
    //************************************************************************

    Should anyone take some further risk of a higher loss and aim at closing at breakeven instead of a loss, in case price has fallen below entry point, lines 17 and 26 can be replaced by:

    SELL AT newSL LIMIT

    and lines 38 and 47 with:

    EXITSHORT AT newSL LIMIT

    I tested it a bit and found out that it yields slightly poorer performances, but less losing trades and less drawdown.

    In a few months this post will, hopefully, be superseded by MTF.

    GraHal, Nicolas, immo_vienna and 3 others thanked this post
    x2-1.jpg x2-1.jpg
    #73311 quote
    robertogozzi
    Moderator
    Master

    The last lines to replace some code should be used IF price has fallen below ENTRY POINT, otherwise with the original lines the trade would be closed in profit, as from the screenshot that I attached.

    #73318 quote
    GraHal
    Participant
    Master

    That is a very useful improvement Roberto, thank you so much!

    I have added, as row 41, to here Snippet Link Library

    robertogozzi, Nicolas and Midlanddave thanked this post
    #173093 quote
    sulimaster
    Participant
    Average

    Hi and thanks for above trailing stop loss code.

    Is there a multi time frame trailing stop loss code snippet that you can share or direct me to please?

    Thanks

    Sachin

    #173097 quote
    robertogozzi
    Moderator
    Master

    To use it in an MTF system, simply add as first line:

    Timeframe(default)

    then use any other TF in your system.
    Usually this snippet is appended to strategies.

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

Modified Nicolas' Trailing Stop code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by robertogozzi
4 years, 7 months ago.

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