Path: blob/master/05_strategy_evaluation/03_pyfolio_demo.ipynb
2923 views
From zipline
to pyfolio
Pyfolio facilitates the analysis of portfolio performance and risk in-sample and out-of-sample using many standard metrics. It produces tear sheets covering the analysis of returns, positions, and transactions, as well as event risk during periods of market stress using several built-in scenarios, and also includes Bayesian out-of-sample performance analysis.
Open-source backtester by Quantopian Inc.
Powers Quantopian.com
State-of-the-art portfolio and risk analytics
Various models for transaction costs and slippage.
Open source and free: Apache v2 license
Can be used:
stand alone
with Zipline
on Quantopian
Imports & Settings
Converting data from zipline to pyfolio
pyfolio
relies on portfolio returns and position data, and can also take into account the transaction costs and slippage losses of trading activity. The metrics are computed using the empyrical library that can also be used on a standalone basis. The performance DataFrame produced by the zipline backtesting engine can be translated into the requisite pyfolio input.
Sector Map
Benchmark
Returns Analysis
Testing a trading strategy involves backtesting against historical data to fine-tune alpha factor parameters, as well as forward-testing against new market data to validate that the strategy performs well out of sample or if the parameters are too closely tailored to specific historical circumstances.
Pyfolio allows for the designation of an out-of-sample period to simulate walk-forward testing. There are numerous aspects to take into account when testing a strategy to obtain statistically reliable results, which we will address here.
Rolling Returns OOS
The plot_rolling_returns
function displays cumulative in and out-of-sample returns against a user-defined benchmark (we are using the S&P 500):
The plot includes a cone that shows expanding confidence intervals to indicate when out-of-sample returns appear unlikely given random-walk assumptions. Here, our strategy did not perform well against the benchmark during the simulated 2017 out-of-sample period
Summary Performance Statistics
pyfolio offers several analytic functions and plots. The perf_stats summary displays the annual and cumulative returns, volatility, skew, and kurtosis of returns and the SR. The following additional metrics (which can also be calculated individually) are most important:
Max drawdown: Highest percentage loss from the previous peak
Calmar ratio: Annual portfolio return relative to maximal drawdown
Omega ratio: The probability-weighted ratio of gains versus losses for a return target, zero per default
Sortino ratio: Excess return relative to downside standard deviation
Tail ratio: Size of the right tail (gains, the absolute value of the 95th percentile) relative to the size of the left tail (losses, abs. value of the 5th percentile)
Daily value at risk (VaR): Loss corresponding to a return two standard deviations below the daily mean
Alpha: Portfolio return unexplained by the benchmark return
Beta: Exposure to the benchmark
Rolling Sharpe
Rolling Beta
Drawdown Periods
The plot_drawdown_periods(returns) function plots the principal drawdown periods for the portfolio, and several other plotting functions show the rolling SR and rolling factor exposures to the market beta or the Fama French size, growth, and momentum factors:
This plot, which highlights a subset of the visualization contained in the various tear sheets, illustrates how pyfolio allows us to drill down into the performance characteristics and exposure to fundamental drivers of risk and returns.
Modeling Event Risk
Pyfolio also includes timelines for various events that you can use to compare the performance of a portfolio to a benchmark during this period, for example, during the fall 2015 selloff following the Brexit vote.