Zipline Backtest

We're ready to run a backtest. Zipline strategies are referenced by the filename without extension; thus, to run the strategy in our winners.py file, we use the code "winners".

Some Zipline backtests can be long-running. You can use the progress parameter to tell Zipline to log progress and performance statistics to flightlog periodically during the backtest. In this example, we log progress for each month of the simulation (progress="M"). Make sure flightlog is running in a terminal so you can see the progress output, then run the backtest:

In [1]:
from quantrocket.zipline import backtest
backtest(
    "winners", 
    start_date="2017-01-01",
    end_date="2020-01-01", 
    progress="M",
    filepath_or_buffer="winner_zipline_results.csv")

Zipline returns the backtest results as a CSV. You can use pyfolio, a companion library to Zipline, to look at a tear sheet of performance results:

In [2]:
import pyfolio as pf
pf.from_zipline_csv("winner_zipline_results.csv")
Start date2017-01-03
End date2019-12-31
Total months35
Backtest
Annual return5.489%
Cumulative returns17.338%
Annual volatility5.726%
Sharpe ratio0.96
Calmar ratio0.62
Stability0.53
Max drawdown-8.787%
Omega ratio1.18
Sortino ratio1.32
Skew-0.55
Kurtosis2.59
Tail ratio0.75
Daily value at risk-0.7%
Gross leverage0.30
Daily turnover7.96%
Worst drawdown periodsNet drawdown in %Peak dateValley dateRecovery dateDuration
08.792018-10-012018-12-242019-10-25280
15.102018-01-172018-03-232018-09-11170
22.402017-06-082017-06-162017-07-1326
32.052017-03-012017-04-132017-04-2540
42.032017-11-062017-12-062018-01-0444
/opt/conda/lib/python3.7/site-packages/pyfolio/tears.py:946: UserWarning: Passed returns do not overlap with anyinteresting times.
  'interesting times.', UserWarning)
Top 10 long positions of all timemax
column
Equity(FIBBG00B3T3HD3 [AA])15.24%
Equity(FIBBG000BPH459 [MSFT])14.73%
Equity(FIBBG000B9XRY4 [AAPL])13.07%
Equity(FIBBG000BMHYD1 [JNJ])10.10%
Equity(FIBBG000BKZB36 [HD])10.06%
Equity(FIBBG000GZQ728 [XOM])10.00%
Equity(FIBBG000BFWKC0 [MON])9.87%
Top 10 short positions of all timemax
column
Top 10 positions of all timemax
column
Equity(FIBBG00B3T3HD3 [AA])15.24%
Equity(FIBBG000BPH459 [MSFT])14.73%
Equity(FIBBG000B9XRY4 [AAPL])13.07%
Equity(FIBBG000BMHYD1 [JNJ])10.10%
Equity(FIBBG000BKZB36 [HD])10.06%
Equity(FIBBG000GZQ728 [XOM])10.00%
Equity(FIBBG000BFWKC0 [MON])9.87%
/opt/conda/lib/python3.7/site-packages/seaborn/distributions.py:369: UserWarning: Default bandwidth for data is 0; skipping density estimation.
  warnings.warn(msg, UserWarning)