Trigger Trailing SL if target is met

Viewing 15 posts - 1 through 15 (of 69 total)
  • Author
    Posts
  • #198152 quote
    Mr.Mjau
    Participant
    New

    Hi

    I now use this in one of my programs

    SET TARGET pPROFIT 425

    Is there a way to make it so if 425 is hit my position is not sold but a trailing stoploss kicks in?

    #198215 quote
    robertogozzi
    Moderator
    Master

    Simlply remove SET TARGET pPROFIT 425 and append this code snippet to your strategy. You only need to set the TRAILINGSTART (I set it to 425 as you rquested) and the TRAILINGSTEP (the pace at which the trailing stop will be updated):

    //*********************************************************************************
    // https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
    // (lines 17- 56)
    //
    //trailing stop function
    trailingstart = 425   //trailing will start @trailinstart points profit
    trailingstep  = 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 close-tradeprice(1)>=trailingstart*pipsize THEN
      newSL = tradeprice(1)+trailingstep*pipsize
     ENDIF
    //next moves
     IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
      newSL = newSL+trailingstep*pipsize
     ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
     IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
      newSL = tradeprice(1)-trailingstep*pipsize
     ENDIF
    //next moves
     IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
      newSL = newSL-trailingstep*pipsize
     ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
     SELL AT newSL STOP
     EXITSHORT AT newSL STOP
    ENDIF
    //*********************************************************************************
    Mr.Mjau thanked this post
    #198266 quote
    Mr.Mjau
    Participant
    New

    Tank you Roberto for your fast answer.

    I have put in the code in one of my long robots and one of my short, but the results I get from backtesting makes no sence. For example changing the trailingstep from 5 to 6 could change the backtesting results by 40% and that is just not possible with such a small change, I trade the DAX and 1 piont is nothing. Every number I used as trailingstop gave a worse result the just useing target profit. I even tried to set trailingstep to 0 to see if I would get the same results as just “SET TARGET pPROFIT 425” and it did not give the same result.

    If you have any ide where I went wrong it would be much appreciated.

    #198276 quote
    GraHal
    Participant
    Master

    Optimise the start and the step using …

    Start:    min 25  max 500 increment 25

    Step   min 10 max 100 increment 10

    Tell us how you get on

    Mr.Mjau thanked this post
    #198299 quote
    Mr.Mjau
    Participant
    New

    Tried both my short and long with alot of different numbers. My short gave great results where all test improved backtesting with 5-10%, but the long gives alot of strange numbers.

    Made a matrix in excel and attached

    Matrix.xlsx
    #198306 quote
    Mr.Mjau
    Participant
    New
    EXITSHORT AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 165
    //trailing stop function
    trailingstart = 405   //trailing will start @trailinstart points profit
    trailingstep  = 30     //trailing step to move the "stoploss"
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    EXITSHORT AT newSL STOP
    ENDIF
    
    SELL AT MARKET
    ENDIF
     
    // Stops and targets
    trailingstart = 425   //trailing will start @trailinstart points profit
    trailingstep  = 25     //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 close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
    
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    

    The first one is my short that is working fine, and the second one is my long that I have problems with.

    #198310 quote
    GraHal
    Participant
    Master

    You still have SET STOP pLOSS 165 in yuor Short so how do you know exits are not due to SET STOP pLOSS 165?

    It is quite time consuming going over trades and only yourself will have the motivation.

    Enter as a  last line of code …

    GRAPH newSL as “trailing”

    and try and work out what is happening re exits … zoom in 1 or 2 trades and look at price levels together with the line for NewSL and fit it altogether in your mind … it can be interesting and enlightening! 🙂

    Mr.Mjau thanked this post
    #198312 quote
    Mr.Mjau
    Participant
    New

    Yes this is really fun, and having you guys help is priceless. I have one more Q before I dive deep in grafs and numbers, that is close to my other Q.

    I have this 3th robot that end like this

    // Conditions to exit long positions
    indicator3 = ExponentialAverage[20](close)
    indicator4 = ExponentialAverage[100](close)
    c2 = (indicator3[2] CROSSES OVER indicator4[2])
    
    IF c2 THEN
    SELL AT MARKET
    ENDIF
    
    // Stops and targets
    SET STOP pLOSS 500
    

    Is there some way to make c2 to not trigger a “sell at market” but to trigger a Trailing stoploss?

    #198313 quote
    Mr.Mjau
    Participant
    New

    at the c2 level, (sry)

    #198319 quote
    robertogozzi
    Moderator
    Master

    You cannot have twice the same code, as it’s read sequentially and the second one will override the first one.

    #198320 quote
    GraHal
    Participant
    Master

    Try this …

    If c2 Then
    // Stops and targets
    trailingstart = 425   //trailing will start @trailinstart points profit
    trailingstep  = 25     //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 close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    Endif
    #198322 quote
    Mr.Mjau
    Participant
    New

    Tank you for taking your time to help me. In this one I’m not intrested in 425 starting point, but would like it to start trailing at c2. Would this work, just changing 425 to c2?

    If c2 Then
    // Stops and targets
    trailingstart = c2 //trailing will start @trailinstart points profit
    trailingstep  = 25     //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 close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    Endif
    #198323 quote
    GraHal
    Participant
    Master

    Would this work, just changing 425 to c2?

    No, because C2 can only be True / 1 or False / 0.  You can’t put 1 or 0 in place of 425.

    c2 = (indicator3[2] CROSSES OVER indicator4[2])

    1. If  indicator3[2] CROSSES OVER indicator4[2]  then C2 = True / 1.
    2. If indicator3[2] CROSSES OVER indicator4[2] does NOT occur then C2 = False / 0.

    Mr.Mjau thanked this post
    #198333 quote
    Mr.Mjau
    Participant
    New

    Ok ,but how would I then go about if I want a trailing stop to start at just that level where  indicator3<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]</span> <span class=”crayon-st”>CROSSES OVER</span> indicator4<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]? Is it possible to  store this value in a constant that I can later use in “trailingstarts?</span>

    #198334 quote
    GraHal
    Participant
    Master

    I want a trailing stop to start at just that level

    To achieve above, I provided you with the code in the post below

    Trigger Trailing SL if target is met

    Mr.Mjau thanked this post
Viewing 15 posts - 1 through 15 (of 69 total)
  • You must be logged in to reply to this topic.

Trigger Trailing SL if target is met


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Mr.Mjau @mr-mjau Participant
Summary

This topic contains 68 replies,
has 5 voices, and was last updated by Mr.Mjau
3 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/30/2022
Status: Active
Attachments: 17 files
Logo Logo
Loading...