Unable to 'order' after upgrading to zipline 0.8.0rc1

57 views
Skip to first unread message

Dave Gilbert

unread,
Sep 29, 2015, 4:23:55 AM9/29/15
to Zipline Python Opensource Backtester
I decided to give zipline 0.8.0rc1 a try. Eventually managed to install using 32 bit Anaconda and python 2.7 on windows 7.

But running the following doesn't seem to work...

from zipline import TradingAlgorithm
from zipline.utils.factory import load_bars_from_yahoo
from zipline.api import symbols, order, sid

data = load_bars_from_yahoo(
    stocks=['SPY'],
    start=dt.datetime(2015,6,1), 
    end=dt.datetime(2015,6,15)
)

def initialize(context):
    context.stocks = symbols('SPY')
    print (context.stocks)
    context.count = 0
def handle_data(context,data):
    if context.count == 0:
        order_target_percent (context.stocks[0], 1)
    context.count = 1
    print (context.portfolio.positions)
    pass

# 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)


OUTPUT:


SPY [Equity(0, symbol='SPY', asset_name='', exchange='', start_date=None, end_date=None, first_traded=None)]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-29-549744c676a3> in <module>()
     26 
     27 # Run algorithm
---> 28 perf_manual = algo_obj.run(data)

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\algorithm.pyc in run(self, source, overwrite_sim_params, benchmark_return_source)
    515             # perf dictionary
    516             perfs = []
--> 517             for perf in self.gen:
    518                 perfs.append(perf)
    519 

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\gens\tradesimulation.pyc in transform(self, stream_in)
    110                         date,
    111                         snapshot,
--> 112                         self.algo.instant_fill,
    113                     )
    114                     # Perf messages are only emitted if the snapshot contained

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\gens\tradesimulation.pyc in _process_snapshot(self, dt, snapshot, instant_fill)
    304 
    305         if any_trade_occurred:
--> 306             new_orders = self._call_handle_data()
    307             for order in new_orders:
    308                 perf_process_order(order)

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\gens\tradesimulation.pyc in _call_handle_data(self)
    333             self.algo,
    334             self.current_data,
--> 335             self.simulation_dt,
    336         )
    337         orders = self.algo.blotter.new_orders

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\utils\events.pyc in handle_data(self, context, data, dt)
    192     def handle_data(self, context, data, dt):
    193         for event in self._events:
--> 194             event.handle_data(context, data, dt)
    195 
    196 

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\utils\events.pyc in handle_data(self, context, data, dt)
    210         """
    211         if self.rule.should_trigger(dt):
--> 212             self.callback(context, data)
    213 
    214 

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\algorithm.pyc in handle_data(self, data)
    303             self.history_container.update(data, self.datetime)
    304 
--> 305         self._handle_data(self, data)
    306 
    307         # Unlike trading controls which remain constant unless placing an

<ipython-input-29-549744c676a3> in handle_data(context, data)
     15 def handle_data(context,data):
     16     if context.count == 0:
---> 17         order_target_percent (context.stocks[0], 1)
     18     context.count = 1
     19     print (context.portfolio.positions)

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\utils\api_support.pyc in wrapped(*args, **kwargs)
     49     def wrapped(*args, **kwargs):
     50         # Get the instance and call the method
---> 51         return getattr(get_algo_instance(), f.__name__)(*args, **kwargs)
     52     # Add functor to zipline.api
     53     setattr(zipline.api, f.__name__, wrapped)

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\algorithm.pyc in order_target_percent(self, sid, target, limit_price, stop_price, style)
   1002                                        limit_price=limit_price,
   1003                                        stop_price=stop_price,
-> 1004                                        style=style)
   1005 
   1006     @api_method

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\algorithm.pyc in order_target_value(self, sid, target, limit_price, stop_price, style)
    980         is actually the target exposure, as Futures have no 'value'.
    981         """
--> 982         target_amount = self._calculate_order_value_amount(sid, target)
    983         return self.order_target(sid, target_amount,
    984                                  limit_price=limit_price,

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\algorithm.pyc in _calculate_order_value_amount(self, asset, value)
    679         asset being ordered.
    680         """
--> 681         last_price = self.trading_client.current_data[asset].price
    682 
    683         if tolerant_equals(last_price, 0):

g:\Anaconda3-32bit\envs\zipline2.7\lib\site-packages\zipline-0.8.0rc1-py2.7-win32.egg\zipline\protocol.pyc in __getitem__(self, name)
    516 
    517     def __getitem__(self, name):
--> 518         return self._data[name]
    519 
    520     def __delitem__(self, name):

KeyError: Equity(0, symbol='SPY', asset_name='', exchange='', start_date=None, end_date=None, first_traded=None)


My understanding is that the Zipline order methods now require Asset/Equity/Future objects to be passed as arguments, rather than simply an integer SID.

So now I'm confused..  Can someone help please?

 

Anthony Garner

unread,
Oct 5, 2015, 1:51:29 PM10/5/15
to Zipline Python Opensource Backtester
You may (or may not) find the following helpful: 

This forum on google groups seems to be 1. inactive and 2. useless
...
Reply all
Reply to author
Forward
0 new messages