Search code examples
pythonapifor-loopstockyfinance

yfinance as an alternative to pandas_finance


Looking to use yfinance as an alternative to pandas_finance to pull stock information (industry, sector, profile) after Yahoo made changes in mid-2021. I have a list of ~50-100 tickers (unchecked_tickers) that I'm hoping to loop through to get industry, sector, profile. Here's my current code that was relatively quick for pandas_finance but takes multiple hours for yfinance:

sector = []

for i in range(len(unchecked_tickers)):
        try: 
            sec_data = yf.Ticker(unchecked_tickers[i]).info['sector']
            sector.append(sec_data)
        except:
            sector.append('0')

unchecked_earnings_df["Sector"] = sector

industry = []

for i in range(len(unchecked_tickers)):
        try: 
            ind_data = yf.Ticker(unchecked_tickers[i]).info['industry']
            industry.append(ind_data)
        except:
            industry.append('0')

unchecked_earnings_df["Industry"] = industry

desc = []

for i in range(len(unchecked_tickers)):
        try: 
            desc_data = yf.Ticker(unchecked_tickers[i]).info['longBusinessSummary']
            desc.append(desc_data)
        except:
            desc.append('0')

unchecked_earnings_df["Desc"] = desc

unchecked_earnings_df.head()

Solution

  • Each yf.Ticker("Stock ticker") function call sends a request to a server where the stock data is stored. If you do this request many times it can take a while. At some point the time that is needed to get a respones gets even longer which is probably caused by the server because the server don't want you to send to many requests in such a small period of time but im not sure if this is the real reason. You can even proof that, when you measure the time before and after each yf.Ticker() function call and print the difference. To solve the problem of the long response time you can't do much about it. But you have a lot in your code that you can improve. The main problem is that you have 3 different for loops that all do the same iteration and all do the same yf.Ticker('stock ticker') function call at the beginning which is the function that takes the most time. To improve the performance you could just do all the things do in the three for loops in one for loop and just call the yf.Ticker() function once per iteration. Liker this:

    for i in range(len(unchecked_tickers)):
        try: 
            ticker = yf.Ticker(unchecked_tickers[i])
            sector.append(ticker.info['sector'])
            industry.append(ticker.info['industry'])
            #.....
        except:
            sector.append('0')
            industry.append('0')
            #.....
    

    This way you woud have to call the yf.Ticker() function only once for each ticker instead of three times which shoud make your program three times faster.