Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_historical_price_data() error #186

Open
jmalbornoz opened this issue Mar 27, 2024 · 4 comments
Open

get_historical_price_data() error #186

jmalbornoz opened this issue Mar 27, 2024 · 4 comments

Comments

@jmalbornoz
Copy link

Here's what I am trying to do:

from yahoofinancials import YahooFinancials
ticker = 'MSFT'
yahoo_financials = YahooFinancials(ticker)
data = yahoo_financials.get_historical_price_data("2024-03-01", "2024-03-26", "daily")

After a long wait of some 20 minutes I get the following error message:


TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 data = yahoo_financials.get_historical_price_data("2024-03-01", "2024-03-26", "daily")

File D:\Programs\Anaconda3\envs\quant\lib\site-packages\yahoofinancials\yf.py:163, in YahooFinancials.get_historical_price_data(self, start_date, end_date, time_interval)
161 end = self.format_date(end_date)
162 hist_obj = {'start': start, 'end': end, 'interval': interval_code}
--> 163 return self.get_stock_data('history', hist_obj=hist_obj)

File D:\Programs\Anaconda3\envs\quant\lib\site-packages\yahoofinancials\data.py:631, in YahooFinanceData.get_stock_data(self, statement_type, tech_type, report_name, hist_obj)
629 if isinstance(self.ticker, str):
630 dict_ent = self._retry_create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj)
--> 631 data.update(dict_ent)
632 else:
633 if self.concurrent:

TypeError: 'NoneType' object is not iterable

I have tried other tickers and the result is the same.

@rhandley1231
Copy link

I've been having the same issue with this code. I'm taking a Quantitative Analytics Course and am encountering this issue when trying to retrieve data with this call.

@2021Anson2016
Copy link

Here's what I am trying to do:

from yahoofinancials import YahooFinancials
ticker = 'MSFT'
yahoo_financials = YahooFinancials(ticker)
data = yahoo_financials.get_historical_price_data("2024-03-01", "2024-03-26", "daily")

After a long wait of some 20 minutes I get the following error message:

TypeError Traceback (most recent call last) Cell In[4], line 1 ----> 1 data = yahoo_financials.get_historical_price_data("2024-03-01", "2024-03-26", "daily")

File D:\Programs\Anaconda3\envs\quant\lib\site-packages\yahoofinancials\yf.py:163, in YahooFinancials.get_historical_price_data(self, start_date, end_date, time_interval) 161 end = self.format_date(end_date) 162 hist_obj = {'start': start, 'end': end, 'interval': interval_code} --> 163 return self.get_stock_data('history', hist_obj=hist_obj)

File D:\Programs\Anaconda3\envs\quant\lib\site-packages\yahoofinancials\data.py:631, in YahooFinanceData.get_stock_data(self, statement_type, tech_type, report_name, hist_obj) 629 if isinstance(self.ticker, str): 630 dict_ent = self._retry_create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj) --> 631 data.update(dict_ent) 632 else: 633 if self.concurrent:

TypeError: 'NoneType' object is not iterable

I have tried other tickers and the result is the same.

I have met the same problem.

@alphaveneno
Copy link

I have encountered this same error intermittently over the last year or so, usually involving some but not all stocks. Today (Sept 28, 2024) it involves all stocks.

I can only speak to error-handling right now. Here's how I handle the error:

from kill_timeout import kill_timeout

....
try:
lastyear = self.get_price_data(financials, prev_yr, this_yr)
except TimeoutError as e:
print(f'get_price_data for {symbol} {prev_yr} could not complete within 15 seconds and was terminated\n')
logging.error(e)
....

############################ HELPER FUNCTIONS ########################################
@kill_timeout(15)
def get_price_data(self, financials, yr1, yr2):
price_data = financials.get_historical_price_data(f'{yr1}-01-01', f'{yr2}-01-01', "daily")
return price_data

I import kill_timeout and use it to decorate a helper function, which attempts the actual download, giving the application 15 seconds to complete this task. I log the error when the exception occurs, moving on to the next stock.

@alphaveneno
Copy link

alphaveneno commented Sep 29, 2024

There is a bug in /lib/python3.11/site-packages/yfinance/cache.py

Look at this block of code around line 374:

try: data = _CookieSchema.get(_CookieSchema.strategy == strategy) cookie = _pkl.loads(data.cookie_bytes) return {'cookie':cookie, 'age':_datetime.datetime.now()-data.fetch_date} except _CookieSchema.DoesNotExist: return None

data.fetch_date should be a datetime object, but it is actually a string. Of course you cannot subtract a string from a datetime object.

Here is my corrected version of the code:

try: data = _CookieSchema.get(_CookieSchema.strategy == strategy) datetime_object = _datetime.datetime.strptime(data.fetch_date, '%Y-%m-%dT%H:%M:%S.%f') cookie = _pkl.loads(data.cookie_bytes) return {'cookie': cookie, 'age': _datetime.datetime.now() - datetime_object} except _CookieSchema.DoesNotExist: return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants