Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parabolic SAR in Python....PSAR keep growing instead of reversing

I have a pandas dataframe of Open/high/low/close stock prices and I am writing to write a function that will add Parabolic SAR to my dataframe. Right now the PSAR number just grows insanely huge and i never seem to get much in terms of flipping between bull and bear directions. Any help in understanding why my PSAR grows so crazy would be great. I've tried several variations on this code to no avail.

For those who are not familiar with PSAR:

  • Prior SAR: The SAR value for the previous period.

Rising SAR

  • Extreme Point (EP): The highest high of the current uptrend.
  • Acceleration Factor (AF): Starting at .02, AF increases by .02 each time the extreme point makes a new high. AF can reach a maximum of .20, no matter how long the uptrend extends.
  • The Acceleration Factor is multiplied by the difference between the Extreme Point and the prior period's SAR. This is then added to the prior period's SAR. Note however that SAR can never be above the prior two periods' lows. Should SAR be above one of those lows, use the lowest of the two for SAR.

    Current SAR = Prior SAR + Prior AF(Prior EP - Prior SAR)

Eg: 13-Apr-10: SAR = 48.28 = 48.13 + .14(49.20 - 48.13)

Falling SAR

  • Extreme Point (EP): The lowest low of the current downtrend.
  • Acceleration Factor (AF): Starting at .02, AF increases by .02 each time the extreme point makes a new low. AF can reach a maximum of .20, no matter how long the downtrend extends.
  • The Acceleration Factor is multiplied by the difference between the Prior period's SAR and the Extreme Point. This is then subtracted from the prior period's SAR. Note however that SAR can never be below the prior two periods' highs. Should SAR be below one of those highs, use the highest of the two for SAR.

    Current SAR = Prior SAR - Prior AF(Prior EP - Prior SAR)

Eg: 9-Feb-10: SAR = 43.56 = 43.84 - .16(43.84 - 42.07)

During Reversal, the PSAR becomes the prior extreme point EP, and the new EP is the previous high or low depending on the direction of the flip. The AF resets to 0.02.

My function:

def addSAR(df):
    df.loc[0, 'AF'] =0.02
    df.loc[0, 'PSAR'] = df.loc[0, 'low']
    df.loc[0, 'EP'] = df.loc[0, 'high']
    df.loc[0, 'PSARdir'] = "bull"

    for a in range(1, len(df)):

        if df.loc[a-1, 'PSARdir'] == 'bull':

            df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))            

            df.loc[a, 'PSARdir'] = "bull"

            if df.loc[a, 'low'] < df.loc[a-1, 'PSAR']:
                df.loc[a, 'PSARdir'] = "bear"
                df.loc[a, 'PSAR'] = df.loc[a-1, 'EP']
                df.loc[a, 'EP'] = df.loc[a-1, 'low']
                df.loc[a, 'AF'] = .02

            else:
                if df.loc[a, 'high'] > df.loc[a-1, 'EP']:
                    df.loc[a, 'EP'] = df.loc[a, 'high']
                    if df.loc[a-1, 'AF'] <= 0.18:
                        df.loc[a, 'AF'] =df.loc[a-1, 'AF'] + 0.02
                    else:
                        df.loc[a, 'AF'] = df.loc[a-1, 'AF']
                elif df.loc[a, 'high'] <= df.loc[a-1, 'EP']:
                    df.loc[a, 'AF'] = df.loc[a-1, 'AF']
                    df.loc[a, 'EP'] = df.loc[a-1, 'EP']               



        elif df.loc[a-1, 'PSARdir'] == 'bear':

            df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] - (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR']))

            df.loc[a, 'PSARdir'] = "bear"

            if df.loc[a, 'high'] > df.loc[a-1, 'PSAR']:
                df.loc[a, 'PSARdir'] = "bull"
                df.loc[a, 'PSAR'] = df.loc[a-1, 'EP']
                df.loc[a, 'EP'] = df.loc[a-1, 'high']
                df.loc[a, 'AF'] = .02

            else:
                if df.loc[a, 'low'] < df.loc[a-1, 'EP']:
                    df.loc[a, 'EP'] = df.loc[a, 'low']
                    if df.loc[a-1, 'AF'] <= 0.18:
                        df.loc[a, 'AF'] = df.loc[a-1, 'AF'] + 0.02
                    else:
                        df.loc[a, 'AF'] = df.loc[a-1, 'AF']

                elif df.loc[a, 'low'] >= df.loc[a-1, 'EP']:
                    df.loc[a, 'AF'] = df.loc[a-1, 'AF']
                    df.loc[a, 'EP'] = df.loc[a-1, 'EP']           

    return df
like image 783
Greg D Avatar asked Feb 28 '19 04:02

Greg D


People also ask

What is the best setting for Parabolic SAR?

The SAR Indicator Settings The best settings are the standard Parabolic parameters. The initial factor is 0.02, the price step is also 0.02, and the maximum AF is 0.2. Most traders use these settings for the Parabolic.

How do you calculate parabolic stop and reverse?

Parabolic SAR Calculation The parabolic SAR is calculated as follows: Uptrend: PSAR = Prior PSAR + Prior AF (Prior EP - Prior PSAR) Downtrend: PSAR = Prior PSAR - Prior AF (Prior PSAR - Prior EP)

Is Parabolic SAR a good indicator?

The parabolic SAR is used to gauge a stock's direction and for placing stop-loss orders. The indicator tends to produce good results in a trending environment, but it produces many false signals and losing trades when the price starts moving sideways.

Which PSAR setting affects its sensitivity to price?

The accuracy and sensitivity of the PSAR depends on several variables, the most important of which are the acceleration factor, its step increment, and the maximum step. When used effectively, the PSAR can serve as both a reversal signal and a price indicator for placing trailing stop-losses.


1 Answers

figured it out Facepalm

df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'EP']-df.loc[a-1, 'PSAR'])) 

Should be df.loc[a, 'PSAR'] = df.loc[a-1, 'PSAR'] + (df.loc[a-1, 'AF']*(df.loc[a-1, 'PSAR']-df.loc[a-1, 'EP']))

last two variables transposed! what a pain...

now I can clean up the function and make it better..

hope this helps someone else from doing something dumb and getting stuck on it for 2 days

like image 71
Greg D Avatar answered Nov 09 '22 03:11

Greg D