Zipline学习笔记

Update on 201181227 re-install the zipline based on Unbuntu 18.04

Steps:

Install:

sudo pip uninstall zipline
sudo apt install python3-pip
sudo pip3 install zipline

QUANDL_API_KEY=[QUANDL_KEY] zipline ingest -b quandl

Run:

zipline run --bundle quantopian-quandl -f buyapple.py -s 2016-01-01 -e 2018-12-01 -o apple.pickle

Plot with Pandas:

with python3

>>> import pandas as pd
>>> perf = pd.read_pickle('apple.pickle')
>>> perf.head()

Plot with Matplot

sudo pip3 install matplotlib
zipline run -f mavggoogle.py -s 2016-01-01 -e 2018-12-01

plot function:

def analyze(context, perf):
    ax1 = plt.subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value in $')
    ax1.set_xlabel('time in years')

    ax2 = plt.subplot(212, sharex=ax1)

    perf['GOOG'].plot(ax=ax2)
    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)

    perf_trans = perf.ix[[t != [] for t in perf.transactions]]
    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]
    sells = perf_trans.ix[[t[0]['amount'] < 0 for t in perf_trans.transactions]]
    ax2.plot(buys.index, perf.short_mavg.ix[buys.index], '^', markersize=10, color='m')
    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],'v', markersize=10, color='k')
    ax2.set_ylabel('price in $')
    ax2.set_xlabel('time in years')
    plt.legend(loc=0)
    plt.show() 

Plot with pyfolio:

pyfolio

sudo pip3 install pyfolio
import pyfolio as pf
import pandas as pd

results = pd.read_pickle('apple.pickle')
returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)
pf.plot_drawdown_periods(returns, top=5).set_xlabel('Date')

pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions, live_start_date='2018-01-01', round_trips=True)

Python3 没有兼容问题。只有Python2才有下面的问题:

目前遇到一个问题就是zipline和pyfolio使用的pandas版本不兼容。需要在不同的环境下运行才可以。这里也有记录

但是有一个图形问题: https://github.com/quantopian/pyfolio/issues/485

Comments