JECSand / yahoofinancials

A powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.
https://pypi.python.org/pypi/yahoofinancials
MIT License
911 stars 215 forks source link

bug using get_financial_stmts: ValueError: read of closed file #15

Closed Eric-szh closed 5 years ago

Eric-szh commented 5 years ago

I am using a demo code on the front page:

from yahoofinancials import YahooFinancials

yahoo_financials = YahooFinancials('AAPL')
print(yahoo_financials.get_financial_stmts('annual', 'income'))

and here is the error: Traceback (most recent call last): File "/Users/eric/Desktop/demp.py", line 6, in <module> print(yahoo_financials.get_financial_stmts('annual', 'income')) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 502, in get_financial_stmts data = self._run_financial_stmt(statement_type, report_num, reformat) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 492, in _run_financial_stmt raw_data = self.get_stock_data(statement_type, report_name=report_name) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 424, in get_stock_data dict_ent = self._create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 348, in _create_dict_ent re_data = self._scrape_data(YAHOO_URL, tech_type, statement_type) File "/usr/local/lib/python3.7/site-packages/yahoofinancials/__init__.py", line 135, in _scrape_data response_content = response.read() File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tempfile.py", line 481, in func_wrapper return func(*args, **kwargs) ValueError: read of closed file

JECSand commented 5 years ago

Eric,

My apologies, I should have been clear in the documentation. We are not supporting python 3.7 yet. It will be supported in a future release though.

Eric-szh commented 5 years ago

Thanks for that. Now I switched my environment from 3.7 to 3.5 by using pyenv. Unfortunately, this error happens again:

raceback (most recent call last): File "/Users/eric/Desktop/demo.py", line 6, in <module> print(yahoo_financials.get_financial_stmts('annual', 'income')) File "/Users/eric/.pyenv/versions/3.5.0/lib/python3.5/site-packages/yahoofinancials/__init__.py", line 502, in get_financial_stmts data = self._run_financial_stmt(statement_type, report_num, reformat) File "/Users/eric/.pyenv/versions/3.5.0/lib/python3.5/site-packages/yahoofinancials/__init__.py", line 492, in _run_financial_stmt raw_data = self.get_stock_data(statement_type, report_name=report_name) File "/Users/eric/.pyenv/versions/3.5.0/lib/python3.5/site-packages/yahoofinancials/__init__.py", line 424, in get_stock_data dict_ent = self._create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj) File "/Users/eric/.pyenv/versions/3.5.0/lib/python3.5/site-packages/yahoofinancials/__init__.py", line 348, in _create_dict_ent re_data = self._scrape_data(YAHOO_URL, tech_type, statement_type) File "/Users/eric/.pyenv/versions/3.5.0/lib/python3.5/site-packages/yahoofinancials/__init__.py", line 135, in _scrape_data response_content = response.read() File "/Users/eric/.pyenv/versions/3.5.0/lib/python3.5/tempfile.py", line 482, in func_wrapper return func(*args, **kwargs) ValueError: read of closed file

Is it possible that it is pyenv that caused the problem?

JECSand commented 5 years ago

Hmm, I can't seem to replicate this for some reason. I did find this however:

https://stackoverflow.com/questions/37024266/how-to-overcome-valueerror-read-of-closed-file-in-python-3-5-1

I am wondering if Yahoo Finance is returning a 400 to your request. Maybe based on your geographic location? A pyenv shouldn't be causing any problems. I am only saying this because I can't seem to replicate this issue and have had errors caused by differences in timezones before. If you clone the repo, go into init.py, and add print(response.getcode()) under line 134, what does it output for you?

If you get a 400 type return like I am expecting, could you share with me the timezone you are running this code in? You can email me at sandersconnor1@gmail.com if you want to keep that info private.

Thanks!

Connor

Eric-szh commented 5 years ago

Ummm,I downloaded your repo and put it on the desktop, added print(response.getcode()) under line 134 and ran demo.py in the repo. It returns 500, which is Internal server error ??? Also, my timezone is UTC/GMT+08:00 and I am wondering if it causes problems.

Usernamelesss commented 5 years ago

Hello, thanks JECSand for your code. I really like it!

I've found the same "ValueError" bug. It appears when I try to make many calls to YF. https://pastebin.com/dJwrhdwf

My ticker array contains over 2k symbols so probably YF blocks web scraping. Is it possible ?

JECSand commented 5 years ago

@Usernameles

Thank you SO much for your feedback here, I was scratching my head on this one. I have a possible solution to this problem I believe. At worst it will atleast handle the error nicely without breaking your process, at best it will get all of your data.

Is there any chance you could send me your ticker array with 2k symbols so I can start testing tonight?

Usernamelesss commented 5 years ago

@JECSand sure! Here you can find the CSV I used to extract all the symbols from NASDAQ https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download You can simply use pandas to import it and get values.

I did a workaround (loop on ticker array and get values symbol by symbol sleeping between two different tickers and between different metrics) to avoid that bug, but I get another error...probably closed connection (after around 36h looping). It would be better manage exception without breaking the process.

JECSand commented 5 years ago

This issue should be resolved in the new v1.4 release. Let me know if you all have any more problems with this.

Thanks for all of your help and thanks for using YahooFinancials!