I'm using the Yahoo Financials package on python to retrieve stock data. I have a list of about 30 stocks that I'm interested in and I wrote a script to loop through this list, extract the closing price and use that data to build a dataframe.
However, I encounter the following TimeoutError:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
It also states that another error occurred while handling the above:
OSError: [Errno socket error] [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
My code is as follows:
stistocks = ["U96.SI", "D01.SI", "J36.SI", "O39.SI", "BN4.SI", "N2IU.SI", "BS6.SI", "G13.SI", "V03.SI", "S63.SI", "F34.SI", "S68.SI", "C52.SI", "Z74.SI",
"A17U.SI", "U11.SI", "H78.SI", "M44U.SI", "C31.SI", "U14.SI", "J37.SI", "T39.SI", "C6L.SI", "S58.SI", "D05.SI", "C38U.SI", "C09.SI", "C61U.SI", "C07.SI", "Y92.SI", "A35.SI"]
factor = 0
for ticker in stistocks:
yahoo_financials = YahooFinancials(ticker)
historical_data = yahoo_financials.get_historical_price_data('2000-01-01', '2020-07-17', 'daily')
historical_stock_prices = historical_data[ticker]["prices"]
date_list = [each_day["formatted_date"] for each_day in historical_stock_prices]
price_list = [each_day["adjclose"] for each_day in historical_stock_prices]
if factor == 0:
data = {"Date": date_list, ticker:price_list}
df_first = pd.DataFrame.from_dict(data)
df_first["Date"] = pd.to_datetime(df_first["Date"], format="%Y-%m-%d")
factor = factor + 1
else:
data = {"Date": date_list, ticker:price_list}
df = pd.DataFrame.from_dict(data)
df["Date"] = pd.to_datetime(df["Date"], format="%Y-%m-%d")
df_first = df_first.merge(df, on="Date", how='outer')