Create beautiful strategy tear sheets with Pyfolio Reloaded
Create beautiful strategy tear sheets with Pyfolio Reloaded
I like to say “no single metric paints the whole picture” of a strategy’s performance.
That’s why you need to look at things like risk-adjusted performance, return distributions, and performance against benchmarks.
The problem?
It’s hard to manually compute all the different metrics you need to understand the dynamics of your strategy fully.
That’s where tear sheets can help.
By reading today’s newsletters, you’ll use a few lines of Python to get the whole picture of a strategy’s performance.
Create beautiful strategy tear sheets with Pyfolio Reloaded
The best traders have the best risk management.
Even if you’re not the best trader, you can still use Pyfolio Reloaded to quickly analyze risk in the same way as the professionals.
Performance analytics, including backtesting and data visualization, help refine these strategies for better outcomes.
Professionals use these tactics to make informed decisions and adapt to market changes. They integrate risk management with performance data to enhance flexibility and consistency in trading results. This integration minimizes emotional bias and supports data-driven trading strategies.
Let's see how it works with Python.
Retrieve historical stock data for selected tickers
Start with our imports.
1import yfinance as yf
2import pandas as pd
3import pyfolio as pf
4import warnings
5warnings.filterwarnings("ignore")
Then use the yfinance
library to download historical stock data for Apple, Nvidia, and the SPDR S&P 500 ETF (SPY). We'll get data from January 1, 2020, to January 1, 2023.
After getting the price data, we'll calculate the daily returns for the stocks using their adjusted closing prices and extract the benchmark returns.
1data = yf.download(
2 ["AAPL", "NVDA", "SPY"],
3 start="2020-01-01",
4 end="2023-01-01"
5)
6
7returns = data["Adj Close"].pct_change().dropna()
8
9benchmark_rets = returns.pop("SPY")
10portfolio_returns = returns.sum(axis=1)
Generate a detailed performance analysis with Pyfolio
Finally, we'll use pyfolio
to create a comprehensive performance analysis of our portfolio against the benchmark.
1pf.create_full_tear_sheet(
2 portfolio_returns,
3 benchmark_rets=benchmark_rets
4)
The code uses the pyfolio
library to generate a detailed performance analysis, known as a "tear sheet," for our portfolio. By calling the create_full_tear_sheet
function, we pass in the portfolio's daily returns and the benchmark returns for SPY.
Pyfolio then generates a comprehensive report that includes various metrics, plots, and analyses to evaluate the portfolio's performance.
Something like this:
Tear sheets are great for reviewing performance statistics like cumulative returns, risk metrics, and visualizations such as the rolling beta and drawdown periods. The tear sheet provides valuable insights into how the portfolio has performed relative to the benchmark over the specified period.
These are some of the key metrics based on this two-stock portfolio.
- Annual Return (40.157%): Indicates the portfolio's average yearly growth, suggesting strong performance but potentially high associated risks.
- Cumulative Returns (174.957%): Shows total portfolio growth over the investment period, highlighting significant gains compared to the starting value.
- Annual Volatility (86.289%): Reflects the portfolio's high level of price fluctuation, indicating substantial risk and potential variability in returns.
- Sharpe Ratio (0.83): Suggests moderate risk-adjusted returns; higher ratios are typically more desirable for balancing risk and reward.
- Calmar Ratio (0.53): Indicates relatively low risk-adjusted returns when factoring in maximum drawdown, signaling room for improvement.
- Max Drawdown (-75.729%): Highlights significant peak-to-trough loss, signaling a high risk of large capital declines.
- Skew (-0.09): Shows a near-symmetric return distribution, with slightly more negative than positive outliers.
- Kurtosis (3.12): Suggests returns distribution with moderate tail risk, close to a normal distribution but with slightly fat tails.
- Alpha (0.52): Demonstrates excess returns relative to the benchmark, suggesting some degree of portfolio manager skill.
- Beta (2.91): Indicates the portfolio is highly sensitive to market movements, amplifying both gains and losses compared to the benchmark.
Your next steps
Now that you've seen how to analyze a portfolio's performance, try experimenting with different stocks or date ranges. You can add more stocks to the portfolio or change the benchmark to see how it affects the results.
Download your live portfolio’s performance, compute the returns, and analyze it with Pyfolio.