Pyfolio

Once you have written and backtested an algorithm, you can analyze the results of any backtest in great detail by creating a pyfolio tearsheet. Pyfolio analyzes a backtest and provides a wealth of performance statistics commonly used by professional fund managers including annual/monthly returns, return quantiles, rolling beta/Sharpe ratios, portfolio turnover, and more.

To analyze a Quantopian backtest using Pyfolio, you will need to start by importing an AlgorithmResult object into a Research notebook. Then, you can access pyfolio functions as methods of the AlgorithmResult object. (Quantopian wraps pyfolio functionality as methods of the AlgorithmResult object, so you shouldn't need to import pyfolio unless you're using it outside of the platform.)

Note

Pyfolio is an open source project maintained by Quantopian. It is wrapped in the create_full_tear_sheet() function in Quantopian Research to make it easier to use, but you can also view the source code and install it locally.

Importing Backtests

Once you have backtested your algorithm, you can import your backtest as an AlgorithmResult object for analysis. To do this, you will need to get your backtest's ID; you can then pass that ID to the get_backtest() function.

For example, the following code snippet imports a backtest:

bt = get_backtest('<your backtest ID>')

Here, bt would be an AlgorithmResult object.

You can also easily get code to access your backtest under the "Notebook" tab of the backtest screen. On this screen, code to access your backtest is pre-populated in the first cell of a Research notebook:

bt = get_backtest('<your backtest ID>')
bt.create_full_tear_sheet()

Note

Once you've run code in this notebook, it will be stored in the Notebooks section of your account, so you can access it at any time.

Running Tearsheets

Once you've retrieved the AlgorithmResult, you can get metrics or run a full tearsheet to analyze your algorithm's performance.

The metrics (non-tearsheet) functions provide specific, standalone plots and tables, while the tearsheet functions provide "tearsheets" that output many plots and tables to provide a comprehensive image of your backtest/algorithm. Metrics and tearsheets are available as methods of the AlgorithmResult object.

Note

One of the most commonly-used methods is create_full_tear_sheet(), which generates a tearsheet with returns, significant events, positions, transactions, and Bayesian analysis.

For a full list of available metrics and tearsheets, see the API Reference.

For a breakdown of how to interpret the various plots and metrics, see the Portfolio Analysis lecture and the Pyfolio documentation.