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()
"cannot import name add_history" 1286 So I removed add_history.
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.
Operating System: Windows 8.1 64-bit Python Version: 2.7.12 zipline Version: 1.0.2
https://github.com/quantopian/research_public/blob/master/research/Markowitz-blog.ipynb
TradingAlgorithm
gotKeyError
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