Downloading Stock Data from Yahoo Finance

Hey Guys,
I am trying to download stock returns from yahoo finance for all S&P500 companies in 2019.
This is how my code looks so far:

Import necessary stuff

import pandas as pd
from pandas_datareader import data
from datetime import datetime

Here I created a function to get the data from yahoo

def get_data(ticker, start_date, end_date):
try:
stock_data = data.DataReader(ticker, “yahoo”, start_date, end_date)
except Exception:
print(“Error occured”)
return stock_data

Here I created a second function to manipulate to get the daily stock returns of a stock

def get_stock_returns(ticker, start_date, end_date):
stock_data = get_data(ticker, start_date, end_date)
stock_data = stock_data.loc[:,“Adj Close”]
stock_data = stock_data.pct_change()
stock_data = stock_data.rename(ticker)
return stock_data

Then I Specified the dates

start_date = “2019-01-01”
end_date = “2019-12-31”

I created a CSV-File containing the tickers of each stock in the S&P500 and transformed it into a list - (I am sure there is a more convienent way but this is not the issue here)

temp = pd.read_csv(“S_and_P_Tickers.csv”)
matrix2 = temp[temp.columns[0]].to_numpy()
ticker_list = matrix2.tolist()

Then I want to actually load the data using this code:

dataset_2019 = pd.DataFrame()
for ticker in ticker_list:
stock = get_stock_returns(ticker, start_date, end_date)
dataset_2019 = pd.concat([dataset_2019, stock], axis=1)

But every time I execute the last part of the code there is an error:

Error occured

UnboundLocalError Traceback (most recent call last)
in
2 for ticker in (ticker_list):
3 i = i+1
----> 4 stock = get_stock_returns(ticker_list[i], start_date, end_date)
5 dataset = pd.concat([dataset, stock], axis=1)

in get_stock_returns(ticker, start_date, end_date)
1 def get_stock_returns(ticker, start_date, end_date):
----> 2 stock_data = get_data(ticker, start_date, end_date)
3 stock_data = stock_data.loc[:,“Adj Close”]
4 stock_data = stock_data.pct_change()
5 stock_data = stock_data.rename(ticker)

in get_data(ticker, start_date, end_date)
4 except Exception:
5 print(“Error occured”)
----> 6 return stock_data

UnboundLocalError: local variable ‘stock_data’ referenced before assignment

But this error always occurs at a different stage.

I ran the code 3 times.
After the first try the error occured after the data was loaded for the first 132 stocks
The second time the error occured after the data was only loaded for 4 stocks
Try number 3 returned an error after stock number 88

So, I have literally no idea why the error occurs always for a different stock. In the first try data for stock 5 was successfully loaded but in the second try stock number 5 raised an error?

Here I created a function to get the data from yahoo

def get_data(ticker, start_date, end_date):
try:
stock_data = data.DataReader(ticker, “yahoo”, start_date, end_date)
except Exception:
print(“Error occured”)
return stock_data
[…]
But every time I execute the last part of the code there is an error:
Error occured

UnboundLocalError Traceback (most recent call last)
[…]
So, I have literally no idea why the error occurs always for a
different stock. In the first try data for stock 5 was successfully
loaded but in the second try stock number 5 raised an error?

Look at the function above. If an exception occurs:

  • what is the value of stock_data?
  • what is the exception?

stock_data is never set if an exception occurs, so your function raises
an UnboundLocalError when it hits the return.

An you never print out the exception! So you have no idea why
data.DataReader failed, and no ability to fix it.

The rule of thumb for exceptions is usually that you should only catch
exceptions which you know how to handle, meaning that you have a
sensible course of action to take when that particular exception
happens.

You’re catching any exception (well, most of them). This is usually
not a great approach. And you’re not printing the exception, so you
don’t get to learn what may be going on.

At the least try this:

except Exception as e:
    print("Error occured:", e)
    stock_data = None

so that (a) you get to see the exception and (b) you set stock_data to
something.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Thanks! Saved me a ton of time and energy.