Convert Figma logo to code with AI

quantopian logopyfolio

Portfolio and risk analytics in Python

5,612
1,762
5,612
161

Top Related Projects

Portfolio analytics for quants, written in Python

15,147

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.

MlFinLab helps portfolio managers and traders who want to leverage the power of machine learning by providing reproducible, interpretable, and easy to use tools.

1,897

ffn - a financial function library for Python

Quick Overview

Pyfolio is an open-source Python library for performance and risk analysis of financial portfolios. It provides a set of tools for analyzing and visualizing portfolio performance, risk metrics, and trade analysis, making it particularly useful for quantitative finance professionals and researchers.

Pros

  • Comprehensive set of performance and risk metrics
  • Easy integration with popular data sources and trading platforms
  • Customizable and extensible for specific analysis needs
  • Well-documented with extensive examples and tutorials

Cons

  • Requires some knowledge of Python and financial concepts
  • May have a steeper learning curve for beginners
  • Limited real-time analysis capabilities
  • Some advanced features may require additional dependencies

Code Examples

  1. Creating a simple tearsheet:
import pyfolio as pf
import pandas as pd

# Assuming 'returns' is a pandas Series of daily returns
returns = pd.Series(...)

pf.create_simple_tear_sheet(returns)
  1. Analyzing a strategy with transactions:
import pyfolio as pf
import pandas as pd

# Assuming 'returns', 'positions', and 'transactions' are pandas DataFrames
returns = pd.DataFrame(...)
positions = pd.DataFrame(...)
transactions = pd.DataFrame(...)

pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions)
  1. Comparing multiple strategies:
import pyfolio as pf
import pandas as pd

# Assuming 'returns1' and 'returns2' are pandas Series of daily returns
returns1 = pd.Series(...)
returns2 = pd.Series(...)

pf.create_simple_tear_sheet(returns1, benchmark_rets=returns2)

Getting Started

To get started with Pyfolio, follow these steps:

  1. Install Pyfolio using pip:

    pip install pyfolio
    
  2. Import the library and prepare your data:

    import pyfolio as pf
    import pandas as pd
    
    # Load your returns data (example using random data)
    import numpy as np
    returns = pd.Series(np.random.normal(0, 0.01, 252), index=pd.date_range('2020-01-01', periods=252, freq='B'))
    
    # Create a simple tearsheet
    pf.create_simple_tear_sheet(returns)
    

This will generate a basic performance analysis of your returns data. For more advanced usage, refer to the Pyfolio documentation and examples.

Competitor Comparisons

Portfolio analytics for quants, written in Python

Pros of QuantStats

  • More actively maintained with frequent updates
  • Broader range of performance metrics and risk measures
  • Includes tear sheets for stocks and ETFs, not just portfolios

Cons of QuantStats

  • Less comprehensive documentation compared to PyFolio
  • Steeper learning curve for beginners
  • Fewer built-in plotting options for custom visualizations

Code Comparison

PyFolio:

import pyfolio as pf

pf.create_full_tear_sheet(returns, 
                          benchmark_rets=benchmark_returns,
                          positions=positions,
                          transactions=transactions)

QuantStats:

import quantstats as qs

qs.reports.html(returns, 
                benchmark=benchmark_returns,
                output='tearsheet.html')

Both libraries offer similar functionality for generating performance reports, but QuantStats provides a more streamlined API for creating HTML tear sheets. PyFolio offers more granular control over individual components of the analysis, while QuantStats focuses on producing comprehensive reports with a single function call.

QuantStats is generally more suitable for quick analysis and report generation, while PyFolio may be preferred for more customized and in-depth portfolio analysis. The choice between the two depends on the specific requirements of the project and the user's familiarity with each library.

15,147

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.

Pros of qlib

  • More comprehensive and feature-rich, offering a complete AI-driven quantitative investment platform
  • Actively maintained with frequent updates and contributions from the community
  • Provides advanced machine learning models and strategies for quantitative trading

Cons of qlib

  • Steeper learning curve due to its complexity and extensive features
  • May be overkill for simple portfolio analysis tasks
  • Requires more computational resources for advanced features

Code Comparison

pyfolio:

import pyfolio as pf

returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(backtest)
pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions)

qlib:

from qlib.workflow import R
from qlib.workflow.record_temp import SignalRecord, PortAnaRecord

with R.start(experiment_name="workflow"):
    SR = SignalRecord(model=model, dataset=dataset)
    PAR = PortAnaRecord(recorder=SR)
    analysis = PAR.generate()

Summary

While pyfolio focuses on portfolio performance analysis and visualization, qlib offers a more comprehensive suite of tools for quantitative investment, including data processing, model training, and strategy development. pyfolio is simpler to use for basic portfolio analysis, while qlib provides more advanced features at the cost of increased complexity.

MlFinLab helps portfolio managers and traders who want to leverage the power of machine learning by providing reproducible, interpretable, and easy to use tools.

Pros of mlfinlab

  • Broader scope, covering machine learning techniques for financial applications
  • More active development and frequent updates
  • Extensive documentation and educational resources

Cons of mlfinlab

  • Steeper learning curve due to more complex algorithms
  • May require more computational resources for some functions
  • Less focus on traditional portfolio analysis techniques

Code Comparison

mlfinlab:

from mlfinlab.portfolio_optimization import MeanVarianceOptimisation

mvo = MeanVarianceOptimisation()
weights = mvo.optimize(expected_returns, covariance_matrix)

pyfolio:

import pyfolio as pf

pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions)

Summary

mlfinlab offers a wider range of machine learning-based financial analysis tools, making it suitable for advanced quantitative research. It's actively maintained and provides extensive documentation. However, it may be more complex to use and resource-intensive compared to pyfolio.

pyfolio, on the other hand, focuses on traditional portfolio analysis and performance metrics. It's simpler to use and provides quick insights into portfolio performance, but may lack some of the advanced features found in mlfinlab.

The choice between the two depends on the specific needs of the project, with mlfinlab being more suitable for machine learning-driven financial research, while pyfolio excels in straightforward portfolio analysis and visualization.

1,897

ffn - a financial function library for Python

Pros of ffn

  • Lightweight and focused on financial functions and analytics
  • Includes performance metrics not found in pyfolio (e.g., Sortino ratio, Calmar ratio)
  • Offers data visualization capabilities with minimal dependencies

Cons of ffn

  • Less comprehensive portfolio analysis tools compared to pyfolio
  • Smaller community and less frequent updates
  • Limited documentation and examples compared to pyfolio's extensive resources

Code Comparison

ffn:

import ffn

returns = ffn.get('SPY,AAPL', start='2010-01-01')
stats = returns.calc_stats()
print(stats.display())

pyfolio:

import pyfolio as pf

returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(backtest)
pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions)

The ffn code focuses on quick performance calculations, while pyfolio provides a more comprehensive analysis with its tear sheet functionality. ffn is more suitable for quick, specific calculations, whereas pyfolio excels in detailed portfolio analysis and reporting.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

pyfolio

pyfolio

Join the chat at https://gitter.im/quantopian/pyfolio build status

pyfolio is a Python library for performance and risk analysis of financial portfolios developed by Quantopian Inc. It works well with the Zipline open source backtesting library. Quantopian also offers a fully managed service for professionals that includes Zipline, Alphalens, Pyfolio, FactSet data, and more.

At the core of pyfolio is a so-called tear sheet that consists of various individual plots that provide a comprehensive image of the performance of a trading algorithm. Here's an example of a simple tear sheet analyzing a strategy:

simple tear 0 simple tear 1

Also see slides of a talk about pyfolio.

Installation

To install pyfolio, run:

pip install pyfolio

Development

For development, you may want to use a virtual environment to avoid dependency conflicts between pyfolio and other Python projects you have. To get set up with a virtual env, run:

mkvirtualenv pyfolio

Next, clone this git repository and run python setup.py develop and edit the library files directly.

Matplotlib on OSX

If you are on OSX and using a non-framework build of Python, you may need to set your backend:

echo "backend: TkAgg" > ~/.matplotlib/matplotlibrc

Usage

A good way to get started is to run the pyfolio examples in a Jupyter notebook. To do this, you first want to start a Jupyter notebook server:

jupyter notebook

From the notebook list page, navigate to the pyfolio examples directory and open a notebook. Execute the code in a notebook cell by clicking on it and hitting Shift+Enter.

Questions?

If you find a bug, feel free to open an issue in this repository.

You can also join our mailing list or our Gitter channel.

Support

Please open an issue for support.

Contributing

If you'd like to contribute, a great place to look is the issues marked with help-wanted.

For a list of core developers and outside collaborators, see the GitHub contributors list.