Realtime trailing-stop not consistent to backtest

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #98603 quote
    Paul
    Participant
    Master

    The trailing stop I use is not consistent in real trading. I can’t figure out the reason, because sometimes it is and sometimes it isn’t! The backtest shows it correct.

    The cause is the trailing stop and there’s no profit-target.

    The question is, why is it selling at 0.35% sharp while that’s the level the trailing-stop should kick in? It works beautifully in the backtest, but it has to work realtime the same, all the time!

    The “underlaying” only purpose is to make this code adjustable for other markets.

    On 13 and 14th it worked correctly.

    In the screenshots there are 2 different strategies and both executed at the same time or about.

    once enablets = 1 // trailing stop
    once displayts = 1 // trailing stop
    
    ts1=0.35
    ts2=0.30
    ts3=0.20
    
    switch =ts3+ts2
    switch2=ts2+ts1
    
    underlaying=100

     

    if enablets then
    
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    trailingstop1 = (tradeprice(1)/100)*ts1
    trailingstop2 = (tradeprice(1)/100)*ts2
    trailingstop3 = (tradeprice(1)/100)*ts3
    endif
    
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    maxprice1=0
    minprice1=close
    priceexit1=0
    
    maxprice2=0
    minprice2=close
    priceexit2=0
    
    maxprice3=0
    minprice3=close
    priceexit3=0
    
    a1=0
    a2=0
    a3=0
    
    pp=0
    endif
    
    if longonmarket then
    pp=((close/tradeprice(1))-1)*100
    if pp>=ts1 then
    a1=1
    endif
    if pp>=switch then
    a2=1
    endif
    if pp>=switch2 then
    a3=1
    endif
    elsif shortonmarket then
    pp=((close/tradeprice(1))-1)*-100
    if pp>=ts1 then
    a1=1
    endif
    if pp>=switch then
    a2=1
    endif
    if pp>=switch2 then
    a3=1
    endif
    endif
    
    //first leg long
    if longonmarket then
    maxprice1=max(maxprice1,close)
    if a1 then
    if maxprice1-tradeprice(1)>=(trailingstop1) then
    priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
    endif
    endif
    endif
    //first leg short
    if shortonmarket then
    minprice1=min(minprice1,close)
    if a1 then
    if tradeprice(1)-minprice1>=(trailingstop1) then
    priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
    endif
    endif
    endif
    
    //2nd leg long
    if longonmarket then
    maxprice2=max(maxprice2,high)
    if a2 then
    if maxprice2-tradeprice(1)>=(trailingstop2) then
    priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
    endif
    endif
    endif
    //2nd leg short
    if shortonmarket then
    minprice2=min(minprice2,low)
    if a2 then
    if tradeprice(1)-minprice2>=(trailingstop2) then
    priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
    endif
    endif
    endif
    
    //3rd leg long
    if longonmarket then
    maxprice3=max(maxprice3,high)
    if a3 then
    if maxprice3-tradeprice(1)>=(trailingstop3) then
    priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
    endif
    endif
    endif
    //3rd leg short
    if shortonmarket then
    minprice3=min(minprice3,low)
    if a3 then
    if tradeprice(1)-minprice3>=(trailingstop3) then
    priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
    endif
    endif
    endif
    
    //first leg exit
    if longonmarket and priceexit1>0then
    sell at priceexit1 stop
    endif
    if shortonmarket and priceexit1>0then
    exitshort at priceexit1 stop
    endif
    
    //2nd leg exit
    if longonmarket and priceexit2>0then
    sell at priceexit2 stop
    endif
    if shortonmarket and priceexit2>0 then
    exitshort at priceexit2 stop
    endif
    
    //3rd leg exit
    if longonmarket and priceexit3>0then
    sell at priceexit3 stop
    endif
    if shortonmarket and priceexit3>0 then
    exitshort at priceexit3 stop
    endif
    
    if displayts then
    graphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"
    graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"
    graphonprice priceexit3 coloured(0,0,255,255) as "trailingstop3"
    endif
    endif

     

    Any idea’s?

    wait… could the cause be there’s no space between > 0 and then?

    priceexit1>0then

    that the backtest still works correctly, but realtime doesn’t?

    Screenshot-2019-05-16-at-12.14.03.jpg Screenshot-2019-05-16-at-12.14.03.jpg Screenshot-2019-05-16-at-12.30.11.jpg Screenshot-2019-05-16-at-12.30.11.jpg Screenshot-2019-05-16-at-12.37.54.jpg Screenshot-2019-05-16-at-12.37.54.jpg
    #98608 quote
    GraHal
    Participant
    Master

    priceexit1>0then

    Whenever I make that error (??then with no space) PRT flags up a syntax error message when I try to run backtest.

    Also an ! box shows in the left hand margin next to the line with the error?

    EDIT / PS

    Ha … strangely I don’t get a syntax error or the ! box … I just tried your code above on my platform.

    #98614 quote
    GraHal
    Participant
    Master

    Deleted, sorry

    #98616 quote
    Paul
    Participant
    Master

    find & replace …same here! That’s strange indeed! and no error in backtest or setting it up live.

     

    Most likely that’s the cause and at the moment i’am just a bit annoyed with myself for letting that happen! An error notice would’ve been nice in the backtest.

    Strangly I can look so many times to a code and then only to notice it when posting here 🙂

    #98618 quote
    GraHal
    Participant
    Master

    Strangly I can look so many times to a code and then only to notice it when posting here

    Yeah same here … for me it is partly because the code appears with much more clarity / contrast on the PRC webpage and I always feel like I have catteracts / cloudy vision on the PRT coding window.

    All these failings will be sorted in version 11 ! ??

    #98619 quote
    Paul
    Participant
    Master

    haha spot on & well said! I hope so in V11.

    #98692 quote
    Nicolas
    Keymaster
    Master

    Coding issues topic should not be placed in the ProRealTime support forum, but in each independent forum depending of the subject: ProBuilder, ProBacktest or ProScreener. Thanks in advance 😉

    #98986 quote
    Paul
    Participant
    Master

    fixed the issue above, but again realtime is not consistent with the backtest for the trailing stop!

    2 positions, different strategies about same time/level entry.

    One strategy get’s out at the wrong level, but both have same trailing-stop.

     

    In the backtest I have the same wrong result as realtime when I set settings to show only 25 units (everything lower than 10000 units ).

    Only from 10000 bars or higher it shows the trailing-stop as intended.

    Both strategy’s have

    defparam preloadbars = 10000

     

    Could it be that I’ve to increase that level?

    Screenshot-2019-05-21-at-12.26.51.jpg Screenshot-2019-05-21-at-12.26.51.jpg
    #98988 quote
    Paul
    Participant
    Master

    pic compare; show units in backtest less than 10000 and bigger.

    Screenshot-2019-05-21-at-12.26.51-1.jpg Screenshot-2019-05-21-at-12.26.51-1.jpg Screenshot-2019-05-21-at-12.27.51.jpg Screenshot-2019-05-21-at-12.27.51.jpg
    #98999 quote
    Nicolas
    Keymaster
    Master

    Position prices in backtests are always set at the exact price you are calculating the entries and exit, while in real time that prices could differ due to spread and slippage (with many different cause of slippage).

    #99005 quote
    Paul
    Participant
    Master

    yeah though the market continued to go up after 0.35% gain but it sold at 0.35% give or take (because of slippage/positionprice/spread).

    I reworked the trailing stop a bit (same results in backtest), set the preloadbars to 50000 and post back after live results.

     

    // trailing stop
    once enablets  = 1 // trailing stop
    once displayts = 1 // trailing stop
    
    ts1=0.35
    ts2=0.30
    ts3=0.20
    
    switch =ts2+ts3
    switch2=ts1+ts2
    
    underlaying=100
    
    // underlaying security / index / forex
    // profittargets and stoploss have to match the lines
    // not to be optimized
    // 0.01 forex [i.e. gbpusd=0.01]
    // 1.00 securities [i.e. aapl=1 ;
    // 100.00 indexes [i.e. dax=100]
    // 100=xauusd
    // 100=cl us crude
    
    
    if enablets then
    
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    trailingstop1 = (tradeprice(1)/100)*ts1
    trailingstop2 = (tradeprice(1)/100)*ts2
    trailingstop3 = (tradeprice(1)/100)*ts3
    endif
    
    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    maxprice1=0
    minprice1=close
    priceexit1=0
    
    maxprice2=0
    minprice2=close
    priceexit2=0
    
    maxprice3=0
    minprice3=close
    priceexit3=0
    
    a1=0
    a2=0
    a3=0
    
    pp=0
    endif
    
    if onmarket then
    pp=(positionperf*100)
    if pp>=ts1 then
    a1=1
    endif
    if pp>=switch then
    a2=1
    endif
    if pp>=switch2 then
    a3=1
    endif
    endif
    
    // setup long
    if longonmarket then
    maxprice1=max(maxprice1,close)
    maxprice2=max(maxprice2,high)
    maxprice3=max(maxprice3,high)
    if a1 then
    if maxprice1-tradeprice(1)>=(trailingstop1) then
    priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
    endif
    endif
    if a2 then
    if maxprice2-tradeprice(1)>=(trailingstop2) then
    priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
    endif
    endif
    if a3 then
    if maxprice3-tradeprice(1)>=(trailingstop3) then
    priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
    endif
    endif
    endif
    
    // setup short
    if shortonmarket then
    minprice1=min(minprice1,close)
    minprice2=min(minprice2,low)
    minprice3=min(minprice3,low)
    if a1 then
    if tradeprice(1)-minprice1>=(trailingstop1) then
    priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
    endif
    endif
    if a2 then
    if tradeprice(1)-minprice2>=(trailingstop2) then
    priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
    endif
    endif
    if a3 then
    if tradeprice(1)-minprice3>=(trailingstop3) then
    priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
    endif
    endif
    endif
    
    // exit long
    if longonmarket then
    if priceexit1>0 then
    sell at priceexit1 stop
    endif
    if priceexit2>0 then
    sell at priceexit2 stop
    endif
    if priceexit3>0 then
    sell at priceexit3 stop
    endif
    endif
    
    // exit short
    if shortonmarket then
    if priceexit1>0 then
    exitshort at priceexit1 stop
    endif
    if priceexit2>0 then
    exitshort at priceexit2 stop
    endif
    if priceexit3>0 then
    exitshort at priceexit3 stop
    endif
    endif
    
    if displayts then
    graphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"
    graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"
    graphonprice priceexit3 coloured(0,0,255,255) as "trailingstop3"
    endif
    endif
    #99159 quote
    Paul
    Participant
    Master

    Happend again and I don’t know how to fix. Backtest does it correct, live-trading does not.

    Running two separate strategies version v1 and v2 which are identical besides exit-times on Friday and trailing-stop.

    adjusted preloadbars to 50000.

    v1 uses trailing-top 0.35% then 0.25% then 0.20% (switch to lower trailing-stop when gain > 0.45% and again when gain > 0.55%)

    v2 uses trailing-top 0.40% then 0.30%  then 0.20% (switch to lower trailing-stop when gain > 0.55% and again when gain > 0.60%)

     

    I adjusted the entry-price in the backtest to match the price in live trading so the real trade performance is correct.

    v1 exits when position-performance > 0.35% (time 11.26) and is sold at the close of next bar at time 11.27

    v2 exits when position-performance > 0.40% (time 11.26) and is sold at the close of next bar at time 11.27 (this version had positive slippage that’s why it reached 0.40% at the same time as v1)

     

    So one thing for sure. It’s directly related to the trailing stop.

    Is this a coding issue or is IG handeling of the stops incorrect? Because I can’t see the reason why it exits immediately when the first performance gains criteria are reached!

    v1-trailingstop-0.35.jpg v1-trailingstop-0.35.jpg v2-trailingstop-0.40.jpg v2-trailingstop-0.40.jpg
    #99352 quote
    Paul
    Participant
    Master

    Seems to be solved after making small adjustments.

    I guess that when only the first trade in any backtest is wrong, then live trading is not reliable.

    The problem was not with IG handeling of the stops, but the code.

    #99616 quote
    Paul
    Participant
    Master

    Here’s the correction I made.

    if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
    trailingstop1 = (tradeprice(1)/100)*ts1
    trailingstop2 = (tradeprice(1)/100)*ts2
    trailingstop3 = (tradeprice(1)/100)*ts3
    endif

    is replaced by (or changed back to the original)

    trailingstop1 = (tradeprice(1)/100)*ts1
    trailingstop2 = (tradeprice(1)/100)*ts2
    trailingstop3 = (tradeprice(1)/100)*ts3
    

    The difference in the backtest is the handling of the first trade but only when that trade uses a trailing-stop!

    The error became visible in live trading,  the trade didn’t activate the proper trailing-stop but exited on the level the trailing-stop should start.

    Besides this, it was missing some pointsize reference as below

    if maxprice1-tradeprice(1)>=(trailingstop1)*pointsize then
    priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
    endif
    endif

    Hopefully it’s all good from now.

    #99618 quote
    stefou102
    Participant
    Veteran

    Thx Paul for the update. I already tried to test your code with some of my strategies but each time I tried, the backtest load time is infinite. Don’t you have the same issue?

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

Realtime trailing-stop not consistent to backtest


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Paul @micky75d Participant
Summary

This topic contains 17 replies,
has 4 voices, and was last updated by stefou102
6 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/16/2019
Status: Active
Attachments: 8 files
Logo Logo
Loading...