Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
Q

quantstats

> 编程语言
Open source

Portfolio analytics for quants, written in Python

7.5K stars0 likes2 views
WebsiteGitHub

About

Portfolio analytics for quants, written in Python

QuantStats: Portfolio analytics for quants

QuantStats Python library that performs portfolio profiling, allowing quants and portfolio managers to understand their performance better by providing them with in-depth analytics and risk metrics.

Changelog »

QuantStats is comprised of 3 main modules:

  1. quantstats.stats - for calculating various performance metrics, like Sharpe ratio, Win rate, Volatility, etc.
  2. quantstats.plots - for visualizing performance, drawdowns, rolling statistics, monthly returns, etc.
  3. quantstats.reports - for generating metrics reports, batch plotting, and creating tear sheets that can be saved as an HTML file.

NEW! Monte Carlo Simulations

Run probabilistic risk analysis with built-in Monte Carlo simulations:

python
mc = qs.stats.montecarlo(returns, sims=1000, bust=-0.20, goal=0.50)
print(f"Bust probability: {mc.bust_probability:.1%}")
print(f"Goal probability: {mc.goal_probability:.1%}")
mc.plot()

Full Monte Carlo documentation »


Quick Start

python
%matplotlib inline
import quantstats as qs

# extend pandas functionality with metrics, etc.
qs.extend_pandas()

# fetch the daily returns for a stock
stock = qs.utils.download_returns('META')

# show sharpe ratio
qs.stats.sharpe(stock)

# or using extend_pandas() :)
stock.sharpe()

Output:

0.7604779884378278

Visualize stock performance

python
qs.plots.snapshot(stock, title='Facebook Performance', show=True)

# can also be called via:
# stock.plot_snapshot(title='Facebook Performance', show=True)

Output:

Creating a report

You can create 7 different report tearsheets:

  1. qs.reports.metrics(mode='basic|full", ...) - shows basic/full metrics
  2. qs.reports.plots(mode='basic|full", ...) - shows basic/full plots
  3. qs.reports.basic(...) - shows basic metrics and plots
  4. qs.reports.full(...) - shows full metrics and plots
  5. qs.reports.html(...) - generates a complete report as html

Let's create an html tearsheet:

python
# benchmark can be a pandas Series or ticker
qs.reports.html(stock, "SPY")

Output will generate something like this:

View original html file

Available methods

To view a complete list of available methods, run:

python
[f for f in dir(qs.stats) if f[0] != '_']
…
python
[f for f in dir(qs.plots) if f[0] != '_']
python
['daily_returns',
 'distribution',
 'drawdown',
 'drawdowns_periods',
 'earnings',
 'histogram',
 'log_returns',
 'monthly_heatmap',
 'montecarlo',
 'montecarlo_distribution',
 'returns',
 'rolling_beta',
 'rolling_sharpe',
 'rolling_sortino',
 'rolling_volatility',
 'snapshot',
 'yearly_returns']

*** Full documentation coming soon ***

Important: Period-Based vs Trade-Based Metrics

QuantStats analyzes return series (daily, weekly, monthly returns), not discrete trade data. This means:

  • Win Rate = percentage of periods with positive returns
  • Consecutive Wins/Losses = consecutive positive/negative return periods
  • Payoff Ratio = average winning period return / average losing period return
  • Profit Factor = sum of positive returns / sum of negative returns

These metrics are valid and useful for:

  • Systematic/algorithmic strategies with regular rebalancing
  • Analyzing return-series behavior over time
  • Comparing strategies on a period-by-period basis

For discretionary traders with multi-day trades, these period-based metrics may differ from trade-level statistics. A single 5-day trade might span 3 positive days and 2 negative days - QuantStats would count these as 3 "wins" and 2 "losses" at the daily level.

This is consistent with how all return-based analytics work (Sharpe ratio, Sortino ratio, drawdown analysis, etc.) - they operate on return periods, not discrete trade entries/exits.


In the meantime, you can get insights as to optional parameters for each method, by using Python's help method:

python
help(qs.stats.conditional_value_at_risk)
Help on function conditional_value_at_risk in module quantstats.stats:

conditional_value_at_risk(returns, sigma=1, confidence=0.99)
    calculates the conditional daily value-at-risk (aka expected shortfall)
    quantifies the amount of tail risk an investment

Installation

Install using pip:

bash
$ pip install quantstats --upgrade --no-cache-dir

Install using conda:

bash
$ conda install -c ranaroussi quantstats

Requirements

  • Python >= 3.10
  • pandas >= 1.5.0
  • numpy >= 1.24.0
  • scipy >= 1.11.0
  • matplotlib >= 3.7.0
  • seaborn >= 0.13.0
  • tabulate >= 0.9.0
  • yfinance >= 0.2.40
  • plotly >= 5.0.0 (optional, for using plots.to_plotly())

Questions?

This is a new library... If you find a bug, please open an issue.

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

Known Issues

For some reason, I couldn't find a way to tell seaborn not to return the monthly returns heatmap when instructed to save - so even if you save the plot (by passing savefig={...}) it will still show the plot.

Legal Stuff

QuantStats is distributed under the Apache Software License. See the LICENSE.txt file in the release for details.

P.S.

Please drop me a note with any feedback you have.

Ran Aroussi

Issues· 12 open

View all issuesOpen on GitHub
  • #514

    qs.stats.information_ratio() uses arithmetic mean instead of geometric

    Updated Sep 28, 2026
  • #547

    conditional_value_at_risk mixes a parametric VaR threshold with an empirical tail mean, and silently returns VaR when the tail is empty

    Updated Sep 12, 2026
  • #546

    _prepare_returns silently converts missing observations to 0.0 returns in every statistic

    Updated Sep 12, 2026
  • #543

    _prepare_returns cache key collision changes Series/DataFrame type and column metadata

    Updated Aug 24, 2026
  • #538

    GIFT NIFTY

    Updated Jul 29, 2026
  • #537

    kelly_criterion is off by a factor of the average loss, and rf is routed by inspecting the call stack

    Updated Jul 27, 2026
  • #535

    Wrong Kelly Criteria

    Updated Jul 21, 2026
  • #521

    qs.reports.html() function assumes the risk free rate is constant

    Updated Jun 5, 2026
  • #518

    [bug] EOY returns vs benchmark on time range longer than 10y

    Updated May 26, 2026
  • #520

    Monthly Return Heatmap Display Issue in VS Code

    Updated May 26, 2026

> Tags

Pythonalgo-tradingalgorithmic-tradingalgotradingfinance

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言