# Enable plotting
%pylab --no-import-all inline
# Provides tabular data structure and much more!
import pandas as pd
import datetime
figsize(10,5)
Populating the interactive namespace from numpy and matplotlib
from zipline.data import load_from_yahoo
data = load_from_yahoo(stocks=['AAPL'],
start=datetime.datetime(2006, 1, 1))
AAPL
data.head()
AAPL | |
---|---|
Date | |
2006-01-03 00:00:00+00:00 | 72.28 |
2006-01-04 00:00:00+00:00 | 72.49 |
2006-01-05 00:00:00+00:00 | 71.92 |
2006-01-06 00:00:00+00:00 | 73.78 |
2006-01-09 00:00:00+00:00 | 73.53 |
5 rows × 1 columns
data['AAPL'].plot()
<matplotlib.axes.AxesSubplot at 0xf575910>
from zipline.algorithm import TradingAlgorithm
from zipline.api import order
def initialize(context):
print('Initialized!')
context.last_price = 0
def handle_data(context, data):
stock = 'AAPL'
if context.last_price < data[stock].price:
order(stock, 10)
else:
order(stock, -10)
context.last_price = data[stock].price
# Instantiate our algorithm
my_algo = TradingAlgorithm(initialize=initialize,
handle_data=handle_data)
# Run algorithm on data
perf_buy_apple = my_algo.run(data)
[2014-01-16 12:33] INFO: Performance: Simulated 2022 trading days out of 2022. [2014-01-16 12:33] INFO: Performance: first open: 2006-01-03 14:31:00+00:00 [2014-01-16 12:33] INFO: Performance: last close: 2014-01-14 21:00:00+00:00
Initialized!
perf_buy_apple.head()
capital_used | ending_cash | ending_value | orders | period_close | period_open | pnl | portfolio_value | positions | returns | starting_cash | starting_value | transactions | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2006-01-03 21:00:00 | 0.000000 | 100000.000000 | 0.0 | [{u'status': 0, u'created': 2006-01-03 00:00:0... | 2006-01-03 21:00:00+00:00 | 2006-01-03 14:31:00+00:00 | 0.000000 | 100000.000000 | [] | 0.000000 | 100000.000000 | 0.0 | [] |
2006-01-04 21:00:00 | -725.207249 | 99274.792751 | 724.9 | [{u'status': 1, u'created': 2006-01-03 00:00:0... | 2006-01-04 21:00:00+00:00 | 2006-01-04 14:31:00+00:00 | -0.307249 | 99999.692751 | [{u'amount': 10, u'last_sale_price': 72.49, u'... | -0.000003 | 100000.000000 | 0.0 | [{u'commission': 0.3, u'amount': 10, u'sid': u... |
2006-01-05 21:00:00 | -719.507192 | 98555.285559 | 1438.4 | [{u'status': 1, u'created': 2006-01-04 00:00:0... | 2006-01-05 21:00:00+00:00 | 2006-01-05 14:31:00+00:00 | -6.007192 | 99993.685559 | [{u'amount': 20, u'last_sale_price': 71.92, u'... | -0.000060 | 99274.792751 | 724.9 | [{u'commission': 0.3, u'amount': 10, u'sid': u... |
2006-01-06 21:00:00 | 737.492622 | 99292.778181 | 737.8 | [{u'status': 1, u'created': 2006-01-05 00:00:0... | 2006-01-06 21:00:00+00:00 | 2006-01-06 14:31:00+00:00 | 36.892622 | 100030.578181 | [{u'amount': 10, u'last_sale_price': 73.78, u'... | 0.000369 | 98555.285559 | 1438.4 | [{u'commission': 0.3, u'amount': -10, u'sid': ... |
2006-01-09 21:00:00 | -735.607353 | 98557.170828 | 1470.6 | [{u'status': 1, u'created': 2006-01-06 00:00:0... | 2006-01-09 21:00:00+00:00 | 2006-01-09 14:31:00+00:00 | -2.807353 | 100027.770828 | [{u'amount': 20, u'last_sale_price': 73.53, u'... | -0.000028 | 99292.778181 | 737.8 | [{u'commission': 0.3, u'amount': 10, u'sid': u... |
5 rows × 13 columns
perf_buy_apple.portfolio_value.plot()
<matplotlib.axes.AxesSubplot at 0x4109a50>
from zipline.transforms import MovingAverage
from zipline.api import order_target, record
def initialize_dma(context):
# Add 2 mavg transforms, one with a long window, one
# with a short window.
# Note that this is bound to change soon and will be easier.
context.add_transform(MovingAverage, 'short_mavg', ['price'],
window_length=100)
context.add_transform(MovingAverage, 'long_mavg', ['price'],
window_length=300)
def handle_data_dma(context, data):
short_mavg = data['AAPL'].short_mavg['price']
long_mavg = data['AAPL'].long_mavg['price']
if short_mavg > long_mavg:
order_target('AAPL', 100)
elif short_mavg < long_mavg:
order_target('AAPL', 0)
record(short_mavg=short_mavg,
long_mavg=long_mavg)
dma = TradingAlgorithm(initialize=initialize_dma,
handle_data=handle_data_dma)
perf_dma = dma.run(data)
[2014-01-15 22:02] INFO: Performance: Simulated 2022 trading days out of 2022. [2014-01-15 22:02] INFO: Performance: first open: 2006-01-03 14:31:00+00:00 [2014-01-15 22:02] INFO: Performance: last close: 2014-01-14 21:00:00+00:00
perf_dma.long_mavg
2006-01-03 21:00:00 72.280000 2006-01-04 21:00:00 72.385000 2006-01-05 21:00:00 72.230000 2006-01-06 21:00:00 72.617500 2006-01-09 21:00:00 72.800000 2006-01-10 21:00:00 73.696667 2006-01-11 21:00:00 74.757143 2006-01-12 21:00:00 75.600000 2006-01-13 21:00:00 76.395556 2006-01-17 21:00:00 76.947000 2006-01-18 21:00:00 77.202727 2006-01-19 21:00:00 77.137500 2006-01-20 21:00:00 76.863077 2006-01-23 21:00:00 76.737143 2006-01-24 21:00:00 76.522667 ... 2013-12-23 21:00:00 480.488300 2013-12-24 18:00:00 480.342233 2013-12-26 21:00:00 480.167267 2013-12-27 21:00:00 479.930967 2013-12-30 21:00:00 479.692867 2013-12-31 21:00:00 479.515167 2014-01-02 21:00:00 479.384967 2014-01-03 21:00:00 479.135967 2014-01-06 21:00:00 478.963700 2014-01-07 21:00:00 478.767233 2014-01-08 21:00:00 478.605767 2014-01-09 21:00:00 478.439100 2014-01-10 21:00:00 478.288600 2014-01-13 21:00:00 478.143433 2014-01-14 21:00:00 478.097700 Name: long_mavg, Length: 2022
fig = plt.figure()
ax1 = fig.add_subplot(211)
perf_dma.portfolio_value.plot(ax=ax1)
ax2 = fig.add_subplot(212)
data['AAPL'].plot(ax=ax2)
perf_dma[['short_mavg', 'long_mavg']].plot(ax=ax2)
perf_dma_trans = perf_dma.ix[[t != [] for t in perf_dma.transactions]]
buys = perf_dma_trans.ix[[t[0]['amount'] > 0 for t in perf_dma_trans.transactions]]
sells = perf_dma_trans.ix[[t[0]['amount'] < 0 for t in perf_dma_trans.transactions]]
ax2.plot(buys.index, perf_dma.short_mavg.ix[buys.index],
'^', markersize=10, color='m')
ax2.plot(sells.index, perf_dma.short_mavg.ix[sells.index],
'v', markersize=10, color='k')
plt.legend(loc=0)
<matplotlib.legend.Legend at 0xc3b4850>