Skip to main content

portfolio analytics and optimization

Project description

Portfolio Analytics & Optimization

Discord X Downloads PyPI PyPI - Support Python Versions Discussions Ask DeepWiki View Code Wiki

This library is NOT ready for use, please wait for 0.1.0 release.

Problem

Most portfolio analytics libraries are shallow — they expect a simple price series and assume Buy and Hold (position = 1 throughout). This makes them useless for evaluating real trading strategies, where positions change, trades have costs, and fills aren't always complete.

Solution

pfolio computes metrics from realistic backtest dataframes with strategy-aware columns (position, trade_size, avg_price, etc.), factoring in fees, slippage, and fill rate.


pfolio is a portfolio analytics and optimization library built on skfolio, using polars to compute accurate metrics from realistic backtest DataFrames — not just buy-and-hold price series.

pfolio is part of the PFund ecosystem but works as a standalone package — any DataFrame in the right shape works

Core Features

  • Accurate backtest metrics — Sharpe, Sortino, Calmar, MDD, CVaR, VaR, and more
  • Trading-specific stats — win rate, payoff ratio, win/loss streaks, holding periods
  • Multi-asset portfolio backtest analytics
  • Portfolio optimization

Table of Contents

Installation

pip install pfolio

Quick Start

backtest_df is any DataFrame that includes strategy-aware columns alongside standard OHLCV data. Key columns pfolio uses (subset shown):

date close position trade_price trade_size avg_price
2024-01-01 96500 0 null 0.0 null
2024-01-02 97200 1 97200 1.0 97200
2024-01-03 98100 1 null 0.0 97200
2024-01-04 97800 0 97800 -1.0 null
import pfolio as po

# One-shot analysis — returns a dict of metrics
results = po.analyze(backtest_df, annualized_factor=252)

# Use a preset bundle: 'performance', 'risk', 'drawdown', 'trading', or 'full' (default)
results = po.analyze(backtest_df, annualized_factor=252, metric_bundle='performance')

# Pick specific metrics
results = po.analyze(backtest_df, annualized_factor=252, metrics=[po.SHARPE, po.MDD, po.SORTINO])

# Factor in transaction costs
results = po.analyze(backtest_df, annualized_factor=252, fee_bps=10, slippage_bps=5)

For multiple queries on the same data, use the Portfolio object directly:

portfolio = po.Portfolio(backtest_df, annualized_factor=252)

portfolio.annualized_sharpe_ratio
portfolio.max_drawdown
portfolio.win_rate
portfolio.holding_periods   # array of bars held per trade
portfolio.win_streaks       # array of consecutive winning trades

annualized_factor is inferred automatically from the timestamp column if not provided (e.g. 252 for daily stocks, 365 for daily crypto).

All of skfolio's measures are supported — see its documentation for the full list.


Related Projects

  • pfund — A Complete Algo-Trading Framework for Machine Learning, TradFi, CeFi and DeFi ready. Supports Vectorized and Event-Driven Backtesting, Paper and Live Trading
  • pfeed — Data engine for algo-trading, helping traders in getting real-time and historical data, and storing them in a local data lake for quantitative research.
  • pytrade.org - A curated list of Python libraries and resources for algorithmic trading.

Disclaimer

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This framework is intended for educational and research purposes only. It should not be used for real trading without understanding the risks involved. Trading in financial markets involves significant risk, and there is always the potential for loss. Your trading results may vary. No representation is being made that any account will or is likely to achieve profits or losses similar to those discussed on this platform.

The developers of this framework are not responsible for any financial losses incurred from using this software. This includes but not limited to losses resulting from inaccuracies in any financial data output by PFeed. Users should conduct their due diligence, verify the accuracy of any data produced by PFeed, and consult with a professional financial advisor before engaging in real trading activities.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pfolio-0.0.4.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pfolio-0.0.4-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file pfolio-0.0.4.tar.gz.

File metadata

  • Download URL: pfolio-0.0.4.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pfolio-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1af4169398afedf201ab888542e9192dcd749249959fc64caeb6c67fab07a26e
MD5 959ac7f7aedbf4116491945440e984c4
BLAKE2b-256 09685174a7512b6f2b3dbef92b9d47dda8b76c97cef16d5d381480ecf127d447

See more details on using hashes here.

File details

Details for the file pfolio-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pfolio-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pfolio-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d610a339b6933ea54b4a3ba16d9921c36be0d43bb56801fb3f50fa4951006512
MD5 3bbd482b25929e00f4ec81a81a8cea3e
BLAKE2b-256 fded4f6b0a6dae5ab22a36d7125544e03f2d202b805ee857cfcf97abaea4be72

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page