Can't connect to Yahoo - Errors: Loader: failed to cache the new benchmark returns
Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Environment
- Operating System: Linux hostname 2.6.18-238.12.1.el5 #1 SMP Tue May 31 13:22:04 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux
- Python Version: Python 3.5.1
- Python Bitness: 32
- How did you install Zipline: pip
- Python packages:
alembic==0.9.1 appdirs==1.4.3 bcolz==0.12.1 Bottleneck==1.3.0.dev0 click==6.7 contextlib2==0.5.5 cyordereddict==1.0.0 Cython==0.25.2 decorator==4.0.11 empyrical==0.2.2 intervaltree==2.1.0 Logbook==1.0.0 lru-dict==1.1.6 Mako==1.0.6 MarkupSafe==1.0 multipledispatch==0.4.9 networkx==1.11 numexpr==2.6.2 numpy==1.12.1 packaging==16.8 pandas==0.18.1 pandas-datareader==0.3.0.post0 patsy==0.4.1 pyparsing==2.2.0 python-dateutil==2.6.0 python-editor==1.0.3 pytz==2017.2 requests==2.13.0 requests-file==1.4.2 requests-ftp==0.3.1 scipy==0.19.0 setuptools-scm==1.15.5 six==1.10.0 sortedcontainers==1.5.7 SQLAlchemy==1.1.9 statsmodels==0.8.0 tables==3.4.2 toolz==0.8.2 zipline==1.1.0
Now that you know a little about me, let me tell you about the issue I am having:
Description of Issue
I just ran the buyapple.py example that described on http://www.zipline.io/beginner-tutorial.html, but got some errors, can you help to check ?
/home/kevinyuan/dev/zipline> bin/zipline ingest Downloading Bundle: quantopian-quandl [####################################] 100% Writing data to /home/kevinyuan/.zipline/data/quantopian-quandl/2017-05-01T16;36;49.048566.
/home/kevinyuan/dev/zipline> bin/zipline run -f examples/buyapple.py -s 2000-1-1 -e 2001-12-31 [2017-05-01 16:33:28.023652] INFO: Loader: Cache at /home/kevinyuan/.zipline/data/^GSPC_benchmark.csv does not have data from 1990-01-02 00:00:00+00:00 to 2017-04-27 00:00:00+00:00. Downloading benchmark data for '^GSPC'. [2017-05-01 16:33:28.076307] ERROR: Loader: failed to cache the new benchmark returns Traceback (most recent call last): File "/share/dev/tools/lib/python3.5/urllib/request.py", line 1240, in do_open h.request(req.get_method(), req.selector, req.data, headers) File "/share/dev/tools/lib/python3.5/http/client.py", line 1083, in request self.send_request(method, url, body, headers) File "/share/dev/tools/lib/python3.5/http/client.py", line 1128, in send_request self.endheaders(body) File "/share/dev/tools/lib/python3.5/http/client.py", line 1079, in endheaders self._send_output(message_body) File "/share/dev/tools/lib/python3.5/http/client.py", line 911, in _sendoutput self.send(msg) File "/share/dev/tools/lib/python3.5/http/client.py", line 854, in send self.connect() File "/share/dev/tools/lib/python3.5/http/client.py", line 1237, in connect serverhostname=server_hostname) File "/share/dev/tools/lib/python3.5/ssl.py", line 376, in wrap_socket context=self) File "/share/dev/tools/lib/python3.5/ssl.py", line 747, in _init self.do_handshake() File "/share/dev/tools/lib/python3.5/ssl.py", line 983, in do_handshake self.sslobj.do_handshake() File "/share/dev/tools/lib/python3.5/ssl.py", line 628, in do_handshake self._sslobj.dohandshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/data/loader.py", line 247, in ensure_benchmark_data last_date, File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/data/benchmarks.py", line 59, in get_benchmark_returns squeeze=True, # squeeze tells pandas to make this a Series File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/pandas/io/parsers.py", line 562, in parser_f return read(filepath_or_buffer, kwds) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/pandas/io/parsers.py", line 301, in read compression=kwds.get('compression', None)) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/pandas/io/common.py", line 308, in get_filepath_or_buffer req = urlopen(str(filepath_or_buffer)) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 162, in urlopen return opener.open(url, data, timeout) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 465, in open response = self.open(req, data) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 483, in _open '_open', req) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 443, in _callchain result = func(*args) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 1283, in https_open context=self.context, check_hostname=self.check_hostname) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 1242, in doopen raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATEVERIFY_FAILED] certificate verify failed (_ssl.c:645)> Traceback (most recent call last): File "/share/dev/tools/lib/python3.5/urllib/request.py", line 1240, in doopen h.request(req.get_method(), req.selector, req.data, headers) File "/share/dev/tools/lib/python3.5/http/client.py", line 1083, in request self.send_request(method, url, body, headers) File "/share/dev/tools/lib/python3.5/http/client.py", line 1128, in send_request self.endheaders(body) File "/share/dev/tools/lib/python3.5/http/client.py", line 1079, in endheaders self._send_output(message_body) File "/share/dev/tools/lib/python3.5/http/client.py", line 911, in _sendoutput self.send(msg) File "/share/dev/tools/lib/python3.5/http/client.py", line 854, in send self.connect() File "/share/dev/tools/lib/python3.5/http/client.py", line 1237, in connect serverhostname=server_hostname) File "/share/dev/tools/lib/python3.5/ssl.py", line 376, in wrap_socket context=self) File "/share/dev/tools/lib/python3.5/ssl.py", line 747, in _init self.do_handshake() File "/share/dev/tools/lib/python3.5/ssl.py", line 983, in do_handshake self.sslobj.do_handshake() File "/share/dev/tools/lib/python3.5/ssl.py", line 628, in do_handshake self._sslobj.dohandshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "bin/zipline", line 11, in sys.exit(main()) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/click/core.py", line 722, in call return self.main(args, **kwargs) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/click/core.py", line 697, in main rv = self.invoke(ctx) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/click/core.py", line 1066, in invoke return process_result(sub_ctx.command.invoke(sub_ctx)) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/click/core.py", line 895, in invoke return ctx.invoke(self.callback, *ctx.params) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/click/core.py", line 535, in invoke return callback(args, **kwargs) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/main.py", line 97, in _ return f(args, **kwargs) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func return f(get_current_context(), *args, *kwargs) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/main.py", line 240, in run environ=os.environ, File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/utils/run_algo.py", line 132, in run env = TradingEnvironment(asset_db_path=connstr) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/finance/trading.py", line 101, in init self.bm_symbol, File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/data/loader.py", line 164, in load_market_data trading_day, File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/data/loader.py", line 247, in ensure_benchmark_data last_date, File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/zipline/data/benchmarks.py", line 59, in get_benchmark_returns squeeze=True, # squeeze tells pandas to make this a Series File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/pandas/io/parsers.py", line 562, in parser_f return read(filepath_or_buffer, kwds) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/pandas/io/parsers.py", line 301, in _read compression=kwds.get('compression', None)) File "/home-tahoe-n2/kevinyuan/dev/zipline/lib/python3.5/site-packages/pandas/io/common.py", line 308, in get_filepath_or_buffer req = _urlopen(str(filepath_or_buffer)) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 162, in urlopen return opener.open(url, data, timeout) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 465, in open response = self.open(req, data) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 483, in open '_open', req) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 443, in _callchain result = func(args) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 1283, in https_open context=self.context, check_hostname=self.check_hostname) File "/share/dev/tools/lib/python3.5/urllib/request.py", line 1242, in doopen raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATEVERIFY_FAILED] certificate verify failed (_ssl.c:645)>
Hi @kevinyuan. I see you've tried installing with pip. Did you check to make sure you have all the necessary dependencies? (Check zipline.io)
Also have you tried using a Python 3.4 conda/virtualenv? As we don't support Python 3.5 yet.
I get a similar error when creating an empty TradingAlgorithm(). I traced it down to the Yahoo! URL for the benchmark returns no longer works.
The code generates the following URL:
https://ichart.finance.yahoo.com/table.csv?a=11&s=%5EGSPC&b=29&e=15&d=4&g=d&f=2017&c=1989
which does not work.
This is generated from format_yahoo_index_url() in zipline/data/benchmarks.py.
@pbharrin I've just been able to reproduce this issue today as well. Will look into how to fix this/provide an improved solution at some point.
Same here today. Not sure how to fix the problem.
All right, now that I'm at my laptop ... it looks like this is an issue coming from Yahoo and their ichart/Finance API.
I believe we have some benchmark data that is cached/downloaded but when those benchmarks are not found then we make additional requests to Yahoo for those benchmarks.
Because we use Yahoo to download our benchmark data, and there is an issue with the API, we cannot make those requests successfully. In regards to providing a more robust solution, at this very moment I don't have any ideas yet. Will just have to wait on the API issue to get resolved for now.
Where we call our get_benchmark_returns function is in zipline.data.loader.ensure_benchmark_data:
"""
Ensure we have benchmark data for `symbol` from `first_date` to `last_date`
Parameters
----------
symbol : str
The symbol for the benchmark to load.
first_date : pd.Timestamp
First required date for the cache.
last_date : pd.Timestamp
Last required date for the cache.
now : pd.Timestamp
The current time. This is used to prevent repeated attempts to
re-download data that isn't available due to scheduling quirks or other
failures.
trading_day : pd.CustomBusinessDay
A trading day delta. Used to find the day before first_date so we can
get the close of the day prior to first_date.
We attempt to download data unless we already have data stored at the data
cache for `symbol` whose first entry is before or on `first_date` and whose
last entry is on or after `last_date`.
If we perform a download and the cache criteria are not satisfied, we wait
at least one hour before attempting a redownload. This is determined by
comparing the current time to the result of os.path.getmtime on the cache
path.
"""
Thank you, Freddie. I didn't include more dates in my back testing. Not sure why it requires download again. I wonder if the cache is saved somewhere on my disk so I can read it like a CSV.
https://forums.yahoo.net/t5/Yahoo-Finance-help/Is-Yahoo-Finance-API-broken/td-p/250503/page/3 it is officially gone. response from Yahoo finance team. Yahoo discontinued the free service.
then how can we use zipline? :(
Hi @ywang412. zipline is one of many friends affected by these changes, so while we're considering solutions, we'll also see what pandas-datareader et al decide to do. We're not sure on the timeframe, but discussion continues on what changes to make. If you have any opinions on the matter feel free to share them 😃
Here is a solution: Google finance through the Pandas datareader.
- no api key needed
- no shady scraping needed
What am I missing?
Calling this with "^GSPC" throws an exception, not sure of the symbol needed for the SPY500, SPY works.
Update 1.0: Due to licensing agreements Google cannot provide data for ^GSPC. SPY is not 100% ^GSPC because SPY pays dividends. Some will actually argue that SPY is a better benchmark as it is something you can actually trade.
@pbharrin : Could you please share your patch to replace yahoo loader with google one? Thanks!
Why not use https://www.quandl.com/product/WIKIP/documentation/documentation ?
Hey Peter @pbharrin thanks for your suggestion. I think we've mentioned this in our discussion at some point; there's some other things we'll need to talk about as well (e.g. like SPY vs ^GSPC).
@quocble If I remember correctly, I looked through that WIKI dataset and there are no tickers like SPY, ^GSPC or other ETFs tracking the S&P
Here is my patch: https://github.com/zipline-live/zipline/blob/ph_live/zipline/data/benchmarks.py It doesn't require any new libraries or api-keys. This uses the SPY data from Google Finance.
It is using the Close of SPY which does not include dividends. For some reason Google Finance does not return Adj. Close.
This is now failing 4 tests from tests.test_examples.ExamplesTests: test_example_4_olmar test_example_3_dual_ema_talib test_example_1_momentum_pipeline test_example_0_buyapple
This is because the benchmark is slightly different.
Thank you, pbharrin! Is https://github.com/zipline-live/ different from https://github.com/quantopian/zipline? One is maintained by the community and the other one by the institution? The push histories are quite different.
Yes https://github.com/zipline-live/ is a project we are working on to allow you to live-trade from Zipline. (Like Quantopian, but you can trade any broker you want, any security you want, provided someone writes the code to interact with that broker.) People have expressed interest in using Zipline-live for exchanges in other countries and even Bitcoin.
We forked from Zipline recently so the code base is very similar. If the guys at Quantopian want to use the fix I have proposed then I can make a formal pull request from a Zipline branch. Otherwise you can just overwrite the original benchmarks.py with my benchmarks.py in Zipline and it will work until they figure out a long-term solution.
pbarrin, thx for google SPY patch, I had to add
import pandas_datareader.data as web
got a benchmark warning, but the test Buy_AAPL script seemed to run ok
Downloading benchmark data for '^GSPC'. [2017-05-18 21:44:29.118015] WARNING: Loader: Still don't have expected data after redownload! [2017-05-18 21:44:50.681792] INFO: Performance: Simulated 334 trading days out of 334. [2017-05-18 21:44:50.681929] INFO: Performance: first open: 2016-01-04 14:31:00+00:00 [2017-05-18 21:44:50.682005] INFO: Performance: last close: 2017-05-01 20:00:00+00:00 AAPL algo_volatility algorithm_period_return \
My suspicion is that after Verizon acquired Yahoo (a SV based company), they're not so inclined to give the data for free. Google will likely provide their data for free.
@pbharrin : Thanks for the patch, Peter!