Trailing stop loss

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #135585 quote
    AntonC
    Participant
    Junior

    Hello,

    I read an article on here regarding a trailing stop loss (https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/). I copied the exact code into one of my strategies, but I seem to be doing something wrong or understanding it incorrectly.

    As i understood it, the stop ploss would first activate when it has moved (in my code) 50 points, and place the stop loss (again, in my code) 8 points over entry. After this it would move the stop loss 8 points for every 8 points the position moves in my direction. But the backtests doesn’t show that. In my backtests the stop less gets placed very weirdly and doesn’t trail the price. Am i coding something wrong or is it how it is supposed to work?

    I have inserted a printscreen of the backtest as well as my code.

    Regards,
    Anton

    defparam cumulateorders = false
    
    enterafter = time > 090000
    enterbefore = time < 220000
    
    
    
    distance = (close - open)
    c1 = (distance > 35)
    c2 = (Close > Dclose(1))
    
    
    
    
    Buyconditions = c1 and c2 and enterafter and enterbefore
    
    IF BuyConditions THEN
    BUY 0.4 CONTRACT AT MARKET
    set stop ploss 100
    ENDIF
    
    indicator1 = CALL "RSI retest under"
    
    
    c5 = (indicator1 => 1)
    
    
    SellConditions = c5
    
    IF SellConditions THEN
    SELL AT MARKET
    ENDIF
    
    
    
    
    trailingstart = 50 
    trailingstep = 8 
     
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    
    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
     
    
     
    
    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    
     
    GRAPH newSL as "trailing"
    #135586 quote
    AntonC
    Participant
    Junior

    Seems like the screenshot didn’t post(:

    2020-06-11-5.png 2020-06-11-5.png
    #135598 quote
    robertogozzi
    Moderator
    Master

    The price in your pic, where your mouse is sitting, is 27363.2 and you entry price was 27377.3, at that point you were suffering a loss, while your trailing stop would only activate when the closing price is at least 27377 + 50 pips.

    That’s why it is still 0.

    #135623 quote
    AntonC
    Participant
    Junior

    The mouse placement was random, didn’t mean to point anything out with it.

    If you look at the plotted graph of the trailing sl and at the price, you can see that price moved a lot more than 8 points after the newsl got triggered, but it remained at the same pricelevel.

    2020-06-11-1.png 2020-06-11-1.png
    #135631 quote
    robertogozzi
    Moderator
    Master

    Count entry price + 50 pips.

    You are summing up wrong values.

    #135844 quote
    AntonC
    Participant
    Junior

    okay, I dont think i fully understand unfortunately but how would I code for it to turn out the way i described before? I.e. the trailing stop gets activated when the price has moved 50 points/pips in my favour, and after that the trailing stop moves 8 points/pips up from the previous stop everytime the position moves 8 points/pips in my favour.

    #135851 quote
    robertogozzi
    Moderator
    Master

    Yes, that’s correct.

    #136224 quote
    keewee
    Participant
    Average

    I jump in with a follow upp question regarding the Trailing Stop Loss code.
    Is it possible to graph the “trailing” on a code running Live?

    #136225 quote
    GraHal
    Participant
    Master

    No, but you could run the same strategy in backtest at same time as running in ProOrder and then you will see the TS using GRAPH in backtest.

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

Trailing stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
AntonC @antonc Participant
Summary

This topic contains 8 replies,
has 4 voices, and was last updated by GraHal
5 years, 8 months ago.

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