Normal atr trailingstop

Viewing 13 posts - 31 through 43 (of 43 total)
  • Author
    Posts
  • #191778 quote
    deleted23092025
    Participant
    New

    Thanks for the answer none. Well it is not random going back in time when indices was much lower you could see that for example a distance of 5 could have MFE for example 50 euro and be a win still. But when indicies or the market has 2x that price the MFE tends to higher to make it a win (easily checked in bt). And btw.. I use it with atr distances > 10 also.

    grahal I run it on all tf basicly but I have noticed it is less robust with fewer trades and when the most trades are for example at a much lower level and the market has gone up much recently. As it would require a much higher MFE which does not seem logical to me.

    #191780 quote
    deleted23092025
    Participant
    New

    I must add does not seem logical to me and robust that way? But still it is the best trailingstop in times of results (gain, drawdown, losses).

    This type of atr trailing would be really good I think with scale with time acceleration (the longer time in market a bigger step). Because I run this on 1 hour tf also. Let say a system consolidates and you are at 1000 euro gain. Then ts wont move and after x days the system is still open with same gain and stoploss. That both costs money (overnight fees etc.) and maybe the entry that was taken days ago is not so validate anymore.

    #191781 quote
    robertogozzi
    Moderator
    Master

    You can increase your trailing stop by an additional X pips every N bars on market:

    Once N = 50            //50  bars
    Once X = 20 * PipSize  //20  pips
    Once M = 1.10          //1.10  multiplier
    MyTrailingStop = …
     .
     .
    If OnMarket and (((BarIndex - TradeIndex) MOD N) = 0) then
       MyTrailingStop = MyTrailingStop + X
       //MyTrailingStop = MyTrailingStop * M
    Endif

    I also included a MULTIPLIER line, instead of a constant value. You can choose between the two.

    thanked this post
    #191782 quote
    deleted23092025
    Participant
    New
    Once N = 50            //50  bars
    Once X = 20 * PipSize  //20  pips
    Once M = 1.10          //1.10  multiplier
    MyTrailingStop = atrtrail
    
    If OnMarket and (((BarIndex - TradeIndex) MOD N) = 0) then
    MyTrailingStop = MyTrailingStop + X
    Endif
    
    ONCE trailingStopType     = 1
    ONCE trailingstoplong     = 10
    ONCE trailingstopshort    = 6
    ONCE atrtrailingperiod    = 100
    ONCE minstop              = 1
     
     
    atrtrail = AverageTrueRange[atrtrailingperiod]((close/10)*pipsize)/1000
    trailingstartl = round(atrtrail*trailingstoplong)
    .... goes on with my stop posted.

    Where am I doing wrong here?

    #191783 quote
    robertogozzi
    Moderator
    Master

    Firstly you have to reference any variable AFTER it’s been defined (the code is executed sequentially), as atrtrail has not yet been defined in line 4.

    Secondly, if you write line 4 that way, you’ll always change it’s value each bar, thus making increases vanish. You should make sure it NEVER drops (if LongOnMarket) below the previous one:

    MyTrailingStop = max(atrtrail,MyTrailingStop)  //LONG  side
    MyTrailingStop = min(atrtrail,MyTrailingStop)  //SHORT side
    #191784 quote
    deleted23092025
    Participant
    New

    Okay, how should it be correctly implemented with original ts posted from start of thread?

    #191785 quote
    robertogozzi
    Moderator
    Master

    There you go:

    /*
    beyond adding the above lines, I also changed it a bit to make sure it never drops (when LongOnMarket) or rises (when ShortOnMarket) compared to the currently retained value
    */
    if not OnMarket then
    if close crosses over average[100] then
    buy at market
    elsif close crosses under average[100] then
    sellshort at market
    endif
    endif
    set stop   ploss   250
    set target pprofit 500
    
    ONCE trailingStopType     = 1
    ONCE trailingstoplong     = 10
    ONCE trailingstopshort    = 6
    ONCE atrtrailingperiod    = 100
    ONCE minstop              = 1
    Once N                    = 5             //5  bars
    Once X                    = 5 * PipSize   //5  pips
    Once M                    = 1.10          //1.10  Long  multiplier
    Once P                    = 0.90          //0.90  Short multiplier
     
    atrtrail = AverageTrueRange[atrtrailingperiod]((close/10)*pipsize)/1000
    trailingstartl = round(atrtrail*trailingstoplong)
    trailingstartS = round(atrtrail*trailingstopshort)
    if trailingStopType = 1 THEN
    TGL =trailingstartl
    TGS=trailingstarts
    if not onmarket then
    MAXPRICE = 0
    MINPRICE = close
    PREZZOUSCITA = 0
    ENDIF
    if longonmarket then
    MAXPRICE = MAX(MAXPRICE,close)
    if MAXPRICE-tradeprice(1)>=TGL*pointsize then
    if MAXPRICE-tradeprice(1)>=MINSTOP then
    PREZZOUSCITA = max(MAXPRICE-TGL*pointsize,PREZZOUSCITA)
    ELSE
    PREZZOUSCITA = max(MAXPRICE - MINSTOP*pointsize,PREZZOUSCITA)
    ENDIF
    If ((BarIndex - TradeIndex) MOD N) = 0 then
    PREZZOUSCITA = PREZZOUSCITA + X
    //PREZZOUSCITA = PREZZOUSCITA * M
    Endif
    ENDIF
    ENDIF
    if shortonmarket then
    MINPRICE = MIN(MINPRICE,close)
    if tradeprice(1)-MINPRICE>=TGS*pointsize then
    IF PREZZOUSCITA = 0 THEN
    PREZZOUSCITA = 9999999      //make sure there's a value large enough to be later used with MIN()
    ENDIF
    if tradeprice(1)-MINPRICE>=MINSTOP then
    PREZZOUSCITA = min(MINPRICE+TGS*pointsize,PREZZOUSCITA)
    ELSE
    PREZZOUSCITA = min(MINPRICE + MINSTOP*pointsize,PREZZOUSCITA)
    ENDIF
    If ((BarIndex - TradeIndex) MOD N) = 0 then
    PREZZOUSCITA = PREZZOUSCITA - X
    //PREZZOUSCITA = PREZZOUSCITA * P
    Endif
    ENDIF
    ENDIF
    if onmarket and PREZZOUSCITA>0 then
    EXITSHORT AT PREZZOUSCITA STOP
    SELL AT PREZZOUSCITA STOP
    ENDIF
    ENDIF
    graphonprice PREZZOUSCITA coloured(255,0,0,255)
    GraHal thanked this post
    #191786 quote
    robertogozzi
    Moderator
    Master

    Lines 1-3 are the newly supported multi-line comments. The “ADD PRT CODE” button doesn’t recognize them yet (ProOrder does, though), but will be updated asap.

    GraHal and Midlanddave thanked this post
    #191791 quote
    deleted23092025
    Participant
    New

    Thanks alot for that!!

    variabel N is number of bars it has gone when trailing has started (correct?) or is it when algo has entried?

    #191792 quote
    deleted23092025
    Participant
    New

    Either one, it would be good testing the other one.

    Example 1. count bars from entry (if trailing has started and it has gone like x bars on 1hour chart maybe the trade just got lucky and would be better to close faster as trailing has kicked in)

    Example 2. count bars from trailing (if trade was not “lucky” but has been a long time in trailing phase but not proceed)

    Which one is the code above and how can I change it to the other one.

    #191793 quote
    GraHal
    Participant
    Master

    Good questions and good ideas!

    I read ‘N’ as number of bars since trade entry without Trailing Stop started and so … get out quick with small profit as ExitPrice + X?

    This TS is getting better all the time and I am making myself learn stuff as I go along … onward and upward!!!  🙂

    Let’s see what Roberto has to say?

    thanked this post
    #191794 quote
    robertogozzi
    Moderator
    Master

    N is related to the start of the trade (TradeIndex). A difference is then computed each bar, with the current BarIndex, to detect when it reaches a multiple of N (I set it to 5, but yoyu can set it to any value, if yoy set it to, say, 99999 it will automatically be disabled, as it’s such a huge number that it will not easily reach a multiple of it!), each time that occurs it’s an increase of your trailing stop.

    Use GRAPH extensively to spot values and expressions:

    graph BarIndex
    graph TradeIndex
    graph (Barindex - TradeIndex) MOD N
    #191796 quote
    deleted23092025
    Participant
    New

    Ok so if I put for example 5 to N
    and X to 5.

    Does this mean after 5 bars, 5 pips will be added to trailing when its starting? And lets say after 20 bars.. and trailing has not started, when its starts it is going to add 5×20=100 pips to it and so on?

    If it is like that there will not be a possiblity to get it better? Because finding a combination that suits N and X would be almost impossible? Then it would be better to just after ANY bars close trade.

    ..

    I hope it works like this: If I put N to 5 it counts the bars and X is started FIRST when trailing is started (which I guess how it will work also)? And then X (in pips will be added each time trailing updates in this case 1hour timeframe)

Viewing 13 posts - 31 through 43 (of 43 total)
  • You must be logged in to reply to this topic.

Normal atr trailingstop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 42 replies,
has 5 voices, and was last updated by deleted23092025
3 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/12/2022
Status: Active
Attachments: 6 files
Logo Logo
Loading...