zipline安装
官方说版本用3.5还强烈建议用conda,我尝试window实在太累了。
创建环境:
conda create -n env_zipline python=3.5
使用环境:
conda activate env_zipline
退出则是:
conda deactivate
安装:
conda install -c Quantopian zipline
安装这个模块,会附带安装几十个依赖模块,我这笔记本疯狂转…
查看:
conda env list
安装ipykernel:
conda install ipykernel
将选择的conda环境注入Jupyter Notebook
python -m ipykernel install --user --name env_zipline --display-name "conda env_zipline"
打开notebook
jupyter notebook
进去就是环境里面了
quandl安装
进入网站:
/account/profile
创建账号,到用户设置查看key:
执行:
set QUANDL_API_KEY=qLkRnYQDoj-7yJUjgoD7
再执行:
zipline ingest -b quandl
安装又遇到了这个错误:
ValueError: Boolean array expected for the condition, not float64
解决办法pandas降到0.18.1:
conda install pandas==0.18.1
再来执行:
zipline ingest -b quandl
成功!!
测试
测试代码test.py:
# coding=gbk"""作者:川川公众号:玩转大数据@时间 : /2/21 5:35"""from zipline.api import order, record, symbolfrom zipline.finance import commission, slippagedef initialize(context):context.asset = symbol('AAPL')# Explicitly set the commission/slippage to the "old" value until we can# rebuild example data.# /quantopian/zipline/blob/master/tests/resources/# rebuild_example_data#L105context.set_commission(commission.PerShare(cost=.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 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()def _test_args():"""Extra arguments to use when zipline's automated tests run this example."""import pandas as pdreturn {'start': pd.Timestamp('-01-01', tz='utc'),'end': pd.Timestamp('-11-01', tz='utc'),}
执行:
zipline run -f test.py --start 2000-1-1 --end -1-1 -o buyapple_out.pickle
然而又遇到报错
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
,
解决:
https://polygon.io/dashboard/
得到你的密钥:
到data目录下:benchmarks.py 中更改 API 请求
修改如下:
…md不改了,睡觉,请参考:
/quantopian/zipline/issues/2480