quantopian / zipline

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

Markowitz-blog notebook doesn't work with latest zipline #1621

Closed QuentinLew closed 7 years ago

QuentinLew commented 7 years ago

Environment

Description of Issue

https://github.com/quantopian/research_public/blob/master/research/Markowitz-blog.ipynb

from zipline.utils.factory import load_bars_from_yahoo
end = pd.Timestamp.utcnow()
start = end - 2500 * pd.tseries.offsets.BDay()

data = load_bars_from_yahoo(stocks=['IBM', 'GLD', 'XOM', 'AAPL', 
                                    'MSFT', 'TLT', 'SHY'],
                            start=start, end=end)

import zipline
from zipline.api import (add_history, 
                         history, 
                         set_slippage, 
                         slippage,
                         set_commission, 
                         commission, 
                         order_target_percent)

from zipline import TradingAlgorithm

def initialize(context):
    '''
    Called once at the very beginning of a backtest (and live trading). 
    Use this method to set up any bookkeeping variables.

    The context object is passed to all the other methods in your algorithm.

    Parameters

    context: An initialized and empty Python dictionary that has been 
             augmented so that properties can be accessed using dot 
             notation as well as the traditional bracket notation.

    Returns None
    '''
    # Register history container to keep a window of the last 100 prices.
     add_history(100, '1d', 'price')
    # Turn off the slippage model
    set_slippage(slippage.FixedSlippage(spread=0.0))
    # Set the commission model (Interactive Brokers Commission)
    set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.0))
    context.tick = 0

def handle_data(context, data):
    '''
    Called when a market event occurs for any of the algorithm's 
    securities. 

    Parameters

    data: A dictionary keyed by security id containing the current 
          state of the securities in the algo's universe.

    context: The same context object from the initialize function.
             Stores the up to date portfolio as well as any state 
             variables defined.

    Returns None
    '''
    # Allow history to accumulate 100 days of prices before trading
    # and rebalance every day thereafter.
    context.tick += 1
    if context.tick < 100:
        return
    # Get rolling window of past prices and compute returns
    prices = history(100, '1d', 'price').dropna()
    returns = prices.pct_change().dropna()
    try:
        # Perform Markowitz-style portfolio optimization
        weights, _, _ = optimal_portfolio(returns.T)
        # Rebalance portfolio accordingly
        for stock, weight in zip(prices.columns, weights):
            order_target_percent(stock, weight)
    except ValueError as e:
        # Sometimes this error is thrown
        # ValueError: Rank(A) < p or Rank([P; A; G]) < n
        pass

# Instantinate algorithm        
algo = TradingAlgorithm(initialize=initialize, 
                        handle_data=handle_data)
# Run algorithm
results = algo.run(data)
results.portfolio_value.plot()
# algo.current_sids()
  1. "cannot import name add_history" #1286 So I removed add_history.
  2. TradingAlgorithm got KeyError
    
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-41-221039edce67> in <module>()
     72                         handle_data=handle_data)
     73 # Run algorithm
    ---> 74 results = algo.run(data)
     75 results.portfolio_value.plot()
     76 # algo.current_sids()

C:\Python27\lib\site-packages\zipline\algorithm.pyc in run(self, data, overwrite_sim_params) 686 try: 687 perfs = [] --> 688 for perf in self.get_generator(): 689 perfs.append(perf) 690

C:\Python27\lib\site-packages\zipline\gens\tradesimulation.pyc in transform(self) 218 for dt, action in self.clock: 219 if action == BAR: --> 220 for capital_change_packet in every_bar(dt): 221 yield capital_change_packet 222 elif action == SESSION_START:

C:\Python27\lib\site-packages\zipline\gens\tradesimulation.pyc in every_bar(dt_to_use, current_data, handle_data) 131 perf_tracker.process_commission(commission) 132 --> 133 handle_data(algo, current_data, dt_to_use) 134 135 # grab any new orders from the blotter, then clear the list.

C:\Python27\lib\site-packages\zipline\utils\events.pyc in handle_data(self, context, data, dt) 182 context, 183 data, --> 184 dt, 185 ) 186

C:\Python27\lib\site-packages\zipline\utils\events.pyc in handle_data(self, context, data, dt) 201 """ 202 if self.rule.should_trigger(dt): --> 203 self.callback(context, data) 204 205

C:\Python27\lib\site-packages\zipline\algorithm.pyc in handle_data(self, data) 457 def handle_data(self, data): 458 if self._handle_data: --> 459 self._handle_data(self, data) 460 461 # Unlike trading controls which remain constant unless placing an

in handle_data(context, data) 55 return 56 # Get rolling window of past prices and compute returns ---> 57 prices = history(100, '1d', 'price').dropna() 58 returns = prices.pct_change().dropna() 59 try: C:\Python27\lib\site-packages\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) C:\Python27\lib\site-packages\zipline\utils\api_support.pyc in wrapped_method(self, *args, **kwargs) 96 if not self.initialized: 97 raise exception ---> 98 return method(self, *args, **kwargs) 99 return wrapped_method 100 return decorator C:\Python27\lib\site-packages\zipline\algorithm.pyc in history(self, bar_count, frequency, field, ffill) 2063 self._calculate_universe(), 2064 field, -> 2065 ffill 2066 ) 2067 C:\Python27\lib\site-packages\zipline\algorithm.pyc in get_history_window(self, bar_count, frequency, assets, field, ffill) 2074 frequency, 2075 field, -> 2076 ffill, 2077 ) 2078 else: C:\Python27\lib\site-packages\zipline\data\data_portal.pyc in get_history_window(self, assets, end_dt, bar_count, frequency, field, ffill) 772 if field == "price": 773 df = self._get_history_daily_window(assets, end_dt, bar_count, --> 774 "close") 775 else: 776 df = self._get_history_daily_window(assets, end_dt, bar_count, C:\Python27\lib\site-packages\zipline\data\data_portal.pyc in _get_history_daily_window(self, assets, end_dt, bar_count, field_to_use) 644 645 data = self._get_history_daily_window_data( --> 646 assets, days_for_window, end_dt, field_to_use 647 ) 648 return pd.DataFrame( C:\Python27\lib\site-packages\zipline\data\data_portal.pyc in _get_history_daily_window_data(self, assets, days_for_window, end_dt, field_to_use) 686 elif field_to_use == 'close': 687 minute_value = self._daily_aggregator.closes( --> 688 assets, end_dt) 689 elif field_to_use == 'volume': 690 minute_value = self._daily_aggregator.volumes( C:\Python27\lib\site-packages\zipline\data\resample.pyc in closes(self, assets, dt) 383 except KeyError: 384 val = self._minute_reader.get_value( --> 385 asset, dt, 'close') 386 if pd.isnull(val): 387 val = self.closes([asset], C:\Python27\lib\site-packages\zipline\data\dispatch_bar_reader.pyc in get_value(self, sid, dt, field) 81 def get_value(self, sid, dt, field): 82 asset = self._asset_finder.retrieve_asset(sid) ---> 83 r = self._readers[type(asset)] 84 return r.get_value(sid, dt, field) 85 KeyError: ``` #959 So I tried `algo.current_sids()` 3. ``` --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 74 # results = algo.run(data) 75 # results.portfolio_value.plot() ---> 76 algo.current_sids() AttributeError: 'TradingAlgorithm' object has no attribute 'current_sids' ``` ## Reproduction Steps Every time I run https://github.com/quantopian/research_public/blob/master/research/Markowitz-blog.ipynb
QuentinLew commented 7 years ago

How can I fix this? I tried install 0.8.0 from source code but failed too.

freddiev4 commented 7 years ago

Hi @QuentinLewes this notebook is currently not using the latest version of zipline, as many API changes have occurred since then. If you'd like to open a PR to that repository to fix that notebook, feel free to do that.

You can look at the q2 migration guide here to see what changes need to be made, and also look at the latest zipline code

freddiev4 commented 7 years ago

x-ref https://github.com/quantopian/research_public/issues/100

freddiev4 commented 7 years ago

Hey @QuentinLewes I'm going to go ahead and close this as it's being handled in https://github.com/quantopian/research_public/pull/134/ 😃