Getting an error msg when trying to run zipline in notebook, adding the add_history method. Looks like som error regarding the datetime?
Any one know how to get by this?
Cheers
My code:
from zipline.api import (
add_history,
history,
order_target,
record,
symbol,
order,
)
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
import pandas as pd
import numpy as np
import pytz
from datetime import datetime
from zipline.algorithm import TradingAlgorithm
from zipline.utils.factory import load_bars_from_yahoo
# 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)
data = load_bars_from_yahoo(stocks=['SPY'], start=start,
end=end)
# Define algorithm
def initialize(context):
add_history(11, '1d', 'price')
context.i = 0
def handle_data(context, data):
# Skip first periode to get the full window
context.i += 1
if context.i < 11:
return
order(symbol('SPY'), 10)
price_history = history(11, '1d', 'price')
# Save values for later inspection
record(SPY=data[symbol('SPY')].price)
# Create algorithm object passing in initialize and
# handle_data functions
algo_obj = TradingAlgorithm(initialize=initialize,
handle_data=handle_data)
# Run algorithm
perf_manual = algo_obj.run(data)
ValueError Traceback (most recent call last)
<ipython-input-31-6e48b596206f> in <module>()
5
6 # Run algorithm
----> 7 perf_manual = algo_obj.run(data)
C:\Users\Eier\Anaconda\lib\site-packages\zipline\algorithm.pyc in run(self, source, overwrite_sim_params, benchmark_return_source)
396 self.history_specs,
397 self.sim_params.sids,
--> 398 self.sim_params.first_open)
399
400 # Create transforms by wrapping them into StatefulTransforms
C:\Users\Eier\Anaconda\lib\site-packages\zipline\history\history_container.pyc in __init__(self, history_specs, initial_sids, initial_dt)
169 # cached since mid-stream creation of containing data frames on every
170 # bar is expensive.
--> 171 self.create_return_frames(initial_dt)
172
173 # Helps prop up the prior day panel against having a nan, when the data
C:\Users\Eier\Anaconda\lib\site-packages\zipline\history\history_container.pyc in create_return_frames(self, algo_dt)
262 self.return_frames = {}
263 for spec_key, history_spec in iteritems(self.history_specs):
--> 264 index = pd.to_datetime(index_at_dt(history_spec, algo_dt))
265 frame = pd.DataFrame(
266 index=index,
C:\Users\Eier\Anaconda\lib\site-packages\pandas\util\decorators.pyc in wrapper(*args, **kwargs)
87 else:
88 kwargs[new_arg_name] = new_arg_value
---> 89 return func(*args, **kwargs)
90 return wrapper
91 return _deprecate_kwarg
C:\Users\Eier\Anaconda\lib\site-packages\pandas\tseries\tools.pyc in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, coerce, unit, infer_datetime_format)
274 return _to_datetime(arg, errors=errors, dayfirst=dayfirst, yearfirst=yearfirst,
275 utc=utc, box=box, format=format, exact=exact,
--> 276 unit=unit, infer_datetime_format=infer_datetime_format)
277
278
C:\Users\Eier\Anaconda\lib\site-packages\pandas\tseries\tools.pyc in _to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, freq, infer_datetime_format)
393 return _convert_listlike(arg, box, format, name=arg.name)
394 elif com.is_list_like(arg):
--> 395 return _convert_listlike(arg, box, format)
396
397 return _convert_listlike(np.array([ arg ]), box, format)[0]
C:\Users\Eier\Anaconda\lib\site-packages\pandas\tseries\tools.pyc in _convert_listlike(arg, box, format, name)
381 return DatetimeIndex._simple_new(values, name=name, tz=tz)
382 except (ValueError, TypeError):
--> 383 raise e
384
385 if arg is None:
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True