Search code examples
python-3.xyahoo-financeyfinance

Loop through list of stock tickers with finance


I am trying to import fundamental data for multiple stocks with yfinance

import yfinance as yf
ticker = yf.Ticker('AAPL')
ticker.info["priceToBook"]

Returns Apples P/B of 15.9 as expected

But what if I want to loop through a list of tickers, like

tickers = ['AAPL', 'ORCL', 'TSLA']

and save them to a df or list


Solution

  • You can just loop through the tickers you want and add them to a dictionary (or whatever you want)

    dictionary = {} 
    tickers = ['AAPL', 'ORCL', 'TSLA']
    for t in tickers:
        ticker = yf.Ticker(t)
        dictionary[t] = ticker.info["priceToBook"]