mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-12 23:27:30 +08:00
DOC: Add development guide .rst
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Zipline beginner tutorial
|
||||
Zipline Beginner Tutorial
|
||||
-------------------------
|
||||
|
||||
Basics
|
||||
@@ -624,7 +624,7 @@ the stock to go down further.
|
||||
As we need to have access to previous prices to implement this strategy
|
||||
we need a new concept: History
|
||||
|
||||
``history()`` is a convenience function that keeps a rolling window of
|
||||
``data.history()`` is a convenience function that keeps a rolling window of
|
||||
data for you. The first argument is the number of bars you want to
|
||||
collect, the second argument is the unit (either ``'1d'`` for ``'1m'``
|
||||
but note that you need to have minute-level data for using ``1m``). For
|
||||
@@ -634,14 +634,14 @@ Let's look at the strategy which should make this clear:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
%%zipline --start 2000-1-1 --end 2014-1-1 -o perf_dma
|
||||
%%zipline --start 2000-1-1 --end 2012-1-1 -o dma.pickle
|
||||
|
||||
|
||||
from zipline.api import order_target, record, symbol, history
|
||||
import numpy as np
|
||||
from zipline.api import order_target, record, symbol
|
||||
|
||||
def initialize(context):
|
||||
context.i = 0
|
||||
context.asset = symbol('AAPL')
|
||||
|
||||
|
||||
def handle_data(context, data):
|
||||
@@ -651,23 +651,23 @@ Let's look at the strategy which should make this clear:
|
||||
return
|
||||
|
||||
# Compute averages
|
||||
# history() has to be called with the same params
|
||||
# data.history() has to be called with the same params
|
||||
# from above and returns a pandas dataframe.
|
||||
short_mavg = history(100, '1d', 'price').mean()
|
||||
long_mavg = history(300, '1d', 'price').mean()
|
||||
short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
|
||||
long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()
|
||||
|
||||
# Trading logic
|
||||
if short_mavg[0] > long_mavg[0]:
|
||||
if short_mavg > long_mavg:
|
||||
# order_target orders as many shares as needed to
|
||||
# achieve the desired number of shares.
|
||||
order_target(symbol('AAPL'), 100)
|
||||
elif short_mavg[0] < long_mavg[0]:
|
||||
order_target(symbol('AAPL'), 0)
|
||||
order_target(context.asset, 100)
|
||||
elif short_mavg < long_mavg:
|
||||
order_target(context.asset, 0)
|
||||
|
||||
# Save values for later inspection
|
||||
record(AAPL=data[symbol('AAPL')].price,
|
||||
short_mavg=short_mavg[0],
|
||||
long_mavg=long_mavg[0])
|
||||
record(AAPL=data.current(context.asset, 'price'),
|
||||
short_mavg=short_mavg,
|
||||
long_mavg=long_mavg)
|
||||
|
||||
|
||||
def analyze(context, perf):
|
||||
|
||||
Reference in New Issue
Block a user