.. image:: https://img.shields.io/badge/python-3.6+-blue.svg?style=flat :target: https://pypi.python.org/pypi/quantstats :alt: Python version
.. image:: https://img.shields.io/pypi/v/quantstats.svg?maxAge=60 :target: https://pypi.python.org/pypi/quantstats :alt: PyPi version
.. image:: https://img.shields.io/pypi/status/quantstats.svg?maxAge=60 :target: https://pypi.python.org/pypi/quantstats :alt: PyPi status
.. image:: https://img.shields.io/travis/ranaroussi/quantstats/main.svg?maxAge=1 :target: https://travis-ci.com/github/ranaroussi/quantstats :alt: Travis-CI build status
.. image:: https://img.shields.io/pypi/dm/quantstats.svg?maxAge=2592000&label=installs&color=%2327B1FF :target: https://pypi.python.org/pypi/quantstats :alt: PyPi downloads
.. image:: https://www.codefactor.io/repository/github/ranaroussi/quantstats/badge :target: https://www.codefactor.io/repository/github/ranaroussi/quantstats :alt: CodeFactor
.. image:: https://img.shields.io/github/stars/ranaroussi/quantstats.svg?style=social&label=Star&maxAge=60 :target: https://github.com/ranaroussi/quantstats :alt: Star this repo
.. image:: https://img.shields.io/twitter/follow/aroussi.svg?style=social&label=Follow&maxAge=60 :target: https://twitter.com/aroussi :alt: Follow me on twitter
\
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 ยป <./CHANGELOG.rst>
__
QuantStats is comprised of 3 main modules: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
quantstats.stats
- for calculating various performance metrics, like Sharpe ratio, Win rate, Volatility, etc.quantstats.plots
- for visualizing performance, drawdowns, rolling statistics, monthly returns, etc.quantstats.reports
- for generating metrics reports, batch plotting, and creating tear sheets that can be saved as an HTML file.Here's an example of a simple tear sheet analyzing a strategy:
.. code:: 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('FB')
# show sharpe ratio
qs.stats.sharpe(stock)
# or using extend_pandas() :)
stock.sharpe()
Output:
.. code:: text
0.8135304438803402
Visualize stock performance ~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
qs.plots.snapshot(stock, title='Facebook Performance')
# can also be called via:
# stock.plot_snapshot(title='Facebook Performance')
Output:
.. image:: https://github.com/ranaroussi/quantstats/blob/main/docs/snapshot.jpg?raw=true :alt: Snapshot plot
Creating a report ~~~~~~~~~~~~~~~~~
You can create 7 different report tearsheets:
qs.reports.metrics(mode='basic|full", ...)
- shows basic/full metricsqs.reports.plots(mode='basic|full", ...)
- shows basic/full plotsqs.reports.basic(...)
- shows basic metrics and plotsqs.reports.full(...)
- shows full metrics and plotsqs.reports.html(...)
- generates a complete report as htmlLet' create an html tearsheet
.. code:: python
(benchmark can be a pandas Series or ticker)
qs.reports.html(stock, "SPY")
Output will generate something like this:
.. image:: https://github.com/ranaroussi/quantstats/blob/main/docs/report.jpg?raw=true :alt: HTML tearsheet
(view original html file <https://rawcdn.githack.com/ranaroussi/quantstats/main/docs/tearsheet.html>
_)
To view a complete list of available methods, run ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
[f for f in dir(qs.stats) if f[0] != '_']
.. code:: text
['avg_loss',
'avg_return',
'avg_win',
'best',
'cagr',
'calmar',
'common_sense_ratio',
'comp',
'compare',
'compsum',
'conditional_value_at_risk',
'consecutive_losses',
'consecutive_wins',
'cpc_index',
'cvar',
'drawdown_details',
'expected_return',
'expected_shortfall',
'exposure',
'gain_to_pain_ratio',
'geometric_mean',
'ghpr',
'greeks',
'implied_volatility',
'information_ratio',
'kelly_criterion',
'kurtosis',
'max_drawdown',
'monthly_returns',
'outlier_loss_ratio',
'outlier_win_ratio',
'outliers',
'payoff_ratio',
'profit_factor',
'profit_ratio',
'r2',
'r_squared',
'rar',
'recovery_factor',
'remove_outliers',
'risk_of_ruin',
'risk_return_ratio',
'rolling_greeks',
'ror',
'sharpe',
'skew',
'sortino',
'adjusted_sortino',
'tail_ratio',
'to_drawdown_series',
'ulcer_index',
'ulcer_performance_index',
'upi',
'utils',
'value_at_risk',
'var',
'volatility',
'win_loss_ratio',
'win_rate',
'worst']
.. code:: python
[f for f in dir(qs.plots) if f[0] != '_']
.. code:: text
['daily_returns',
'distribution',
'drawdown',
'drawdowns_periods',
'earnings',
'histogram',
'log_returns',
'monthly_heatmap',
'returns',
'rolling_beta',
'rolling_sharpe',
'rolling_sortino',
'rolling_volatility',
'snapshot',
'yearly_returns']
*** Full documenttion coming soon ***
In the meantime, you can get insights as to optional parameters for each method, by using Python's help
method:
.. code:: python
help(qs.stats.conditional_value_at_risk)
.. code:: text
Help on function conditional_value_at_risk in module quantstats.stats:
conditional_value_at_risk(returns, sigma=1, confidence=0.99)
calculats the conditional daily value-at-risk (aka expected shortfall)
quantifies the amount of tail risk an investment
Install using pip
:
.. code:: bash
$ pip install quantstats --upgrade --no-cache-dir
Install using conda
:
.. code:: bash
$ conda install -c ranaroussi quantstats
Python <https://www.python.org>
_ >= 3.5+pandas <https://github.com/pydata/pandas>
_ (tested to work with >=0.24.0)numpy <http://www.numpy.org>
_ >= 1.15.0scipy <https://www.scipy.org>
_ >= 1.2.0matplotlib <https://matplotlib.org>
_ >= 3.0.0seaborn <https://seaborn.pydata.org>
_ >= 0.9.0tabulate <https://bitbucket.org/astanin/python-tabulate>
_ >= 0.8.0yfinance <https://github.com/ranaroussi/yfinance>
_ >= 0.1.38plotly <https://plot.ly/>
_ >= 3.4.1 (optional, for using plots.to_plotly()
)This is a new library... If you find a bug, please
open an issue <https://github.com/ranaroussi/quantstats/issues>
_
in this repository.
If you'd like to contribute, a great place to look is the
issues marked with help-wanted <https://github.com/ranaroussi/quantstats/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22>
_.
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.
QuantStats is distributed under the Apache Software License. See the LICENSE.txt <./LICENSE.txt>
_ file in the release for details.
Please drop me a note with any feedback you have.
Ran Aroussi
Currently we use line chart for it and it seems a bit odd.
Hi, I'm running quantstats on Ubuntu 20.04 and found the "Monthly Returns (%)" chart shows all "0.0" for every months. Other charts are all fine. I run the same code on another Windows system and the "Monthly Returns (%)" is fine. Not sure what's the problem on the Ubuntu system.
Fixed import due to depracation warning below: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display
Hi,
I'm trying to generate the full report for the returns and benchmark series. For example,
qs.reports.html(returns, benckmark=benchmark, rf=0.0, output='', download_filename='quantstats-returns.html', match_dates=True)
The issue is that it would only plot the returns data. The benchmark data does not show up in the plot nor there is a second column in the performance table.
Funny enough, if i use this code, qs.reports.full(returns, benchmark), the IDE would display both tables and all the plots with both set of data (returns and benchmark).
So it seems the qs.reports.html is not working.
Any ideas?
Problem: - GitHub Actions are versioned. There is currently no way of automatically updating to the latest actions.
Solution: - Have dependabot update GitHub Actions.
Hi @ranaroussi
First of all, I must thank you for your two amazing projects.
Let me now dive into my question: - Context: I want to compute the Sharpe Ratio of my portfolio consisting of daily results. For now, I only have 1 week of daily results, meaning ~ 5-6 results. - Problem: Leaving the Sharpe Ratio method as-is, the Sharpe Ratio is way too high: around 15!
What I know - I've read in another github issue https://github.com/ranaroussi/quantstats/issues/116 that I needed to change the period in the sharpe ratio method, for instance by overwriting it with a smaller period, which I did with period=52 and period=12. - Another user was mentioning setting trading_year_days=52 for weekly, so I am a bit lost - However, I still believe that the Sharpe Ratio is too high when using these periods. - The Sharpe Ratio seems better when using period=6 or period=4 or period=3, but I have no idea which period I should really use.
Question - I would like to know and understand the relationship between the number of daily results I've got, and the period value I should set in the Sharpe Ratio method, to be as close as possible to a realistic Sharpe Ratio Value - Keep in mind that for now, I only have access to 1 week of daily results, but not the entire month. - Are you able to advise the rule of thumb in that case ? - How about the trading_year_days, does it really need to be modified?
Thanks in advance
Fixed EOY compounded return calculation
fillna(0)
on plot's beta (issue #193)Fixed sigma calculation in stats.probabilistic_ratio()
Added option to explicitly provide the benchmark title in html reports via benchmark_title=...
Fix for benchmark name in html report when supplied by the user
Fixed dependency name in requirements.txt
Founder @Tradologics. Creating tools to help people work smarter, not harder. Programming is how I meditate.
GitHub Repositoryquant algotrading algorithmic-trading quantitative-trading quantitative-analysis algo-trading python visualization plotting quantitative-finance finance