[PYTHON] zipline Beginner Tutorial

1. This article is

I will explain the procedure to run the back test described in the Beginner Tutorial of zipline (link here).

2. Contents

2-1 Preparation

Please finish 2-1,2-2,2-3 of the linked article below.

2-1 [Install python3.5 on jupyter virtual environment. ](Https://qiita.com/NT1123/items/6ca9ee2d2c586371bcaf#2-1-python35%E3%82%92jupyter%E4%BB%AE%E6%83%B3%E7%92%B0%E5%A2% 83% E4% B8% 8A% E3% 81% AB% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC% E3% 83% AB% E3% 81% 99% E3% 82% 8B) 2-2 [Install zipline on the virtual environment. ](Https://qiita.com/NT1123/items/6ca9ee2d2c586371bcaf#2-1-python35%E3%82%92jupyter%E4%BB%AE%E6%83%B3%E7%92%B0%E5%A2% 83% E4% B8% 8A% E3% 81% AB% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC% E3% 83% AB% E3% 81% 99% E3% 82% 8B) 2-3 [Fixed benchmarks.py and loaders.py](https://qiita.com/NT1123/items/6ca9ee2d2c586371bcaf#2-1-python35%E3%82%92jupyter%E4%BB%AE%E6%83 % B3% E7% 92% B0% E5% A2% 83% E4% B8% 8A% E3% 81% AB% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 83% 88 % E3% 83% BC% E3% 83% AB% E3% 81% 99% E3% 82% 8B)

2-2 Acquisition of stock data

Get stock price data using the Quandle API. (If you have a free account, you will not be able to get data after 2018.) To use it, go to https://www.quandl.com/ and go to Please get the API key first.

(0) Obtain the API key from https://www.quandl.com/. The obtained key is "xxx123".

(1) Launch the anaconda3 prompt.

(2) Set the QUANDL API key. For windows, you need to set the API key with set *** as shown below.

python


(python355) C:\Users\***\anaconda3>set QUANDL_API_KEY=xxx123

(3) Obtain stock price data from QUANDL.

python


(python355) C:\Users\***\anaconda3>zipline ingest

(4) You can confirm that the data set "quandl" has been created.

python


(python355) C:\Users\fdfpy\anaconda3>zipline bundles
quandl 2020-06-23 11:59:39.478449

2-3 Run Beginner Tutorial (Run on Console)

When running in the console, run C: /Users/fdfpy/anaconda3/envs/python355/Lib/site-packages/zipline/examples/buyapple.py.

(1) Launch the anaconda3 prompt.

(2) Go to C: / Users / fdfpy / anaconda3 / envs / python355 / Lib / site-packages / zipline / examples /.

(3) Execute buyapple.py and perform backtesting.

python


(python355) C:\Users\fdfpy\anaconda3\envs\python355\Lib\site-packages\zipline\examples>zipline run -f buyapple.py --start 2016-1-1 --end 2018-1-1 -o buyapple_out.pickle

(4) The execution result is displayed. 85.JPG

2-4 Run Beginner Tutorial (Run on Jupyter)

Execute the following code on jupyter.

python


%load_ext zipline

python


from zipline.api import symbol, order, record

def initialize(context):
    pass

def handle_data(context, data):
    order(symbol('AAPL'), 10)
    record(AAPL=data[symbol('AAPL')].price)

python


%zipline --bundle quandl --start 2016-1-1 --end 2017-1-1 -o start.pickle

Below is the output result. The result of the back test is output. 86.JPG

Recommended Posts

zipline Beginner Tutorial
sqlalchemy tutorial
PyODE Tutorial 2
Python tutorial
PyODE Tutorial 1
PyODE Tutorial 3
TensorFlow tutorial tutorial