0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

zipline Beginner Tutorial

Posted at

1.この記事は

ziplineのBeginner Tutorial(リンクはこちら)に載っているバックテストを動かすまでの手順を説明します。

2.内容

2-1 準備

下記リンク記事の2-1,2-2,2-3を終わらせてください。

2-1 python3.5をjupyter仮想環境上にインストールする。
2-2 仮想環境上にziplineをインストールする。
2-3 benchmarks.pyとloaders.pyの修正

2-2 銘柄データの取得

Quandle APIを使って株価データを取得します。(無料アカウントの場合2018年以降のデータは取得できないようです。) 使用するには、https://www.quandl.com/にアクセスし、APIキーの取得をまず行ってください。

(0)https://www.quandl.com/よりAPIキーを入手する。
入手したキーを"xxx123"とします。

(1)anaconda3プロンプトを立ち上げます。

(2)QUANDLのAPIキーをセットします。
windowsの場合は下記のとおりset ***でAPIキーをセットする必要があります。

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

(3)QUANDLから株価データを入手します。

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

(4)データセット「quandl」が作成されていることが確認できます。

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

2-3 Beginner Tutorialの実行(コンソールで実行)

コンソールで実行する場合、C:/Users/fdfpy/anaconda3/envs/python355/Lib/site-packages/zipline/examples/buyapple.py を実行します。

(1)anaconda3プロンプトを立ち上げます。

(2)C:/Users/fdfpy/anaconda3/envs/python355/Lib/site-packages/zipline/examples/に移動します。

(3)buyapple.pyを実行し、バックテストを行います。

(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)実行結果が表示されます。
85.JPG

2-4 Beginner Tutorialの実行(Jupyter上で実行)

jupyter上で下記コードを実行してください。

%load_ext zipline
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)
%zipline --bundle quandl --start 2016-1-1 --end 2017-1-1 -o start.pickle

下記が出力結果です。バックテストの結果が出力されます。
86.JPG

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?