Forgive me if I am unclear as I am new to posting on here.
Trying to pull data and tickers from yahoo finance using the following code. When pulling from yfinance, I want to keep track of each of the tickers that fail to download (whether it be due to the stock being delisted or other) by adding to an empty list called "stocks_not_downloaded". To do so I am using a try / except block so that if a stock fails to download it appends to the stocks_not_downloaded list.
When running the code, the stocks download and when an exception is reached it shows for example "1 Failed download: ['Ticker']: Exception('%ticker%: No timezone found, symbol may be delisted')"
BUT
A) it fails to print("Could not Get Data for:", ticker) which I have set it to do and... B) when running the stocks_not_downloaded_list it returns "[]" - an empty list
It seems to be failing to recognize the error as a normal exception despite outputting the above message. I read that there may be something weird with yfinance, but I cannot figure it out.
I will post the code and the output. Thanks in advance.
I am using this data set (https://onedrive.live.com/?authkey=%21ANM0er8uwPV%2DmRg&id=3B0309A579EBD636%2113144&cid=3B0309A579EBD636) - not sure if its sketch to link a onedrive but let me know if you want it in another format.
Code
#Imports
import numpy as np
import pandas as pd
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
import datetime as dt
import mplfinance as mpf
import time
import os
from os import listdir
from os.path import isfile, join
import yfinance as yf
yf.pdr_override()
stocks_not_downloaded = []
missing_stocks = []
#Saves Stock Data to CSV
def save_to_csv_from_yahoo(folder, ticker, syear, smonth, sday, eyear, emonth, eday):
start = dt.datetime(syear, smonth, sday)
end = dt.datetime(eyear, emonth, eday)
try:
print("Get Data for :", ticker)
df = web.DataReader(ticker, start, end)["Adj Close"]
time.sleep(10)
df.to_csv(folder + ticker + ".csv")
except Exception as ex:
stocks_not_downloaded.append(ticker)
print("Could not Get Data for:", ticker)
#Returns a Named Column Data from CSV
def get_column_from_csv(file, col_name):
try:
df = pd.read_csv(file)
except FileNotFoundError:
print("File Does Not Exist")
else:
return df[col_name]
#Defines tickers and folder
tickers = get_column_from_csv("C:/Users/15189/Documents/Python_For_Finance/Wilshire-5000-Stocks.csv","Ticker")
folder = "/Users/15189/Documents/Python_For_Finance/Stock_List/"
for x in range(20):
save_to_csv_from_yahoo(folder, tickers[x], 2017, 1, 1, 2021, 8, 19)
print("Finished")
stocks_not_downloaded
As you can see I am using:
try:
print("Get Data for :", ticker)
df = web.DataReader(ticker, start, end)["Adj Close"]
time.sleep(10)
df.to_csv(folder + ticker + ".csv")
except Exception as ex:
stocks_not_downloaded.append(ticker)
print("Could not Get Data for:", ticker)
But when running:
for x in range(20):
save_to_csv_from_yahoo(folder, tickers[x], 2017, 1, 1, 2021, 8, 19)
print("Finished")
stocks_not_downloaded
I get the following output with a "[]" at the end - an empty list for stocks not downloaded along with no "Could not get data for:" message. I really appreciate any insight
Output:
Get Data for : A
[*********************100%%**********************] 1 of 1 completed
Get Data for : AA
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAL
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAME
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAN
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAOI
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAON
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAP
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAPL
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAT
[*********************100%%**********************] 1 of 1 completed
Get Data for : AAWW
[*********************100%%**********************] 1 of 1 completed
1 Failed download:
['AAWW']: Exception('%ticker%: No timezone found, symbol may be delisted')
Get Data for : AAXN
[*********************100%%**********************] 1 of 1 completed
1 Failed download:
['AAXN']: Exception('%ticker%: No timezone found, symbol may be delisted')
Get Data for : ABBV
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABC
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABCB
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABEO
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABG
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABIO
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABM
[*********************100%%**********************] 1 of 1 completed
Get Data for : ABMD
[*********************100%%**********************] 1 of 1 completed
Finished
[]
Not sure exactly why the error isn't being picked up. I tried it locally and having the same problem. Seems to be how pandas_datareader handles raised errors.
One option you could do is check if the Adj Close is empty:
try:
print("Get Data for :", ticker)
df = web.DataReader(ticker, start, end)["Adj Close"]
if df.empty:
print("No Data for :", ticker)
stocks_not_downloaded.append(ticker)
pass
time.sleep(10)
df.to_csv(folder + ticker + ".csv")
Looks like pandas-datareader has a yahoo reader built in also. Might be worth a shot: https://pandas-datareader.readthedocs.io/en/latest/readers/yahoo.html