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?