Visualize performance with Plotting Functions
mainpyfolio.plotting module contains functions designed to facilitate the visualization of performance metrics, allowing you to create charts and plots for reporting and analysis.repository·main·Indexed 20 days ago
https://github.com/stefan-jansen/pyfolio-reloadedA Python library for the performance and risk analysis of financial portfolios. It provides comprehensive 'tear sheets' containing metrics and time-series plots, and is specifically designed to work with the Zipline backtesting engine. Key features include performance attribution, round trip analysis, sector-based PnL reporting, and liquidity analysis such as estimating days to liquidate positions and applying quadratic slippage penalties.
pyfolio.plotting module contains functions designed to facilitate the visualization of performance metrics, allowing you to create charts and plots for reporting and analysis.pyfolio.utils module contains helper functions for common tasks. For example, it includes utilities to format factor data into the specific input formats required by other pyfolio modules.pyfolio.tears module is used to generate thematic plots and summary tear sheets that combine key portfolio metrics. These tear sheets provide a comprehensive overview of strategy performance in a single visual or tabular format.The core functionality of pyfolio is the generation of "tear sheets". A tear sheet is a comprehensive report that combines individual plots and summary statistics to provide a view of a trading algorithm's performance.
Key components of a tear sheet include:
pyfolio is designed to work well with the Zipline backtesting library.
pyfolio.timeseries module to calculate specific performance and risk metrics over time. This module is the primary entry point for obtaining granular time-based statistical data for a portfolio or strategy.In versions 0.9.0 and later, pyfolio is completely independent of benchmarks. This allows for the analysis of international equities and alternative data sets without requiring a U.S. market benchmark like SPY.
pyfolio.perf_attrib module provides tools to analyze the sources of performance and risk. Use this module to decompose returns and understand the drivers behind your strategy's results.To contribute to or develop pyfolio-reloaded, it is recommended to use a virtual environment.
virtualenvwrapper):mkvirtualenv pyfoliopython -m pip install .[all]mkvirtualenv pyfolio
python -m pip install .[all]The best way to explore pyfolio is by running the provided examples in a Jupyter notebook.
jupyter notebookpyfolio/examples directory.Shift+Enter.You can install pyfolio-reloaded using either pip or conda.
For standard installation, use:
pip install pyfolio-reloadedFor conda users, use the ml4t channel:
conda install -c ml4t pyfolio-reloadedTo include sector-based analysis in your performance reports, you must provide a dictionary (or dict-like structure) where the keys are asset symbols and the values are their corresponding sectors.
Providing these mappings allows pyfolio to:
You can pass this dictionary to create_position_tear_sheet or to the high-level create_full_tearsheet function using the sector_mappings keyword argument.
# Define your mapping: {symbol: sector}
sect_map = {
'COST': 'Consumer Goods',
'INTC': 'Technology',
'CERN': 'Healthcare',
'GPS': 'Technology',
'MMM': 'Construction',
'DELL': 'Technology',
'AMD': 'Technology'
}
# Use in position tear sheet
pf.create_position_tear_sheet(returns, positions, sector_mappings=sect_map)
# Use in round trip tear sheet
pf.create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings=sect_map)
# Use in full tear sheet
pf.create_full_tearsheet(returns, positions, sector_mappings=sect_map)Pyfolio requires input data to be timezone-aware and set to the UTC timezone. When using yfinance to fetch stock history, you must explicitly localize the index to UTC to avoid errors during analysis.
To prepare returns:
yf.Ticker.history()..tz_localize('utc').Close) to create a returns series.import yfinance as yf
import pyfolio as pf
# Download data
fb = yf.Ticker('FB')
history = fb.history('max')
# CRITICAL: Pyfolio expects tz-aware input set to UTC timezone
history.index = history.index.tz_localize('utc')
# Calculate returns
returns = history.Close.pct_change()