Updated version of trailing SL

Forums ProRealTime English forum ProOrder support Updated version of trailing SL

Viewing 15 posts - 1 through 15 (of 20 total)
  • #44175

    Hi,

    I tried to post a comment here in another thread (https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/) but could add code, so I’ll post it here instead.

    This has been in the back of my mind for quite some time, and I finally got around to work on it.

    I have changed the way that the SL is updated after first being initialized. Instead of moving in increments of TrailingStep, the newSL is now based on the close of the highest candle since the trade was entered.

    So imagine if a trade goes our way, with 5 30 pip candles (!) after the SL was first initialized. Then, to use TrailingStep of 5 (as above), the SL would move 5*5 (=25) pips in the original version. If we instead base the SL on the previously closed candle and we use the TrailingStart of 20 as above, we lock in 130 out of the 150 pips.

    Since I’m working on pretty short time frames, this suits me better. Hopefully someone else can make use of it as well.

    I had to introduce the variable CAND to find the highest/lowest close from the time the trade was entered, instead of simply use higher/lower close since there can be spikes and dips. And, like I mentioned before  – TrailingStart is now the difference between the highest/lowest close and the SL (aka how much leg room you’ll allow price) and the TrailingStep is how much you’d like to lock in when price have gone TrailingStart in your way.

    No offence, Nicolas – I’m just trying to improve it!

    Comments are more than welcome!

    Tobias

     

    6 users thanked author for this post.
    #44188

    No offence at all 🙂 Thanks a lot for sharing this improvement!

    #44305

    Here’s an slightly updated version:

    Enjoy!

    5 users thanked author for this post.
    #54834

    Hi, is it working like the Nicholas Version? We just need to insert at the end of any of ourt trade system?

    #54979
    Leo

    I rewrite it

    instead of

    I use:

    Someone knows Why for me is not working your codes?

    thanks in advance

     

     

    #55214

    Hi,

    I understand that this type of trailing does not work in a 4h candle, if all the action occurs within those four hours. I am backtesting and seeing that big candles do not read trailing information if everything happens in one candle.

    I understand that the code reads the changes at the CLOSE, so if everything happens in just one candle nothing happens.

    EXAMPLE: trailingstart = 20, trailingstep=10, TF:H4, LONG candle, open 1,1310, close 1,1329, maximum 1,1360…..nothing happens, the operation continue in the next candle and goes wrong…

    Can anyone confirm this??? 

    Thanks,

    Juan

     

    #57547

    Hi Juan. I also have similar problems: I understand that the important value of the candle is the low value (for long trades). That means that, if in some moment of the candle (4H or day candle, as I use) the price touches the already activated newSL, the trade should stop immediately because this is the given instruction code: IF xxx SELL AT newSL STOP.

    But also in my case, there are many candles where the trailing stop doesn’t work properly, and the problem is that I don’t know the reason.

    Could anyone help us?

    #57551
    AVT

    A general remark how I set my trailing stops (as a highspeed scalper I do not allow ANY kind of retracements):

    Base: a long trend is defined as higher highs and higher lows.

    Going long: first a security stop (1 Min. DAX 10 points).

    Second step: Break even, if in profit (caculated with 2*spread+2*slippage expressed in Euro).

    Third step: Set absolute stop at low of previous candle-(spread+slippage).

    Repeat each minute until getting stopped out.

    A violation of the previous bars low means no longer a long trend by definition, hard but simple.

    I understand that for a H4 or daily that would be sometimes a lot of points – that’s why I am no longtermer – but with this I do not have to play with such things as “after how many points to start for how many points”. It just trails each bar with the low of the previous bar. // Remark: during PRT using time I did not robot, but I do with MT.

    #68891

    Hi AVT,

    could you share your code for this kind of SL you use?

    Thanks

    #99145

    Hi AVN – I’m using a similar strategy, would you be able to share the code you use so I can add this change into mine…?

    #231558

    Is it possible to code this stop with percentage, like always move the stopp 0.6% from the highest candel while trade are active?

    #231591
    JS

    Hi,

    You can change these lines in the trailing stop…

    (There are a number of errors in this trailing stop)

     

    IF newSL > 0 and Close[1] >= Highest[CAND](Close) THEN

    newSL = Close[1] – (0.006*Close[1])

     

    IF newSL > 0 and Close[1] <= Lowest[CAND](Close) THEN

    newSL = Close[1] +  (0.006*Close[1])

    #231633

    Thanks for your answer, one question more. Iam not that good att this codeing.

    If i want a trailing stopp that start when i am 0.1% profit and it should trailing 0.3% from tradingprice, this will work as i want for a long position?

    TrailingPercent = 0.1
    Trailingdistans = 0.003
    if onmarket then
    trailingstart = tradeprice(1)*(trailingpercent/100)
    endif

    //Reset the trailing value
    IF NOT ONMARKET THEN
    newSL=0
    CAND = 0
    ENDIF

    //Manage long positions
    IF LONGONMARKET THEN
    IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
    newSL = Close[1] – (trailingdistans*Close[1])
    ENDIF
    //next moves
    CAND = BarIndex – TradeIndex
    IF newSL > 0 and Close[1] >= Highest[CAND](Close) THEN
    newSL = Close[1] – (trailingdistans*Close[1])
    ENDIF
    ENDIF

    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF

    #231634
    JS

    Hi

    Yes, looks good (long only)…

    What you might need to pay attention to is that when the trailing stop has started, the new stop loss (newSL) is placed immediately, below the previous close… So there is no bar in between…

    #231635

    Yes this was just a exampel on the percentage, i am going to look into what value i will use when the code is done.

    I was doing a research on a algo with backtest and useing “SET STOP %TRAILING” but when i as going to start on demo that code wasnt okey to use.

    The code i write above  had a hole, if the trade goes up 0.05% i have now stop so now i am rewrite it to below.

    And a again, really Appreciate your time into this.

    // Stops and targets
    SET STOP %LOSS 0.6
    SET TARGET %PROFIT 1.2
    //%trailing stop function
    trailingPercent = 0.1
    Trailingdistans = 0.005
    if onmarket then
    trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
    endif

    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    CAND = 0
    ENDIF

    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
    newSL = Close[1] – (trailingdistans*Close[1])
    ENDIF
    //next moves
    CAND = BarIndex – TradeIndex
    IF newSL > 0 and Close[1] >= Highest[CAND](Close) THEN
    newSL = Close[1] – (trailingdistans*Close[1])
    ENDIF
    ENDIF

    //stop order to exit the positions
    IF newSL>0 AND CLOSE < NEWSL THEN
    SELL AT MARKET
    ENDIF

Viewing 15 posts - 1 through 15 (of 20 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login