Fibonacci Trading Backtest

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #204531 quote
    Trimicha
    Participant
    New

    Dear Team,

    can someone please write me a trading program, so I can backtest that?

    it’s related, somehow, to the zigzag indicator. The scenario is as follows:

    a swing is retraced min 50%, max 85% and at least with 2 bars..

    entry is  after a close below 38.2% retracement of that same fib ret.

    stop-loss is after a close above 50% or 3 bars sideways action. profit taking is at 127.2 and 138.2% projection of that first swing and its retracement.

     

    I have no idea how I could do it and if the procedure asking for these things is completely different, excuse me please. any help is greatly appreciated.

     

    Thank you,

    Michael

    fib_strategy.jpg fib_strategy.jpg
    #204536 quote
    robertogozzi
    Moderator
    Master
    • Only post in the language of the forumthat you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
    • Do not double post. Ask your question only once and only in one forum. All double posts will be deleted anyway so posting the same question multiple times will just be wasting your own time and will not get you an answer any quicker. Double posting just creates confusion in the forums.

    I deleted your post wiritten in English in the German forum.

    Thank you 🙂

    #204537 quote
    Trimicha
    Participant
    New

    Yeah, thank you. I am quite new here. You are fast. Is the request in general okay?

    #204691 quote
    robertogozzi
    Moderator
    Master

    There you go (I tested it on DAX, Daily TF):

    // my FIBO system
    //
    // https://www.prorealcode.com/topic/fibonacci-trading-backtest/
    //
    DEFPARAM CumulateOrders = False
    DEFPARAM PreLoadBars    = 0
    ONCE p      = 20
    ONCE UPflag = 0
    ONCE DNflag = 0
    IF Not onMarket THEN
       HH          = highest[p](high)
       LL          = lowest[p](low)
       Diff        = HH - LL
       IF Diff <> Diff[1] THEN
          UPflag = 0
          DNflag = 0
       ENDIF
       Fib0236 = Diff * 0.236
       Fib0382 = Diff * 0.382
       Fib0500 = Diff * 0.500
       Fib0618 = Diff * 0.618
       Fib0764 = Diff * 0.764
       Fib0850 = Diff * 0.850
       Fib1272 = Diff * 1.272
       Fib1382 = Diff * 1.382
       UP      = 0
       DN      = 0
       FOR i = 0 TO (p - 1)
          IF high[i] = HH THEN
             UP = 1
             LastBar = BarIndex[i]
             BREAK
          ENDIF
          IF low[i] = LL THEN
             DN = 1
             LastBar = BarIndex[i]
             BREAK
          ENDIF
       NEXT
       BarsAfterFIBO = max(1,BarIndex - LastBar)
       IF UP THEN
          Fib0230 = HH - Fib0230
          Fib0382 = HH - Fib0382
          Fib0500 = HH - Fib0500
          Fib0618 = HH - Fib0618
          Fib0764 = HH - Fib0764
          Fib0850 = HH - Fib0850
          Fib1272 = LL + Fib1272
          Fib1382 = LL + Fib1382
          LastHH  = lowest[BarsAfterFIBO](min(open,close))
          IF LastHH < Fib0850 THEN
             UPflag = -1
          ENDIF
       ELSIF DN THEN
          Fib0236 = LL + Fib0236
          Fib0236 = LL + Fib0236
          Fib0500 = LL + Fib0500
          Fib0618 = LL + Fib0618
          Fib0764 = LL + Fib0764
          Fib0850 = LL + Fib0850
          Fib1272 = HH - Fib1272
          Fib1382 = HH - Fib1382
          LastLL  = highest[BarsAfterFIBO](max(open,close))
          IF LastHH > Fib0850 THEN
             DNflag = -1
          ENDIF
       ENDIF
       IF UP AND (UPflag = 0) THEN
          IF (close < Fib0500) AND (BarsAfterFIBO >= 2) THEN
             UPflag = 1
          ENDIF
       ELSIF DN AND (DNflag = 0) THEN
          IF (close > Fib0500) AND (BarsAfterFIBO >= 2) THEN
             DNflag = 1
          ENDIF
       ENDIF
    ELSE
       UPflag = 0
       DNflag = 0
       UP     = 0
       DN     = 0
    ENDIF
    IF UP AND (UPflag = 1) AND (close >= Fib0382) AND Not OnMarket THEN
       BUY 2 Contract at Market
       SELL AT Fib0500 STOP         //SL
       SELL AT Fib1382 LIMIT        //TP
    ENDIF
    IF DN AND (DNflag = 1) AND (close <= Fib0382) AND Not OnMarket THEN
       SELLSHORT 2 Contract at Market
       EXITSHORT AT Fib0500 STOP    //SL
       EXITSHORT AT Fib1382 LIMIT   //TP
    ENDIF
    IF LongOnMarket THEN
       SELL AT Fib0500 STOP         //SL
       SELL AT Fib1382 LIMIT        //TP
       IF close >= Fib1272 THEN
          SELL 1 Contract at Market       //exit 50%
       ENDIF
    ELSIF ShortOnMarket THEN
       EXITSHORT AT Fib0500 STOP    //SL
       EXITSHORT AT Fib1382 LIMIT   //TP
       IF close <= Fib1272 THEN
          EXITSHORT 1 Contract at Market //exit 50%
       ENDIF
    ENDIF
    graphonprice Fib1272 coloured("Red")  AS "TP 1"
    graphonprice Fib1382 coloured("Blue") AS "TP 2"
    //graph UP
    //graph DN
    //graph Diff <> Diff[1]
    //graph UPflag
    //graph DNflag
    //graph LastBar
    //graph BarsAfterFIBO

    I only have to add the SIDWEWAYS exit. Can you explain what you mean exactly by sideways?

    my-FIBO-system.itf
    #204693 quote
    robertogozzi
    Moderator
    Master

    I used this custom indicator to plot and debug FIBO levels:

    DEFPARAM CalculateOnLastBars = 3000
    DEFPARAM DrawOnLastBarOnly   = True
    //ONCE p  = 100
    ONCE p     = max(1,min(9999,p))       //range 1 - 9999
    IF BarIndex <= EndBar THEN
       HH      = highest[p](high)
       LL      = lowest[p](low)
       Diff    = HH - LL
       Fib0236 = Diff * 0.236
       Fib0382 = Diff * 0.382
       Fib0500 = Diff * 0.500
       Fib0618 = Diff * 0.618
       Fib0764 = Diff * 0.764
       Up     = 0
       Dn     = 0
       FOR i = 0 TO (p - 1)
          IF high[i] = HH THEN
             Up = 1
             BREAK
          ENDIF
          IF low[i] = LL THEN
             Dn = 1
             BREAK
          ENDIF
       NEXT
    ENDIF
    DRAWHLINE(HH) coloured(238,122,233,255)//(0,128,0,255)
    DRAWHLINE(LL) coloured(238,122,233,255)//(0,128,0,255)
    LastBar = min(EndBar,BarIndex)
    IF Up THEN
       DRAWTEXT("---------------------------(23.6%)",LastBar,HH - Fib0236,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(38.2%)",LastBar,HH - Fib0382,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(50.0%)",LastBar,HH - Fib0500,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(61.8%)",LastBar,HH - Fib0618,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(76.4%)",LastBar,HH - Fib0764,SansSerif,Bold,10)coloured(238,122,233,255)
    ELSIF Dn THEN
       DRAWTEXT("---------------------------(23.6%)",LastBar,LL + Fib0236,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(38.2%)",LastBar,LL + Fib0382,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(50.0%)",LastBar,LL + Fib0500,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(61.8%)",LastBar,LL + Fib0618,SansSerif,Bold,10)coloured(238,122,233,255)
       DRAWTEXT("---------------------------(76.4%)",LastBar,LL + Fib0764,SansSerif,Bold,10)coloured(238,122,233,255)
    ENDIF
    RETURN

    variable EndBar is used to debug code, so that you can ban the indicator from advancing after any given BARINDEX bar.

    Trimicha thanked this post
    Fibonacci.itf
    #204698 quote
    robertogozzi
    Moderator
    Master

    Here is an updated version with some fixes:

    // my FIBO system
    //
    // https://www.prorealcode.com/topic/fibonacci-trading-backtest/
    //
    DEFPARAM CumulateOrders = False
    DEFPARAM PreLoadBars    = 0
    ONCE p      = 20
    ONCE UPflag = 0
    ONCE DNflag = 0
    IF Not onMarket THEN
    HH          = highest[p](high)
    LL          = lowest[p](low)
    Diff        = HH - LL
    IF Diff <> Diff[1] THEN
    UPflag = 0
    DNflag = 0
    ENDIF
    Fib0236 = Diff * 0.236
    Fib0382 = Diff * 0.382
    Fib0500 = Diff * 0.500
    Fib0618 = Diff * 0.618
    Fib0764 = Diff * 0.764
    Fib0850 = Diff * 0.850
    Fib1272 = Diff * 1.272
    Fib1382 = Diff * 1.382
    UP      = 0
    DN      = 0
    FOR i = 0 TO (p - 1)
    IF high[i] = HH THEN
    UP = 1
    LastBar = BarIndex[i]
    BREAK
    ENDIF
    IF low[i] = LL THEN
    DN = 1
    LastBar = BarIndex[i]
    BREAK
    ENDIF
    NEXT
    BarsAfterFIBO = max(1,BarIndex - LastBar)
    IF UP THEN
    Fib0236 = HH - Fib0236
    Fib0382 = HH - Fib0382
    Fib0500 = HH - Fib0500
    Fib0618 = HH - Fib0618
    Fib0764 = HH - Fib0764
    Fib0850 = HH - Fib0850
    Fib1272 = LL + Fib1272
    Fib1382 = LL + Fib1382
    LastHH  = lowest[BarsAfterFIBO](min(open,close))
    IF LastHH < Fib0850 THEN
    UPflag = -1
    ENDIF
    ELSIF DN THEN
    Fib0236 = LL + Fib0236
    Fib0382 = LL + Fib0382
    Fib0500 = LL + Fib0500
    Fib0618 = LL + Fib0618
    Fib0764 = LL + Fib0764
    Fib0850 = LL + Fib0850
    Fib1272 = HH - Fib1272
    Fib1382 = HH - Fib1382
    LastLL  = highest[BarsAfterFIBO](max(open,close))
    IF LastHH > Fib0850 THEN
    DNflag = -1
    ENDIF
    ENDIF
    IF UP AND (UPflag = 0) THEN
    IF (close < Fib0500) AND (BarsAfterFIBO >= 2) THEN
    UPflag = 1
    ENDIF
    ELSIF DN AND (DNflag = 0) THEN
    IF (close > Fib0500) AND (BarsAfterFIBO >= 2) THEN
    DNflag = 1
    ENDIF
    ENDIF
    ELSE
    UPflag = 0
    DNflag = 0
    UP     = 0
    DN     = 0
    ENDIF
    IF UP AND (UPflag = 1) AND (close >= Fib0382) AND Not OnMarket THEN
    BUY 2 Contract at Market
    SELL AT Fib0500 STOP         //SL
    SELL AT Fib1382 LIMIT        //TP
    ENDIF
    IF DN AND (DNflag = 1) AND (close <= Fib0382) AND Not OnMarket THEN
    SELLSHORT 2 Contract at Market
    EXITSHORT AT Fib0500 STOP    //SL
    EXITSHORT AT Fib1382 LIMIT   //TP
    ENDIF
    IF LongOnMarket THEN
    SELL AT Fib0500 STOP         //SL
    SELL AT Fib1382 LIMIT        //TP
    IF close >= Fib1272 THEN
    SELL 1 Contract at Market       //exit 50%
    ENDIF
    ELSIF ShortOnMarket THEN
    EXITSHORT AT Fib0500 STOP    //SL
    EXITSHORT AT Fib1382 LIMIT   //TP
    IF close <= Fib1272 THEN
    EXITSHORT 1 Contract at Market //exit 50%
    ENDIF
    ENDIF
    graphonprice Fib1272 coloured("Red")  AS "TP 1"
    graphonprice Fib1382 coloured("Blue") AS "TP 2"
    //graph UP
    //graph DN
    //graph Diff <> Diff[1]
    //graph UPflag
    //graph DNflag
    //graph LastBar
    //graph BarsAfterFIBO
    Trimicha thanked this post
    my-FIBO-system-1.itf
    #204705 quote
    Trimicha
    Participant
    New

    Wow, thanks a lot!!! how have the results been on DAX?

    I did it by hand on SPX from 2020 to now today. that looked quite promising. but I used the “human” approach (especially, when it took to long to reach levels)

    I will test your latest version now. many, many thanks!

    #204706 quote
    Trimicha
    Participant
    New

    Ciao Roberto,

    I loaded the indicator and the backtest. but…it’s not doing what I want ;D

    I tested on various indices and stocks but it returns only a handful trades from early 2020. That doesn’t match with my “hand analysis”…

    My humble assumption is that there are not enough highs/lows recognized.

    I used this modified indicator from user AVT “ZigZag2” attached to show highs and lows and with a cp=1, it’s not perfect but provides a good starting point with a lot more retracements to check for signals. but… I am certainly not able to translate this into a system.

    Can I ask you to give it a [third] try? 🙂

     

    Mille Grazie,

    Michael

    ZigZag2.itf
    #204737 quote
    robertogozzi
    Moderator
    Master

    Both the built-in ZigZag and the ZigZag2 you posted repaint, as you can see from the attached pic (it means that the signal is actually returned later, but the code plots it on a previous candle).

    Try adding the attached version below your chart and set it to histogram.

    In any case, I can make a trading system out of it. Probably having to wait for a ratracement, some delay in swing signals is not so important.

    So, basically, all you need is what you have written in your first post, where the UPper and LOWer swing points are those plotted by ZigZag2, right?

    Which and instrument TF do you plan to use most often?

    ZigZag2-1.itf x-10.jpg x-10.jpg
    #204770 quote
    Trimicha
    Participant
    New

    I use SP500/ES and that system should work on daily candles. But you could also use TSLA or QQQ for example, as I assume, that more volatile underlyings take the most out of that strategy. That said, I don’t think the underlying matters, as my main point is the recognition of the retracement (more than 50%) and the outbreak above 0.236 retracement (or below for shorts, I’ve written 0.382 before, I think, but that’s easily adjustable).

    And yes, the ZigZag2 is not perfect in identifiying each LOW and TOP but reasonably good to make for a good Backtest indication.

    I am aware that the top/lows are only recognised later, but in most cases, the 0.236 line is not crossed directly after the low is in place. so that’s the reason, why the ZigZag indicator is mentioned in the handbook as not usable for an automated system 😀

    Many thanks for your effort! I am really looking forward to it.

    Michael

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

Fibonacci Trading Backtest


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Trimicha @trimicha Participant
Summary

This topic contains 9 replies,
has 2 voices, and was last updated by Trimicha
3 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/20/2022
Status: Active
Attachments: 7 files
Logo Logo
Loading...