quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.52k stars 4.71k forks source link

Extremely basic test fails on some days and times and not others #558

Closed doctorpangloss closed 9 years ago

doctorpangloss commented 9 years ago

I use a simple test for my environment integrity. On Sunday this worked. Today it does not. Nothing has changed.

pip freeze

Logbook==0.9.0
numpy==1.9.2
pandas==0.16.0
pycosat==0.6.0
python-dateutil==2.4.2
pytz==2015.2
PyYAML==3.11
requests==2.6.0
scikit-learn==0.16.0
scipy==0.15.1
six==1.9.0
zipline==0.7.0

Test:

import unittest

import pytz
from datetime import datetime

from zipline.api import order, record, symbol
from zipline.algorithm import TradingAlgorithm
from zipline.utils.factory import load_bars_from_yahoo

from sklearn import datasets, svm

class Basics(unittest.TestCase):
    # Load data manually from Yahoo! finance
    start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc)
    end = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc)

    # Define algorithm
    @staticmethod
    def initialize(context):
        pass

    @staticmethod
    def handle_data(context, data):
        order(symbol('AAPL'), 10)
        record(AAPL=data[symbol('AAPL')].price)

    def test_execute_quick_start(self):
        # From http://www.zipline.io/#quickstart
        data = load_bars_from_yahoo(stocks=['AAPL'], start=Basics.start, end=Basics.end)
        algorithm = TradingAlgorithm(initialize=Basics.initialize, handle_data=Basics.handle_data)
        algorithm.run(data)
        self.assertAlmostEqual(algorithm.portfolio.portfolio_value, 113733.70)

    def test_sklearn_quick_start(self):
        # From http://scikit-learn.org/0.10/tutorial.html
        digits = datasets.load_digits()
        clf = svm.SVC(gamma=0.001)
        clf.fit(digits.data[:-1], digits.target[:-1])
        self.assertEquals(clf.predict(digits.data[-1]), 8)
/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --multiproc --client 127.0.0.1 --port 49171 --file /Applications/PyCharm.app/Contents/helpers/pycharm/utrunner.py /Users/bberman/AdStart/tests/test_basics.py true
Testing started at 3:04 PM ...
Connected to pydev debugger (build 139.1659)
pydev debugger: process 98083 is connecting

AAPL

Process finished with exit code 0

Error
Traceback (most recent call last):
  File "/Users/bberman/AdStart/tests/test_basics.py", line 33, in test_execute_quick_start
    algorithm = TradingAlgorithm(initialize=Basics.initialize, handle_data=Basics.handle_data)
  File "/Library/Python/2.7/site-packages/zipline/algorithm.py", line 158, in __init__
    capital_base=self.capital_base
  File "/Library/Python/2.7/site-packages/zipline/utils/factory.py", line 65, in create_simulation_parameters
    emission_rate=emission_rate,
  File "/Library/Python/2.7/site-packages/zipline/finance/trading.py", line 365, in __init__
    self._update_internal()
  File "/Library/Python/2.7/site-packages/zipline/finance/trading.py", line 369, in _update_internal
    environment = TradingEnvironment.instance()
  File "/Library/Python/2.7/site-packages/zipline/finance/trading.py", line 83, in instance
    environment = TradingEnvironment()
  File "/Library/Python/2.7/site-packages/zipline/finance/trading.py", line 101, in __init__
    load(self.bm_symbol)
  File "/Library/Python/2.7/site-packages/zipline/data/loader.py", line 207, in load_market_data
    saved_curves = pd.DataFrame.from_csv(tr_filepath)
  File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1035, in from_csv
    infer_datetime_format=infer_datetime_format)
  File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 470, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 246, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 562, in __init__
    self._make_engine(self.engine)
  File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 699, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1066, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "pandas/parser.pyx", line 512, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4804)
ValueError: No columns to parse from file
ssanderson commented 9 years ago

@doctorpangloss the failure there is zipline trying to load a cached csv file of treasury rate information. I would look at the tr_filepath value that's being loaded on 207 of zipline/data/loader.py and verify that that file exists and looks like a CSV representing treasury rates. I'd expect the first few lines to look something like:

,10year,1month,1year,20year,2year,30year,3month,3year,5year,6month,7year,date,tid
1990-01-02 00:00:00+00:00,0.0794,,0.0781,,0.0787,0.08,0.0783,0.079,0.0787,0.0789,0.0798,1990-01-02 00:00:00+00:00,1753
1990-01-03 00:00:00+00:00,0.0799,,0.0785,,0.0794,0.0804,0.0789,0.0796,0.0792,0.0794,0.0804,1990-01-03 00:00:00+00:00,1752
1990-01-04 00:00:00+00:00,0.0798,,0.0782,,0.0792,0.0804,0.0784,0.0793,0.0791,0.079,0.0802,1990-01-04 00:00:00+00:00,1754

If that's not the case, I'm not sure what might have messed up that file, but I believe it should re-download if deleted.

doctorpangloss commented 9 years ago

Thanks so much, I'm investigating this now and I'll post a PR if I can have zipline resolve these sorts of issues automatically.

doctorpangloss commented 9 years ago

https://github.com/quantopian/zipline/pull/559

I can also try to write a test for this issue.