Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    ParabolicSar - Issue or Misunderstanding?

    Indicators/Strategies/Analyzers
    2
    5
    1908
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      ThatBlokeDave last edited by

      Hello All,
      I am taking a look at Backtraders parabolic SAR indicator and am a little confused. So I would just like to know if my understanding/expectations are wrong or whether there is an issue with the indicator?

      Here is the crux of it: My understanding is the SAR value should only move from above the bar to below the bar when price breaks the PSAR level. (High >= PSAR etc)

      Sometimes I notice that the PSAR moves when price has not quite broken it yet. Here are some examples:

      In the first example, price did not break below the PSAR value

      alt text

      And another - This one is quite close to the PSAR value

      alt text

      After checking my debugs, the high appears to have missed by a whisker.

      ---------------------------- NEXT ----------------------------------
      1: Data Name:                            AAPL
      2: Bar Num:                              205
      3: Current date:                         2016-10-24 23:59:59.999989
      4: Open:                                 115.0565191216
      5: High:                                 115.68535065223
      6: Low:                                  114.95826419493
      7: Close:                                115.59692121824
      8: Volume:                               23538673.0
      9: PSAR:                                 116.33621966247183
      10: PSAR position                        Above bar
      11: Position Size:                       -10
      --------------------------------------------------------------------
      ---------------------------- NEXT ----------------------------------
      1: Data Name:                            AAPL
      2: Bar Num:                              206
      3: Current date:                         2016-10-25 23:59:59.999989
      4: Open:                                 115.89168599823
      5: High:                                 116.29453119754
      6: Low:                                  115.26285446759
      7: Close:                                116.18645077821
      8: Volume:                               48128970.0
      9: PSAR:                                 111.81410654174
      10: PSAR position                        Below bar
      11: Position Size:                       -10
      --------------------------------------------------------------------
      ---------------------------- NEXT ----------------------------------
      1: Data Name:                            AAPL
      2: Bar Num:                              207
      3: Current date:                         2016-10-26 23:59:59.999989
      4: Open:                                 112.31520666772
      5: High:                                 113.68095014832
      6: Low:                                  111.33265740109
      7: Close:                                113.57286972899
      8: Volume:                               66134219.0
      9: PSAR:                                 116.29453119754
      10: PSAR position                        Above bar
      11: Position Size:                       -10
      --------------------------------------------------------------------
      

      You can see the on the 24th, the PSAR value is at: 116.33621966247183
      Then on the 25th the high is at: 116.29453119754

      Although I know this is a bit of an apple to oranges comparison....(different data provider, different indicator code, different platform etc) but the PSAR on Tradingview never seems to change direction unless the PSAR value is taken out.

      Here is an image from the same period.
      alt text

      So have I just got the essence of how the PSAR should work wrong? Or is there possibly an issue?

      If there are questions on the code... The following code can reproduce what I am seeing.

      import backtrader as bt
      from datetime import datetime
      
      
      class PSARStrat(bt.Strategy):
      
          params = (
              ("period", 2),
              ("af", 0.02),
              ("afmax", 0.2)
              )
      
          def __init__(self):
              self.psar = bt.indicators.ParabolicSAR(period=self.p.period, af=self.p.af, afmax=self.p.afmax)
      
          def next(self):
              pass
      
      
      # Create an instance of cerebro
      cerebro = bt.Cerebro()
      
      # Add our strategy
      cerebro.addstrategy(PSARStrat)
      
      data = bt.feeds.Quandl(
          dataname='AAPL',
          fromdate = datetime(2016,1,1),
          todate = datetime(2017,1,1),
          buffered= True
          )
      
      # Add the data to Cerebro
      cerebro.adddata(data)
      
      # Run over everything
      cerebro.run()
      
      # Finally plot the end results
      cerebro.plot(style='candlestick')
      

      Thanks!

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by backtrader

        Let's focus on one the cases, the 1st chart, where you have the arrow:

        • In that bar, one can clearly see the SAR spot in between the open and the high, with the low right clearly below the PSAR spot. The price has broken below the SAR spot. Because the price being considered is the low and not the high.

          That means: the trend has changed (according to the definition made by Wilder and is now a downtrend. The SAR value for the next bar is calculated, which (as expected) is well above the high of the next bar to start the parabolic descent.

        Quoting from Wikipedia (because the book has no free online version):

        • https://en.wikipedia.org/wiki/Parabolic_SAR

        If the next period’s SAR value is inside (or beyond) the next period’s price range, a new trend direction is then signaled. The SAR must then switch sides.

        That's exactly the case, the next SAR spot is inside (not beyond in this case) the next period's price and the trend reverses for the next bar. The usage of the word next in the Wikipedia definition is not anecdotical, because the value of the SAR is calculated one bar in advance and not with the values of the current bar.

        As stated above and focusing on that 1st chart, What do you think is wrong with that concrete example?

        Note about the TradingView Chart

        If you look at day the 25th of October you can see the spot slightly inside the trading range (just slightly above the low) and the SAR trend is reversed for the next bar. The confusion here is probably created by the visual effect of the 1st days of Oct in the chart, for which the SAR spot is very close to the low, so close that it would seem they are touching, but that's not the case.

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          In any case it would seem the data you have for AAPL in Stockcharts doesn't match what's being used here and somewhere else.

          From StockCharts

          0_1518379288290_d3b6bbac-5e8f-4f71-9e40-a3357f1a8e22-image.png

          Notice how in the middle of October 2016 there is a huge movement downwards ... which is missing in your chart.

          AAPL rendered with backtrader with another data source. Notice how the downwards peak which can be seen in Stockcharts is there.

          0_1518379385014_ae9d6848-7357-4179-918b-09cf2a8ce7dc-image.png

          Which of course completely changes the picture when calculating the PSAR reversal (this of course doesn't discard that the calculations made for the PSAR in backtrader may -or may not- be wrong)

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            After much perusal ... a bug was found (but not where you suspected) in the capping of the acceleration. The price penetration logic (so far) has proven to be right.

            See this (which we'll take as basis)

            0_1518384584819_4dea780d-eba4-4ff2-9c68-0abca187f4ab-image.png

            backtrader before the correction can be in the previous post. Notice how the price in backtrader keeps on accelerating, whereas in the chart above the price has an initial acceleration and then stabilizes. This can be really noticed at the end of the chart where the Parabolic SAR of backtrader collides with the initial retracement of the price and makes 2 quick trend reversals.

            See now below with the capping of the acceleration corrected.

            0_1518384769232_c73cd55c-5272-4361-a603-d8adb7e6d342-image.png

            The collision is now no longer happening because the price stops accelerating after reaching the cap and remains away from the highs. And it matches the PSAR from the source above.

            Commit (development branch): https://github.com/backtrader/backtrader/commit/89a45fe0fe86252f98db56e29230baa62441b3c6

            1 Reply Last reply Reply Quote 1
            • T
              ThatBlokeDave last edited by

              Hi @backtrader

              Thanks for taking such a close look at it! Awesome...

              I will download the latest commit and take a look later this week.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }