For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

ParabolicSar - Issue or Misunderstanding?

  • 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,, afmax=self.p.afmax)
        def next(self):
    # Create an instance of cerebro
    cerebro = bt.Cerebro()
    # Add our strategy
    data = bt.feeds.Quandl(
        fromdate = datetime(2016,1,1),
        todate = datetime(2017,1,1),
        buffered= True
    # Add the data to Cerebro
    # Run over everything
    # Finally plot the end results


  • administrators

    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):

    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.

  • administrators

    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


    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.


    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)

  • administrators

    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)


    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.


    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):

  • 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.

Log in to reply