Zipline Example of Repeated Apple Purchases and Benchmarking
Summary
This Zipline example runs a daily algorithm over Apple data from 2014 through 2018. At each data point, it places an order for ten shares and records the current Apple price. The setup specifies per-share commissions with a minimum trade cost and volume-share slippage, then runs the simulation with a stated starting capital and Quandl data bundle.
The script also loads S&P 500 index values from FRED, converts them to daily percentage returns for use as benchmark returns, and plots portfolio value alongside the recorded Apple price. These elements illustrate how to connect asset orders, trading costs, benchmark data, and simple result visualization in a backtest. The document reports no resulting return, risk statistics, or comparison with the benchmark, so it demonstrates mechanics rather than evidence of strategy performance. Its fixed purchase rule does not explain cash management or how to handle constraints that might affect repeated orders.
Key ideas
- The algorithm places a fixed order for ten Apple shares at each daily data point.
- Commission and volume-based slippage are explicitly configured.
- S&P 500 daily returns are supplied as the simulation benchmark.
- Portfolio value and Apple price are plotted, but no performance conclusions are reported.
- The example demonstrates backtest wiring rather than a tested trading thesis.
Tags
Full text
# buyapple_ide.py
```py
#!/usr/bin/env python
#
# Copyright 2014 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from zipline.api import order, record, symbol
from zipline.finance import commission, slippage
from zipline import run_algorithm
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
def initialize(context):
context.asset = symbol("AAPL")
# Explicitly set the commission/slippage to the "old" value until we can
# rebuild example data.
# github.com/quantopian/zipline/blob/master/tests/resources/
# rebuild_example_data#L105
context.set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.0))
context.set_slippage(slippage.VolumeShareSlippage())
def handle_data(context, data):
order(context.asset, 10)
record(AAPL=data.current(context.asset, "price"))
# Note: this function can be removed if running
# this algorithm on quantopian.com
def analyze(context=None, results=None):
import matplotlib.pyplot as plt
# Plot the portfolio and asset data.
ax1 = plt.subplot(211)
results.portfolio_value.plot(ax=ax1)
ax1.set_ylabel("Portfolio value (USD)")
ax2 = plt.subplot(212, sharex=ax1)
results.AAPL.plot(ax=ax2)
ax2.set_ylabel("AAPL price (USD)")
# Show the plot.
plt.gcf().set_size_inches(18, 8)
plt.show()
start = pd.Timestamp("2014")
end = pd.Timestamp("2018")
sp500 = web.DataReader("SP500", "fred", start, end).SP500
benchmark_returns = sp500.pct_change()
print(benchmark_returns.head())
result = run_algorithm(
start=start,
end=end,
initialize=initialize,
handle_data=handle_data,
capital_base=100000,
benchmark_returns=benchmark_returns,
bundle="quandl",
data_frequency="daily",
)
print(result.info())
result.portfolio_value.plot()
plt.show()
```Shown in full with attribution under the source's licence. Licence: Apache-2.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.