QuantRocket logo
Disclaimer


Zipline Intro › Part 6: Zipline Backtest


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="2022-08-31", 
    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-04
End date2022-08-31
Total months67
Backtest
Annual return14.17%
Cumulative returns111.566%
Annual volatility18.74%
Sharpe ratio0.80
Calmar ratio0.47
Stability0.82
Max drawdown-30.093%
Omega ratio1.17
Sortino ratio1.12
Skew-0.59
Kurtosis8.16
Tail ratio0.96
Daily value at risk-2.301%
Gross leverage0.60
Daily turnover7.793%
Alpha0.06
Beta0.69
Worst drawdown periodsNet drawdown in %Peak dateValley dateRecovery dateDuration
030.092022-03-242022-07-14NaTNaN
120.882020-02-192020-03-232020-06-1081
215.962018-10-032018-12-242019-09-05242
310.852022-03-042022-03-152022-03-2314
49.962020-09-022020-09-232021-02-05113
Stress Eventsmeanminmax
2018 Bear Market-0.09%-2.63%2.69%
COVID-190.11%-9.46%6.98%
Top 10 long positions of all timemax
column
Equity(FIBBG00B3T3HD3 [AA])47.25%
Equity(FIBBG000BPH459 [MSFT])32.31%
Equity(FIBBG000B9XRY4 [AAPL])28.60%
Equity(FIBBG000GZQ728 [XOM])26.28%
Equity(FIBBG000BMHYD1 [JNJ])18.74%
Equity(FIBBG000BKZB36 [HD])18.00%
Equity(FIBBG000BDTBL9 [SPY])17.37%
Equity(FIBBG000BFWKC0 [MON])16.70%
Top 10 short positions of all timemax
column
Top 10 positions of all timemax
column
Equity(FIBBG00B3T3HD3 [AA])47.25%
Equity(FIBBG000BPH459 [MSFT])32.31%
Equity(FIBBG000B9XRY4 [AAPL])28.60%
Equity(FIBBG000GZQ728 [XOM])26.28%
Equity(FIBBG000BMHYD1 [JNJ])18.74%
Equity(FIBBG000BKZB36 [HD])18.00%
Equity(FIBBG000BDTBL9 [SPY])17.37%
Equity(FIBBG000BFWKC0 [MON])16.70%

View backtest trades in the Data Browser¶

You can also open the Zipline backtest results CSV in the Data Browser to see price charts for individual securities overlaid with green or red shading to indicate when you were long or short the security. To do so, right-click on the CSV file in the JupyterLab File Browser, then click "Open in Data Browser as..." > "Zipline backtest results file". See the explainer video below.

In [3]:
from IPython.display import VimeoVideo
VimeoVideo('919690103', width=700, height=394)
Out[3]:

Next Up¶

Part 7: Zipline Parameter Scans