0

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!

DMac_codes
  • 17
  • 5

1 Answers1

1

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)])
r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • If you don't want to stick to the wide format, you can convert it to the long format, add the technical analysis you need, and finally convert it back to the wide format. If my answer is helpful, please accept my response. – r-beginners Jan 07 '22 at 13:04
  • Hi r-beginners thanks for your answer. It works for the most part however, I'm having trouble getting it to work over all tickers in the df. I have used `code stocks = (' '.join(stocks))` to strip the list down to the way you presented it. But the sma10 and sma25 columns are only adding for one ticker not all tickers. How can I get the results for every ticker and is there a control in the code that gives me control of which tickers are implemented on? – DMac_codes Jan 08 '22 at 02:15
  • The last line, this one, specifies that only one stock should be displayed. Simply try to display the `df`. Also, I'm updating the posted code because I was using unnecessary variables in some of the code. Please run all my code and check it out. The way you have it in your comment, it will be one stock, so I need to add more loop processing. I am creating a column for two indicators, two stocks at a time. – r-beginners Jan 08 '22 at 03:29
  • So sorry, maybe I'm missing something, but to clarify. I have 25 tickers that I will be correlating with the pandas-ta tools. At the moment my output looks like yours (for 25 tickers -ohlcv - not only 2) however, only 2 of the tickers have the additional sma10 and sma25 columns (not all 25). It also appears that they are the first 2 tickers in the original 'stocks' list (not the MultiIndex as it is alphabetical). To simplify I have removed the ' df.iloc[:,0:8].tail() ' feature so that I don't confuse display with dataframe. Is there anything I can do to run it over all tickers. Thanks again – DMac_codes Jan 08 '22 at 05:10
  • Since 25 stocks are unknown, I modified the program by adding to 4. The first loop process is the stocks and the next loop is the technical analysis. Now replace the list of stocks with 25. However, each stock must be separated by a space. – r-beginners Jan 08 '22 at 07:28
  • We have a winner. Thank you for your time and effort! – DMac_codes Jan 09 '22 at 06:46
  • Thank you again r-beginners. I have one very last issue. I can't seem to run indicators that require multiple ohlcv (e.g vwma requires 'close' & 'volume'). Can you include how to run more than one value of ohlcv in something like data[(t, ticker)] = ta.vwma.(data.loc[:,('Close', ticker]) where ('Close', ticker]) requires multi ohlcv (open, high, low, volume) as well as close? I don't know how to translate the offical method `ta.example(df['high'], df['low'],df['close'],df['volume'])` into the method above, As a newb I am very grateful for any help – DMac_codes Jan 09 '22 at 14:23
  • The current mechanism is to set the target period for the moving average by getting it from the technical analysis name. This cannot fit multiple prices, so I modified it so that moving averages and volume weighted moving averages can be added based on the analysis name. – r-beginners Jan 09 '22 at 14:44