I am wanting to use pandas-ta. Although most aspects of this library seem easier for technical analysis I can only make it function on single ticker dataframes.
I would like to figure out how to get pandas-ta to work over multiple tickers in a multiindex dataframe.
I get the data using: - where [stocks] come from a csv list.
df = yf.download[stocks], '2021-1-1', interval='1d')
the pandas-ta download method below only creates a single ticker dataframe and only iterates the first ticker when using [stocks].
df.ta.ticker('GOOG', period = '1y', interval = "1h")
My current dataframe appears something like below. (where the list of tickers will change)
Adj Close Close High Low Open Volume
BTC-USD ETH-USD BTC-USD ETH-USD BTC-USD ETH-USD BTC-USD ETH-USD BTC-USD ETH-USD BTC-USD ETH-USD
Date
2020-12-31 29001.720703 737.803406 29001.720703 737.803406 29244.876953 754.299438 28201.992188 726.511902 28841.574219 751.626648 46754964848 13926846861
2021-01-01 29374.152344 730.367554 29374.152344 730.367554 29600.626953 749.201843 28803.585938 719.792236 28994.009766 737.708374 40730301359 13652004358
2021-01-02 32127.267578 774.534973 32127.267578 774.534973 33155.117188 786.798462 29091.181641 718.109497 29376.455078 730.402649 67865420765 19740771179
2021-01-03 32782.023438 975.507690 32782.023438 975.507690 34608.558594 1006.565002 32052.316406 771.561646 32129.408203 774.511841 78665235202 45200463368
2021-01-04 31971.914062 1040.233032 31971.914062 1040.233032 33440.218750 1153.189209 28722.755859 912.305359 32810.949219 977.058838 81163475344 56945985763
When I try to apply a pandas-ta function such as:
df[stocks] = data[stocks].ta.sma(length=10)
I get the error. AttributeError: 'Series' object has no attribute 'ta'
When I use the documentation standard method
sma10 = ta.sma(df["Close"], length=10)
I don't know how to target the specific (BTC-USD)'Close' columns for all tickers in the .csv list - ie. (df['Close']
In both examples pandas-ta sma is using the 'close' value but I'm hoping to be able to apply all pandas-ta methods to a multiindex.
I can download 'Close' only data -
data = yf.download[stocks], '2021-1-1', interval='1d')['Close']
however the columns will be the 'ticker names' containing 'Close' data and I still have the same issue with pandas-ta trying to find the 'close' column data.
I don't know how to make pandas-ta function over multiple tickers in the same dataframe. Is there a solution to this?
Thanks for any help!
Since each column of multi-column consists of a tuple, it is possible to deal with data frames in horizontal format by specifying them in tuple format using .loc, etc. Two types of technical analysis are added by loop processing. The last step is to reorder the columns. If you need to handle more than just the closing price, you can use the closing price as the target of the loop.
import pandas as pd
import pandas_ta as ta
import yfinance as yf
stocks = 'BTC-USD ETH-USD XRP-USD XEM-USD'
df = yf.download(stocks, '2021-1-1', interval='1d',)
technicals = ['sma10', 'sma25', 'vwma']
tickers = stocks.split(' ')
for ticker in tickers:
for t in technicals:
if t[:2] == 'sma':
l = int(t[3:])
df[(t, ticker)] = ta.sma(df.loc[:,('Close', ticker)], length=l)
else:
df[(t, ticker)] = ta.vwma(df.loc[:,('Close', ticker)], df.loc[:,('Volume', ticker)])