Zipline backtest and pyfolio tear sheet with algo

This notebook shows how to run a futures backtest in Zipline using the QuantRocket client then analyze the backtests results inside the notebook using pyfolio.

The algo is a pairs trading algo using crude oil and gasoline futures. It is adapted from the Quantopian futures tutorial.

Step 1: Collect Historical Data (skip if you already have data)

First, let's collect historical data for the backtest. (Typically you might use the command line to perform these steps, but they can also be performed using the Python client, and we show them here to make the notebook more self-contained.)

The first step is to fetch the listings for CL and RB. This retrieves all the individual contracts from IB for the underlying symbols.

In [2]:
from quantrocket.master import collect_listings
collect_listings(exchanges="NYMEX", sec_types=["FUT"], symbols=["CL", "RB"])
{'status': 'the listing details will be fetched asynchronously'}

Monitor flightlog for the listings to be fetched, then make a universe:

In [3]:
from quantrocket.master import download_master_file, create_universe
import io

# Download the listings we just fetched
f = io.StringIO()
download_master_file(f, exchanges=["NYMEX"], sec_types=["FUT"], symbols=["CL", "RB"])
# then create a universe
create_universe("cl-rb", infilepath_or_buffer=f)
{'code': 'cl-rb', 'inserted': 155, 'provided': 155, 'total_after_insert': 155}

Now create a history database and fetch the data:

In [4]:
from quantrocket.history import create_db, collect_history
create_db("cl-rb-1d", universes=["cl-rb"], bar_size="1 day")
{'status': 'successfully created'}
In [5]:
{'status': 'the historical data will be fetched asynchronously'}

Monitor flightlog for the historical data to be fetched, then move on to the next step, ingesting the data into Zipline.

Step 2: Ingest Historical Data into Zipline

Next we ingest our history database into Zipline. The historical data for each individual contract is ingested, then Zipline will represent the contracts as continuous futures contracts at the time we run our backtest.

In [1]:
from quantrocket.zipline import ingest_bundle
ingest_bundle(history_db="cl-rb-1d", calendar="us_futures")
{'msg': 'successfully ingested cl-rb-1d bundle', 'status': 'success'}

Step 3: Install algo file

QuantRocket looks for Zipline strategies inside the "zipline" directory, so execute the following cell to "install" the strategy by moving the file to that location:

The ! sytax below lets us execute terminal commands from inside the notebook.

In [1]:
# make directory if doesn't exist
!mkdir -p /codeload/zipline

!mv /codeload/zipline/

Step 4: Run the Algo

Next, to run the backtest, we run the algo file.

QuantRocket returns the Zipline backtest results as a CSV. We write this CSV to a file.

(Note that the backtest runs remotely on the zipline service, not inside the jupyter service, i.e. not in the notebook environment itself. QuantRocket's jupyter service doesn't support running Zipline backtests via the %%zipline extension.)

In [2]:
from quantrocket.zipline import run_algorithm

Step 5: Create Tear Sheet

We can easily get a pyfolio tear sheet:

In [3]:
import pyfolio as pf
/opt/conda/lib/python3.6/site-packages/empyrical/ UserWarning: Unable to import pandas_datareader. Suppressing import error and continuing. All data reading functionality will raise errors; but has been deprecated and will be removed in a later version.
In [4]:
Start date2015-02-05
End date2017-06-30
Total months28
Annual return0.3%
Cumulative returns0.6%
Annual volatility7.4%
Sharpe ratio0.07
Calmar ratio0.03
Max drawdown-7.6%
Omega ratio1.02
Sortino ratio0.10
Tail ratio1.03
Daily value at risk-0.9%
Gross leverage0.00
Daily turnover33.5%
/opt/conda/lib/python3.6/site-packages/numpy/core/ FutureWarning: 'argmin' is deprecated, use 'idxmin' instead. The behavior of 'argmin'
will be corrected to return the positional minimum in the future.
Use 'series.values.argmin' to get the position of the minimum now.
  return getattr(obj, method)(*args, **kwds)
Worst drawdown periodsNet drawdown in %Peak dateValley dateRecovery dateDuration
Stress Eventsmeanminmax
New Normal0.00%-2.22%2.45%
Top 10 long positions of all timemax
Future(43635385 [CLZ5-201512])0.06%
Future(47207307 [CLM6-201606])0.05%
Future(55928744 [CLZ7-201712])0.05%
Future(47207310 [CLZ6-201612])0.05%
Future(123513564 [RBH6-201603])0.00%
Future(135678174 [RBV6-201610])0.00%
Future(121687393 [RBG6-201602])0.00%
Future(133925537 [RBU6-201609])0.00%
Future(119783831 [RBF6-201601])0.00%
Future(116588449 [RBX5-201511])0.00%
Top 10 short positions of all timemax
Future(47207307 [CLM6-201606])-0.05%
Future(47207310 [CLZ6-201612])-0.05%
Future(55928744 [CLZ7-201712])-0.05%
Future(43635385 [CLZ5-201512])-0.05%
Future(119783831 [RBF6-201601])-0.00%
Future(116588449 [RBX5-201511])-0.00%
Future(123513564 [RBH6-201603])-0.00%
Future(158931144 [RBN7-201707])-0.00%
Future(153402292 [RBM7-201706])-0.00%
Future(147587121 [RBJ7-201704])-0.00%
Top 10 positions of all timemax
Future(43635385 [CLZ5-201512])0.06%
Future(47207307 [CLM6-201606])0.05%
Future(47207310 [CLZ6-201612])0.05%
Future(55928744 [CLZ7-201712])0.05%
Future(123513564 [RBH6-201603])0.00%
Future(135678174 [RBV6-201610])0.00%
Future(121687393 [RBG6-201602])0.00%
Future(119783831 [RBF6-201601])0.00%
Future(133925537 [RBU6-201609])0.00%
Future(116588449 [RBX5-201511])0.00%