yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

ChunkedEncodingError when reading data about ticker

Open Mustufain opened this issue 4 years ago • 6 comments

I am trying to read tickers info using the following code:

tickers_list = ['NIO', 'PLTR', 'MSFT']

tickers = ' '.join(tickers_list) tickers = yf.Tickers(tickers) print(tickers.tickers['NIO'].info)

I get this error:

requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(82 bytes read, 1069 more expected)', IncompleteRead(82 bytes read, 1069 more expected))

Environment: python 3.6.1 MacOS BigSur

Mustufain avatar Apr 03 '21 19:04 Mustufain

I'm getting the same when trying to download price history for stocks... I have been running these scripts several times daily for last years without much problem - but suddently they started to fail for well above 50% of the tickers I try to download since Thursday (1st of April). I'm running Python 3.8. I am experiencing these problems both in Windows and Linux environments... Would really appreciate if someone has any kind of input!

HeadbangerFinance avatar Apr 03 '21 20:04 HeadbangerFinance

I also have this problem.

dmitrievichh avatar Apr 04 '21 11:04 dmitrievichh

Have been trying various settings the last two days and I seem to get slightly more data with "threads"=False and I could probably live with the fact that you get some errors and that it is somewhat slow - but the primary problem right now is that the "ChunkedEncodingError" isn't returned until after almost 2 minutes...so it takes ages to run through a loop with try /except... Would be happy just to find a faster workaround at this stage, suggestions?

HeadbangerFinance avatar Apr 05 '21 10:04 HeadbangerFinance

I've also been getting this error a lot since April 1st. I'm running Python 3.8.7 on macOS Big Sur.

Update: It seems to be a problem with Yahoo Finance. My browser gets stuck loading data sometimes as well when I go to https://query1.finance.yahoo.com.

My quick-fix workaround: Go to base.py in the yfinance library and add a timeout to the requests.get, that is, change

data = self.session.get(url=url, params=params, proxies=proxy)

to

data = self.session.get(url=url, params=params, proxies=proxy, timeout=0.1)

This way, you get a timeout error after 0.1 seconds instead of having to wait a couple of minutes for the connection broken error. You can then catch the timeout error like this

try: 
    data = yf.download(ticker, period="5d", interval="1m", threads = False, progress=False)
except requests.exceptions.RequestException:
    pass

Note: You need to turn threads off otherwise you need to catch the error inside the threads.

walkeratwood avatar Apr 07 '21 14:04 walkeratwood

Thanks! Will try that workaround while yahoofinance behaves like it does currently!

HeadbangerFinance avatar Apr 09 '21 14:04 HeadbangerFinance

Latest version has timeout, can someone confirm problem gone.

ValueRaider avatar Jan 06 '23 15:01 ValueRaider