yfinance
yfinance copied to clipboard
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I've been using yfinance for some time and was working great. All of a sudden, I am getting this error (sporadically) on the "download" when I specifically use the following:
import yfinance as yf start = 'some valid date' end = 'another valid date' data = yf.download(tickers[x], start=starting_point, end=ending_point, progress=False)
Hello! Also had the same/similar problem (first post on GitHub how about that!). I used the following:
tickers = Final_Tickers start = datetime.datetime(2018,12,27) end = datetime.datetime(2019,12,27) findata = yf.download(tickers, start=start, end=end, interval = '3mo')
Prices
price_2018 = findata['Adj Close'].iloc[0,:] price_2019 = findata['Adj Close'].iloc[-1,:]
Final_Tickers is just a list I created of stock tickers. My progress bar gets stuck at around 90% and with every progress bar there's this error:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Thanks!
Hi boys.
I can confirm this bug.
Exception in thread Thread-4:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 470, in _download_one_threaded
period, interval, prepost, proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 483, in _download_one
proxy=proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 297, in history
data = data.json()
File "/app/.heroku/python/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/app/.heroku/python/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[*** 6% ] 6 of 94 downloadedException in thread Thread-12:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 470, in _download_one_threaded
period, interval, prepost, proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 483, in _download_one
proxy=proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 297, in history
data = data.json()
File "/app/.heroku/python/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/app/.heroku/python/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Exception in thread Thread-15:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 470, in _download_one_threaded
period, interval, prepost, proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 483, in _download_one
proxy=proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 297, in history
data = data.json()
File "/app/.heroku/python/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/app/.heroku/python/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Exception in thread Thread-13:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/app/.heroku/python/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 470, in _download_one_threaded
period, interval, prepost, proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 483, in _download_one
proxy=proxy)
File "/app/.heroku/python/lib/python3.6/site-packages/yfinance/__init__.py", line 297, in history
data = data.json()
File "/app/.heroku/python/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/app/.heroku/python/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[************ 26% ] 24 of 94 downloaded
I'm also getting the same error:
[ 0% ] [* 3% ] 2 of 75 completed [** 4% ] 3 of 75 completed [** 5% ] 4 of 75 completed [*** 7% ] 5 of 75 completed [**** 8% ] 6 of 75 completed [**** 9% ] 7 of 75 completedException in thread Thread-11: Traceback (most recent call last): File "xxx\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File xxx\Python\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self.kwargs) File "xxx\Python\Python38-32\lib\site-packages\multitasking_init.py", line 102, in _run_via_pool return callee(*args, **kwargs) File "xxx\Python\Python38-32\lib\site-packages\yfinance\multi.py", line 166, in _download_one_threaded data = _download_one(ticker, start, end, auto_adjust, back_adjust, File "xxx\Python\Python38-32\lib\site-packages\yfinance\multi.py", line 178, in download_one return Ticker(ticker).history(period=period, interval=interval, File "xxx\Python\Python38-32\lib\site-packages\yfinance\base.py", line 155, in history data = data.json() File "xxx\Python\Python38-32\lib\site-packages\requests\models.py", line 898, in json return complexjson.loads(self.text, **kwargs) File "xxx\Python\Python38-32\lib\json_init.py", line 357, in loads return _default_decoder.decode(s) File "xxx\Python\Python38-32\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "xxx\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
for the following code:
import pandas as pd
import pandas_datareader.data as pdr
import yfinance as yf
yf.pdr_override()
tickers = [lots of tickers here]
result = pdr.get_data_yahoo(tickers)
I tried running it via vs code, python terminal, and Jupyter, also tried with VPN, but no success. I also tried multiple times passing the same stock ticker and sometimes it would work and sometimes won't.
Same boat here, and I think i know why this happens, but don't have a solution or a workaround. I think what's happening is Yahoo is probably trying to block your requests when you have multiple. I saw this by simply adding a line within the history function in base.py. BUT when i try to access the url manually, it shows up just fine.
url = "{}/v8/finance/chart/{}".format(self._base_url, self.ticker) # print (url) data = _requests.get(url=url, params=params, proxies=proxy) data.raise_for_status() if "Will be right back" in data.text: raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n" "Our engineers are working quickly to resolve " "the issue. Thank you for your patience.") data = data.json()
Proof of same issue: [.......] [*****************62% ] 79 of 128 completedException in thread Thread-96: Traceback (most recent call last): File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/usr/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/multitasking/init.py", line 102, in _run_via_pool return callee(*args, **kwargs) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/yfinance/multi.py", line 167, in _download_one_threaded actions, period, interval, prepost, proxy, rounding) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/yfinance/multi.py", line 182, in _download_one rounding=rounding, many=True) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/yfinance/base.py", line 152, in history data.raise_for_status() File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://query1.finance.yahoo.com/v8/finance/chart/CAT?period1=1592798400&period2=1593230400&interval=1d&includePrePost=False&events=div%2Csplits
[*****************62% ] 80 of 128 completedException in thread Thread-100: Traceback (most recent call last): File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/usr/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/multitasking/init.py", line 102, in _run_via_pool return callee(*args, **kwargs) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/yfinance/multi.py", line 167, in _download_one_threaded actions, period, interval, prepost, proxy, rounding) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/yfinance/multi.py", line 182, in _download_one rounding=rounding, many=True) File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/yfinance/base.py", line 152, in history data.raise_for_status() File "/home/jaimid/Programming/robinstocksautotrading/venv/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://query1.finance.yahoo.com/v8/finance/chart/AMZN?period1=1592798400&period2=1593230400&interval=1d&includePrePost=False&events=div%2Csplits [.......]
Same problem. tried single symbol pulls in a loop with a pause between attempts to avoid multiple simultaneous requests, and still the error occurs.
when i then try yf.download(symbol,start=max_hist,end=today) again on the symbol that caused the error, it works.
OK, found a solution that appears to work for me: add "threads= False" yf.download(symbol,threads= False,start=max_hist,end=today)
OK, found a solution that appears to work for me: add "threads= False" yf.download(symbol,threads= False,start=max_hist,end=today)
I've tried. The progress bar goes further, but it hangs.
OK, found a solution that appears to work for me: add "threads= False" yf.download(symbol,threads= False,start=max_hist,end=today)
Worked for me. Thanks!
Was getting this today as well. I used it on Friday with no errors. Add threads= False stopped it from hanging. Was using get_data_yahoo.
OK, found a solution that appears to work for me: add "threads= False" yf.download(symbol,threads= False,start=max_hist,end=today)
Worked for me. Thanks!
So, this works on and off for me. It may not be a permanent fix. I am running this on a simple loop:
import yfinance as yf
for row in df2:
try:
df3 = yf.download(row, threads = False, start="2019-06-29", end="2020-06-26")
df3["Ticker"] = row[0]
df4 = df4.append(df3)
except:
pass
first two times I ran it, it hung up. 3rd time it worked, 4th time it hung again.
It becomes more manual, but if you are running this in a Jupyter Notebook, and you get that long error message @Vido mentions, I was able to hit the "interrupt kernel" button and it would continue with the next ticker. Not optimal, just saying.
OK, found a solution that appears to work for me: add "threads= False" yf.download(symbol,threads= False,start=max_hist,end=today)
I've tried. The progress bar goes further, but it hangs.
in general I don't input a list of symbols because one will randomly
OK, found a solution that appears to work for me: add "threads= False" yf.download(symbol,threads= False,start=max_hist,end=today)
Worked for me. Thanks!
So, this works on and off for me. It may not be a permanent fix. I am running this on a simple loop:
import yfinance as yf
for row in df2:
try:
df3 = yf.download(row, threads = False, start="2019-06-29", end="2020-06-26")
df3["Ticker"] = row[0]
df4 = df4.append(df3)
except:
pass
first two times I ran it, it hung up. 3rd time it worked, 4th time it hung again.
It becomes more manual, but if you are running this in a Jupyter Notebook, and you get that long error message @Vido mentions, I was able to hit the "interrupt kernel" button and it would continue with the next ticker. Not optimal, just saying.
unfortunately, pulling a list of names all at once never worked consistently for me. instead, i just have a list that i loop through to pull symbols individually(like your example). if it fails, then i create a list of errors and then iterate through the error list again in the end.
without threads = False the loop would fail and my script stops. with threads = False, it will continue on to the next symbol.
In other new, I fixed my Date logic, thanks for the bug!
two_business_day_ago = (date.today() - BDay(2)).strftime("%Y-%m-%d") one_business_day_ago = (date.today() - BDay(1)).strftime("%Y-%m-%d") today = date.today().strftime("%Y-%m-%d") tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d") if datetime.now().time() > time(16,00): market_last_close_add_a_day = tomorrow compare_market_close = one_business_day_ago elif datetime.today().weekday() < 5 or datetime.today().weekday() == 0: market_last_close_add_a_day = today compare_market_close = two_business_day_ago
Seems like the unaltered code is working again for me. I've seen this issue in the past with other yahoo downloaders where something is an issue with multiple requests from Yahoo historical data, then it resolves itself in a day or two. Something must be going on with their servers when these issues happen.
If yfinance breaks again in the future I've put together a basic way to read stock data from Yahoo. This just uses built in functions within Python (tabulate is not native but can be easily replaced with just a straight print function). You can also save these dataframes to a csv by just adding the pandas function at the end of this code. https://github.com/MLauer91/Cheap-Finance-Tools/blob/master/Testing%20CSV%20Yahoo%20Finance%20Download%20(1%20Yr%20Trailing).py
If yfinance breaks again in the future I've put together a basic way to read stock data from Yahoo. This just uses built in functions within Python (tabulate is not native but can be easily replaced with just a straight print function). You can also save these dataframes to a csv by just adding the pandas function at the end of this code. https://github.com/MLauer91/Cheap-Finance-Tools/blob/master/Testing%20CSV%20Yahoo%20Finance%20Download%20(1%20Yr%20Trailing).py
I think if that happens again, I would prefer to have an application that instead of doing only requests, it too storage data and structures. That way you can easily hold the data, instead of just having the features to do requests (And of course you have the freedom to use the library the way that you want). The requests will be already set, and the structures. The data would be updated with the recursion to timers, that way you have de data up to date.
Happen again.
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1984/3850486091.py in <module>
4 # while True:
5 # try:
----> 6 if len(yf.Ticker(symbol).options) > 0:
7 option_symbols.append(symbol)
8
D:\Miniconda3\lib\site-packages\yfinance\ticker.py in options(self)
193 def options(self):
194 if not self._expirations:
--> 195 self._download_options()
196 return tuple(self._expirations.keys())
D:\Miniconda3\lib\site-packages\yfinance\ticker.py in _download_options(self, date, proxy)
54 proxy = {"https": proxy}
55
---> 56 r = _requests.get(url=url, proxies=proxy).json()
57 if len(r.get('optionChain', {}).get('result', [])) > 0:
58 for exp in r['optionChain']['result'][0]['expirationDates']:
D:\Miniconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
898 # used.
899 pass
--> 900 return complexjson.loads(self.text, **kwargs)
901
902 @property
D:\Miniconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
348 cls = JSONDecoder
D:\Miniconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
D:\Miniconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Yep, I just hit the same error
Same problem here. It worked one hour ago.
Me too...
me too...
dup: https://github.com/ranaroussi/yfinance/issues/691
I think this is caused by the Yahoo server side.
dup: #691
I think this is caused by the Yahoo server side.
I'm not an expert but if this error happens to everyone at the same moment, it's maybe that.
It's strange that this happens a few days after pandas_datareader is no longer functional at all for yahoo finance...
Yes happening to me as well..
BTW, for all of us who hit this problem: is there another source other than Yahoo we can download daily US stock bars, reliably?
BTW, for all of us who hit this problem: is there another source other than Yahoo we can download daily bars?
For which market (US, India...)? I heard about this one
It's strange that this happens a few days after pandas_datareader is no longer functional at all for yahoo finance...
Not sure about you, but my same program is working yesterday (downloading hundreds of stock symbols), so I don't think it's a pandas_datareader issue. BTW I'm using pandas-datareader 0.9.0
@mingwugmail Quandl but you have to pay the subscription fee, it might be free depending on what tickers you need and how much history
It's strange that this happens a few days after pandas_datareader is no longer functional at all for yahoo finance...
Not sure about you, but my same program is working yesterday (downloading hundreds of stock symbols), so I don't think it's a pandas_datareader issue. BTW I'm using pandas-datareader 0.9.0
pandas-datareader did have problems with yahoo finance early this week, and it hasn't been solved yet
Or go for alpha vantage, IEX cloud, or twelve. Tiingo also.
For which market (US, India...)? I heard about this one
I'm only interested in US stocks. (original post edited).