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.39k stars 1.05k forks source link

VWAP that matched values of TradingView VWAP indicator (anchor) #381

Closed slhawk98 closed 3 years ago

slhawk98 commented 3 years ago

Running python: 3.8.5 pandas_ta version: 0.3.14b0

Description: Trying to get VWAP that matched values of TradingView indicator at this link. https://www.tradingview.com/support/solutions/43000502018-volume-weighted-average-price-vwap/

I think the key to matching this TradingView indicator is its "Anchor Setting", default is "session".

I want pandas_ta VWAP that matches TradingView indicator 30 minute chart. The "Timeseries Offset Aliases" documentation shows: T,min = minutely frequency I am not sure what combination of pandas_ta "anchor" and "offset" settting can be used to match TradingView VWAP on 30 minute chart. Also, I am not sure if pandas_ta can match this TradingView indicator.

Code I have tried:

df.set_index(pd.DatetimeIndex(df["date"]), inplace=True)
vwap = df.ta.vwap(anchor="T", offset=30, append=True)

I can provide screenshots if necessary.

twopirllc commented 3 years ago

Hello @slhawk98,

I want pandas_ta VWAP that matches TradingView indicator 30 minute chart.

I see

Also, I am not sure if pandas_ta can match this TradingView indicator.

Neither am I at this point.

The "Timeseries Offset Aliases" documentation shows: T,min = minutely frequency

That is correct. However it either does not allow other minute frequencies such as 5min, 30min, et al or I do not know where to find the example code to make it happen. It may take some digging.

Also, the offset in the following code does not make the anchor`` set to 30 mins. As it says inhelp(ta.vwap)``` and the rest of the indicators, it shifts the final calculation left or right by so many bars/rows.

vwap = df.ta.vwap(anchor="T", offset=30, append=True)

Assuming there is a way to anchor other than by minute "T", ideally the code should look like this to execute:

vwap = df.ta.vwap(anchor="30T", append=True) # for 30 mins

Lastly while Pandas TA does have many indicators that are correlated with TradingView, this is not a Python TradingView TA Library. It is at minimum an alternative to TA Lib, which can be difficult to install in some cases. I do my best to get most indicators with greater than 99% correlation for both TA Lib and TradingView, but sometimes it's not possible.

In any case, I'll keep digging around and hope that someone else has seen a solutions to this and can comment or contribute.

Kind Regards, KJ

twopirllc commented 3 years ago

Hello @slhawk98,

So I tested the exported TV data at 30min timeframe and Pandas TA vwap does work as intended. Before applying vwap, your data must be in the desired timeframe (30 mins in your case). In fact there is 100% (1-1) correlation of Pandas TA's vwap with TV's vwap.

Here is the TV SPY 30min VWAP.csv source I exported for the test below.


vwapdf = pd.read_csv("../TV_SPY_30min_VWAP.csv")

vwapdf.set_index(pd.to_datetime(vwapdf["time"]), inplace=True)
vwapdf.drop(columns=["time", "VMA", "LB", "UB"], inplace=True)
vwap = vwapdf.ta.vwap(anchor="D", offset=None, append=True)

last = 150
vwapdf.index = vwapdf.index.astype("str") # To remove the session gaps for plotting
vwapdf.iloc[:,-2:].tail(last).plot(figsize=(16,8), color=["blue", "red"], linewidth=1, title=vwap.name)
vwapdf.close.tail(last).plot(figsize=(16,8), color=['black'], linewidth=1.4, grid=True)

vwapdf.iloc[:,-2:].corr()  # TV vs Pandas TA vwap correlation

Results:

Screen Shot 2021-08-31 at 8 32 59 PM


vwapdf.iloc[:,-2:].tail(30).style.background_gradient()
Screen Shot 2021-08-31 at 8 32 40 PM


Hope this clears up any confusion.

Kind Regards, KJ

twopirllc commented 3 years ago

Hello @slhawk98,

I assume by no response that the solution provided was sufficient. Thus I will be closing this issue in a few days.

Kind Regards, KJ

slhawk98 commented 3 years ago

Sorry for late response. Let me see if I can duplicate your TV SPY results. I also saw this suggestion. I will try both. Thanks

vwap = df.ta.vwap(anchor="30T", append=True) # for 30 mins

slhawk98 commented 3 years ago

I was able to replicate matching results using your data (TV SPY 30min VWAP.csv) and code above. It's possible my time series was not set correctly. I also see you used anchor="D" (default), with 30m data. Makes since. I will attempt on BTCUSDT and let you know the results. Thanks

slhawk98 commented 3 years ago

Results did not match with BTCUSDT, ETHUSDT or AAVEUSDT (Binance API Data, 30 minute candles) I also tried MSFT data from yfinance API , which did not match.

Both our date indexes results are of type string (<class 'str'>) but the SPY date appear to be delta format. Could that make a difference? Could there be a difference with API data?

2021-08-24 07:30:00 (BTCUSDT example ) 2021-06-04 14:00:00+00:00 (SPY example )

twopirllc commented 3 years ago

Sorry for late response. Let me see if I can duplicate your TV SPY results. I also saw this suggestion. I will try both. Thanks

No problem

vwap = df.ta.vwap(anchor="30T", append=True) # for 30 mins

This does not work as intended. Hence the first comment was hidden.

Results did not match with BTCUSDT, ETHUSDT or AAVEUSDT (Binance API Data, 30 minute candles) I also tried MSFT data from yfinance API , which did not match.

So you downloaded both the Binance API data for BTCUSDT, ETHUSDT, and AAVEUSDT and their vwap values to compare? If so please provide a sample csv from one of the three tickers with Binance API's vwap values; I will need it to compare and contrast. I need real data not anecdotal evidence.

Both our indexes results are of type string (<class 'str'>) but the SPY date appear to be delta format. Could that make a difference?

You can comment out vwapdf.index = vwapdf.index.astype("str"). If you do, the chart will have the gap hours included. Your choice. It was the recommended statement to remove the weird gaps in the chart. For crypto/fx and other 24/7 assets, you can likely comment that line out.

Could there be a difference with API data?

It's possible. It's not uncommon for errors or missing data to occur; it's the users responsibility to manage that. In fact, expect live/stream data to include erroneous/missing data.

To compare and correlate non-TA Lib indicators such as TV, Binance et al. I need TV data plus the TV/Binance indicator values.

Thanks, KJ

slhawk98 commented 3 years ago

I have attached the BTUSDT & ETHUDT - 30 minute csv. What package are you using to plot graph? I don't get a graph, but the code does not produce an error. I am using Windows 10, python 3.8.5 and visual studio code.

Also, not sure how you get the "TV_VWAP_30m" column in you examples above.

https://github.com/slhawk98/files/blob/main/BTCUSDT.csv https://github.com/slhawk98/files/blob/main/ETHUSDT.csv

twopirllc commented 3 years ago

@slhawk98,

What package are you using to plot graph?

Matplotlib. See Pandas Visualization

Also, not sure how you get the "TV_VWAP_30m" column in you examples above.

I manually rename the columns before reading the csv.

I have attached the BTUSDT & ETHUDT - 30 minute csv.

Are those _VWAPD's in BTUSDT & ETHUDT - 30 minute from Binance or did you generate it from Pandas TA's vwap? If _VWAPD is not from your data source, I can not compare it with Pandas TA vwap.

KJ

slhawk98 commented 3 years ago

(TV_VWAP_30m column) - "I manually rename the columns before reading the csv."

The only way I can think of to get the actual Teamviewer VWAP values is to manually hoover over each candle and manually add the TV VWAP values to the spreadsheet/csv/dataframe. Am I missing something here?

This is actually how I verified results against your SPY.csv data. Example, when I loaded the SPY 30 min chart in TeamViewer, I was able to hoover over a few candles listed in your data and the VWAP values on TV matched those returned by pandas_ta. When you hoover over a candle in Teanviewer values for all indicators are displayed.

What's confusing is why SPY (TV/Pandas_TA) values matched but not when I try using API data directly.

What code did you use to download your SPY data?

twopirllc commented 3 years ago

@slhawk98,

The only way I can think of to get the actual Teamviewer VWAP values is to manually hoover over each candle and manually add the TV VWAP values to the spreadsheet/csv/dataframe. Am I missing something here? What code did you use to download your SPY data?

TeamViewer, The Remote Desktop Application Software? Or did you mean TradingView? 🤔

Yes. That is of course inefficient. There currently is no script or Python package you can use to download data from TV nor do they provide REST API access for non-brokers. 😑 You need a paid TV plan that allows you to manually (click) export/download the data. See the following https://github.com/twopirllc/pandas-ta/issues/376#issuecomment-902123018 process I use so I can do correlation tests with Pandas TA.

What's confusing is why SPY (TV/Pandas_TA) values matched but not when I try using API data directly.

I am curious also. Let me know when you find out. 😎

KJ

slhawk98 commented 3 years ago

;-) - Mixing my TV applications I meant TradingView. Bummer. I have TV Pro but you need Pro+ to export data. You can consider this one close for now. Thanks