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.
%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
qs.plots.snapshot(stock, title='Facebook Performance', show=True)
# can also be called via:
# stock.plot_snapshot(title='Facebook Performance', show=True)
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:
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.95, prepare_returns=True, method='parametric')
Calculate the Conditional Value at Risk (CVaR), also known as Expected Shortfall.
CVaR measures the expected loss given that a loss exceeds the VaR threshold.
It quantifies the amount of tail risk an investment faces, providing a more
comprehensive risk measure than VaR alone.
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
Works well with
Use together with quantstats: extensions, drivers, servers and companion tools.
pandasPythie’s Index: 88Powerful data structures for data analysis, time series, and statistics720.9M downloads/mo