twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.24k stars 1.02k forks source link

some of psar results are not same as Tradingview #539

Open Ben0ni opened 2 years ago

Ben0ni commented 2 years ago

Which version are you running? The lastest version is on Github. Pip is for major releases. My pandas_ta version is the latest: 0.3.14b0.

Is your feature request related to a problem? Please describe.

df.ta.psar(af0=0.5, af=0.2, max_af=0.4, append=False)

the result is: image there are some differences with tradingview: image I ran the data for about six months, there will be some different results sometimes.

You can check the red numbers, it is different from Tradingview. I used Bitcoin futures data from Binance API and BTCUSDTPREP symbol from Binance in Tradingview. The two data are totally the same.

Describe the solution you'd like Could you tell me why and how to fix it?

Thank you very much.

twopirllc commented 2 years ago

Hello @Ben0ni,

Please update to the development version and let me know if you have the same issue.

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development

There was an initial index increment requested in here for the for loop.

It is sometimes difficult to match TV. 😐 But if you can figure it out, would appreciate a PR. 😎

Kind Regards, KJ

Ben0ni commented 2 years ago

@twopirllc image

I did the update. However, the result is totally the same as before. image

twopirllc commented 2 years ago

@Ben0ni

Damn!

Like I said, it is sometimes difficult to match TV. 😐 For more info, see https://github.com/twopirllc/pandas-ta/pull/311 If you can figure it out, we would appreciate a PR. 😎

Kind Regards, KJ

AbyssAlora commented 2 years ago

Hi guys,

I think that it is really hard to maintain issues like this. I've made some tests. Sorry, I wrote this code in few min and did not see python for a long time.

import pandas as pd
import pandas_ta as ta
import numpy as np

if __name__ == "__main__":
    data = pd.read_csv("BINANCE_BTCUSDTPERP, D.csv")

    data.ta.psar(append=True)
    data.fillna(0, inplace=True)

    print(
        np.corrcoef(data['ParabolicSAR'].values, 
        data['PSARl_0.02_0.2'].values + data['PSARs_0.02_0.2'].values)
    )

    print(
        np.corrcoef(data['ParabolicSAR'].values[10:], 
        data['PSARl_0.02_0.2'].values[10:] + data['PSARs_0.02_0.2'].values[10:])
    )

Output:

[[1.         0.99986257]
 [0.99986257 1.        ]]
[[1. 1.]
 [1. 1.]]

Output (BINANCE_BTCUSDTPERP, 30.csv):

[[1.         0.99981688]
 [0.99981688 1.        ]]
[[1.         0.99999999]
 [0.99999999 1.        ]]

The one and only difference between TV and Pandas TA is that TV calculates starting SAR in much simple way. I think that TV starts with Long PSAR and previous low. Pandas TA uses DM. This is not an issue in my opinion, because Parabolic SAR corrects itself after few iterations (you can see second correlation).

Best, AbyssAlora

AbyssAlora commented 2 years ago

Hi guys, I am really sorry for my spam, but as I dig into this issue a little bit more, I realized that @Ben0ni is right. I found that SAR after reverse is sometimes different. Here is the original version of PSAR from Pandas TA:

          high       low  ParabolicSAR  PSARl_0.5_0.4  PSARs_0.5_0.4
          ...
41     8108.54   7852.12     7812.0000      7812.0000         0.0000
42     8300.50   7871.17     8300.5000         0.0000      8300.5000
43     8334.57   8150.00     7852.1200      7871.1700         0.0000
44     8290.00   8000.00     8334.5700         0.0000      8334.5700
45     8047.78   7172.76     8334.5700         0.0000      8334.5700
46     7495.81   7337.44     8290.0000         0.0000      8290.0000
47     8800.00   7359.00     7172.7600      7172.7600         0.0000
48    10408.48   8626.89     7337.4400      7337.4400         0.0000
49     9803.83   9066.07     7359.0000      7359.0000         0.0000
50     9930.13   9156.03     8578.7920      8578.7920         0.0000
51     9569.10   9070.00    10408.4800         0.0000     10408.4800
52     9418.65   8975.72     9930.1300         0.0000      9930.1300
          ...

ParabolicSAR -> TV version (exported)

The output for correlation (warmup period is ignored):

[[1.         0.99914584]
 [0.99914584 1.        ]]

You can see the row 43 where Pandas TA is 7871.1700 and TV is 7852.1200 (looks like TV PSAR is not looking just to previous H/L after reverse). It looks like reversal is calculated in the right way. I've made a new version for experiments trying to fit TV:

def psar(...) -> DataFrame:
   ...
        if reverse:
            # _sar = ep
            if falling:
                _sar = min(low.iloc[row - 1], low.iloc[row - 2], ep)
            else:
                _sar = max(high.iloc[row - 1], high.iloc[row - 2], ep)
   ...

Result:

          high       low  ParabolicSAR  PSARl_0.5_0.4  PSARs_0.5_0.4
          ...
41     8108.54   7852.12     7812.0000      7812.0000         0.0000
42     8300.50   7871.17     8300.5000         0.0000      8300.5000
43     8334.57   8150.00     7852.1200      7852.1200         0.0000
44     8290.00   8000.00     8334.5700         0.0000      8334.5700
45     8047.78   7172.76     8334.5700         0.0000      8334.5700
46     7495.81   7337.44     8290.0000         0.0000      8290.0000
47     8800.00   7359.00     7172.7600      7172.7600         0.0000
48    10408.48   8626.89     7337.4400      7337.4400         0.0000
49     9803.83   9066.07     7359.0000      7359.0000         0.0000
50     9930.13   9156.03     8578.7920      8578.7920         0.0000
51     9569.10   9070.00    10408.4800         0.0000     10408.4800
          ...

with correlation (warmup period is ignored):

[[1. 1.]
 [1. 1.]]

I am not sure if it is the only thing to change, but I hope it helps.

Kind regards, AbyssAlora

Ben0ni commented 2 years ago

@AbyssAlora Thanks, it works. Now the result is all the same.

kaanguven commented 2 years ago

Do we look at " PSARr_0.02_0.2" for uptrend and downtrend?

twopirllc commented 2 years ago

Hello @kaanguven,

So "PSARr_0.02_0.2" is an extra column providing the "reversal", when it switches from long to short and back.

The uptrend is the long column or "PSARl_0.02_0.2" and the downtrend is the short column or "PSARs_0.02_0.2".

If you want one PSAR line, then you need to combine the up and down trend lines into one signal.

When in doubt, plot the results and see what is useful. Also check help(ta.psar).

Kind Regards KJ

einarjohnson commented 6 months ago

Hello @Ben0ni,

Please update to the development version and let me know if you have the same issue.

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development

There was an initial index increment requested in here for the for loop.

It is sometimes difficult to match TV. 😐 But if you can figure it out, would appreciate a PR. 😎

Kind Regards, KJ

hi there. are there any plans to merge these changes into main and into a new release?

twopirllc commented 6 months ago

Hello @einarjohnson

hi there. are there any plans to merge these changes into main and into a new release?

Yes and you can become more familiar with the future main but using the development branch. It has numerous improvements across the board. There are still some outstanding PRs, Issues, and features to include before making a new release.

Kind Regards, KJ

einarjohnson commented 6 months ago

@twopirllc , thank you for such a quick response.