Downloading mutliple stocks at once from yahoo finance python

ScharcoMolten picture ScharcoMolten · Apr 7, 2018 · Viewed 36.8k times · Source

I have a question about the function of yahoo finance using the pandas data reader. I'm using for months now a list with stock tickers and execute it in the following lines:

import pandas_datareader as pdr
import datetime

stocks = ["stock1","stock2",....]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2018,3,1)

f = pdr.DataReader(stocks, 'yahoo',start,end)

Since yesterday i get the error "IndexError: list index out of range", which appears only if I try to get multiple stocks.

Has anything changed in recent days which I have to consider or do you have a better solution for my problem?

Answer

abccd picture abccd · Apr 7, 2018

Updated as of 2021-01-19

tickers = ['msft', 'aapl', 'twtr', 'intc', 'tsm', 'goog', 'amzn', 'fb', 'nvda']
df = pdr.DataReader(tickers, data_source='yahoo', start='2017-01-01', end='2020-09-28')

Original Answer

If you read through Pandas DataReader's documentation, they issued an immediate depreciation on multiple data source API's, one of which is Yahoo! Finance.

v0.6.0 (January 24, 2018)

Immediate deprecation of Yahoo!, Google Options and Quotes and EDGAR. The end points behind these APIs have radically changed and the existing readers require complete rewrites. In the case of most Yahoo! data the endpoints have been removed. PDR would like to restore these features, and pull requests are welcome.

This could be the culprit to why you been getting IndexError's (or any other normally none-existant errors).


However, there is another Python package whose goal is to fix the support for Yahoo! Finance for Pandas DataReader, you can find that package here:

https://pypi.python.org/pypi/fix-yahoo-finance

According to their documentation:

Yahoo! finance has decommissioned their historical data API, causing many programs that relied on it to stop working.

fix-yahoo-finance offers a temporary fix to the problem by scraping the data from Yahoo! finance using and return a Pandas DataFrame/Panel in the same format as pandas_datareader’s get_data_yahoo().

By basically “hijacking” pandas_datareader.data.get_data_yahoo() method, fix-yahoo-finance’s implantation is easy and only requires to import fix_yahoo_finance into your code.

All you need to add is this:

from pandas_datareader import data as pdr
import fix_yahoo_finance as yf

yf.pdr_override() 

stocks = ["stock1","stock2", ...]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2018,3,1)

f = pdr.get_data_yahoo(stocks, start=start, end=end)

Or even without the need of Pandas DataReader:

import fix_yahoo_finance as yf

stocks = ["stock1","stock2", ...]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2018,3,1)
data = yf.download(stocks, start=start, end=end)