DOC: Reference example in quickstart to keep it up-to-date.

Fix the quickstart docs by including the example so that it's
always up-to-date.
This commit is contained in:
Ben McCann
2013-06-07 10:46:39 -07:00
committed by Eddie Hebert
parent eb027833b3
commit eaa9880203
+3 -44
View File
@@ -8,48 +8,7 @@ Dual-Moving Average Example
The following code implements a simple dual moving average algorithm
and tests it on data extracted from yahoo finance.
.. code-block:: python
from zipline.algorithm import TradingAlgorithm
from zipline.transforms import MovingAverage
from zipline.utils.factory import load_from_yahoo
class DualMovingAverage(TradingAlgorithm):
"""Dual Moving Average algorithm.
"""
def initialize(self, short_window=200, long_window=400):
# Add 2 mavg transforms, one with a long window, one
# with a short window.
self.add_transform(MovingAverage, 'short_mavg', ['price'],
market_aware=True,
days=short_window)
self.add_transform(MovingAverage, 'long_mavg', ['price'],
market_aware=True,
days=long_window)
# To keep track of whether we invested in the stock or not
self.invested = False
self.short_mavg = []
self.long_mavg = []
def handle_data(self, data):
if (data['AAPL'].short_mavg['price'] > data['AAPL'].long_mavg['price']) and not self.invested:
self.order('AAPL', 100)
self.invested = True
elif (data['AAPL'].short_mavg['price'] < data['AAPL'].long_mavg['price']) and self.invested:
self.order('AAPL', -100)
self.invested = False
# Save mavgs for later analysis.
self.short_mavg.append(data['AAPL'].short_mavg['price'])
self.long_mavg.append(data['AAPL'].long_mavg['price'])
data = load_from_yahoo()
dma = DualMovingAverage()
results = dma.run(data)
You can find other examples in the zipline/examples directory.
.. include:: ../zipline/examples/dual_moving_average.py
:literal:
You can find other examples in `the zipline/examples directory <https://github.com/quantopian/zipline/tree/master/zipline/examples>`_.