0

I am trying to pull out multiple ticker data from the yfinance API and save it to a csv file (in total I have 1000 tickers I need to get the data for, that data being the entire table of date, open, high, low, close, volume, etc etc), so far I am able to successfully get data for 1 ticker by using the following Python code:

import yfinance as yf

def yfinance(ticker_symbol):
    ticker_data = yf.Ticker(ticker_symbol)
    tickerDF = ticker_data.history(period='1d', start='2020-09-30', end='2020-10-31')
    
    print(tickerDF)

yfinance('000001.SS')

However if I try on multiple tickers this doesn't work. Following the yfinance docs which say for multiple tickers use:

tickers = yf.Tickers('msft aapl goog')
# ^ returns a named tuple of Ticker objects

# access each ticker using (example)
tickers.tickers.MSFT.info
tickers.tickers.AAPL.history(period="1mo")
tickers.tickers.GOOG.actions

I have a couple of issue here, the docs use a string such as 'aapl' my tickers are all of digit format like '000001.SS', the ".SS" part is proving to be an issue when passing it into the code:

tickers.tickers.000001.SS.history(period="1mo")
# Clearly this wont for for a start

The next issue I am having is, even if I pass in for example 3 tickers to my function like so:

yfinance('000001.SS 000050.KS 00006.KS')
# similar to yfinance docs of tickers = yf.Tickers('msft aapl goog')

I get errors like:

AttributeError: 'Tickers' object has no attribute '000001.SS'

(I have also tried to run these into a for loop and pass each on to the Tickers object but get the same error.)

Im stuck now, I dont know how to pass in multiple tickers to yfinance and get back data that I want and the docs aren't very helpful.

Is anyone able to help me with this?

Nav
  • 15
  • 8

2 Answers2

0

Could you not just store them in an array specifying the type as dtype object then use that pull the data from.

import yfinance as yf
import numpy as np

tickers = ['msft', 'aapl', 'goog']

totalPortfolio = np.empty([len(tickers)], dtype=object)

num = 0
for ticker in tickers:
    totalPortfolio[num] = yf.download(ticker, start='2020-09-30', end='2020-10-31', interval="1d")
    num = num + 1
Scott
  • 1
  • 3
  • Hey Scott thanks for your reply, I have 1000 tickers in a csv file sitting on an s3 bucket on AWS, so at the moment to read these in i am using boto3 and a for loop to populate an empty tickers list. How does numpy help here? just interested to know? – Nav Dec 14 '20 at 09:53
  • I used it so that the array with the appropriate length would be empty and I could identify the dtype as an object so the tickers could be stored in it. If there is a more efficient way to do it omitting numpy let me know. I did it this way based on familiarity. – Scott Dec 14 '20 at 16:32
0

Take a look at the code below:

test = yf.Tickers("A B C") 
# creates test as a yf.tickers object

test_dict = test.tickers 
# creates a dict object containing the individual tickers. Can be checked with type()

You are trying to use "tickers.tickers.MSFT.info" to retrieve the ticker data from your dictionary "tickers.tickers" but like your error message says, a dict object has no attributes named after your specific ticker names. This is in general not how you access elements in a dictionary. Instead you should use the code as below (like with all dict objects):

#old code from above
test = yf.Tickers("A B C") 
test_dict = test.tickers 
#new code accessing the dict correctly
a_data = test_dict["A"]
a_data = test.tickers["A"] #does the same as the line above
b_data = test.tickers["B"] #and so on for the other tickers

In a loop this could look something like this:

ticker_list = ["A", "B", "C"] #add tickers as needed
tickers_data = {}
tickers_history = {}
for ticker in ticker_list:
    tickers_data[ticker] = yf.Ticker(ticker)
    tickers_history = tickers_data[ticker].history(period='1d', start='2020-09-30', end='2020-10-31')
#access the dicts as needed using tickers_data[" your ticker name "]

alternatively you can also use the "yf.Tickers" function to retrieve multiple tickers at once, but because you save the history seperately I don't think this will necessarily improve your code much.

You should pay attention however, that "yf.Ticker()" and "yf.Tickers()" are different functions from each other with differing syntax and are not interchangeable. You did mix that up when you tried accessing multiple tickers with your custom "yfinance()" function, that has been previously defined with the "yf.Ticker()" function and thus only accepts one symbol at a time.

passwortknacker
  • 121
  • 2
  • 8