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:
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:
import pyfolio as pf
pf.from_zipline_csv("winner_zipline_results.csv")
Start date | 2017-01-04 | |
---|---|---|
End date | 2022-08-31 | |
Total months | 67 | |
Backtest | ||
Annual return | 14.17% | |
Cumulative returns | 111.566% | |
Annual volatility | 18.74% | |
Sharpe ratio | 0.80 | |
Calmar ratio | 0.47 | |
Stability | 0.82 | |
Max drawdown | -30.093% | |
Omega ratio | 1.17 | |
Sortino ratio | 1.12 | |
Skew | -0.59 | |
Kurtosis | 8.16 | |
Tail ratio | 0.96 | |
Daily value at risk | -2.301% | |
Gross leverage | 0.60 | |
Daily turnover | 7.793% | |
Alpha | 0.06 | |
Beta | 0.69 |
Worst drawdown periods | Net drawdown in % | Peak date | Valley date | Recovery date | Duration |
---|---|---|---|---|---|
0 | 30.09 | 2022-03-24 | 2022-07-14 | NaT | NaN |
1 | 20.88 | 2020-02-19 | 2020-03-23 | 2020-06-10 | 81 |
2 | 15.96 | 2018-10-03 | 2018-12-24 | 2019-09-05 | 242 |
3 | 10.85 | 2022-03-04 | 2022-03-15 | 2022-03-23 | 14 |
4 | 9.96 | 2020-09-02 | 2020-09-23 | 2021-02-05 | 113 |
Stress Events | mean | min | max |
---|---|---|---|
2018 Bear Market | -0.09% | -2.63% | 2.69% |
COVID-19 | 0.11% | -9.46% | 6.98% |
Top 10 long positions of all time | max |
---|---|
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 time | max |
---|---|
column |
Top 10 positions of all time | max |
---|---|
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% |
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.
from IPython.display import VimeoVideo
VimeoVideo('919690103', width=700, height=394)