QuantStats

0.0.81 · active · verified Thu Apr 16

QuantStats is a Python library for portfolio analytics that enables quantitative analysts and portfolio managers to gain in-depth insights into their investment performance and risk metrics. It provides modules for calculating various statistics, generating plots, and creating comprehensive HTML reports. The library is actively maintained with frequent updates, with the current version being 0.0.81.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import QuantStats, extend pandas DataFrames for easy access to metrics, download historical stock data using `yfinance`, calculate a basic performance metric like the Sharpe Ratio, and generate a comprehensive HTML tearsheet comparing a stock's performance against a benchmark.

import quantstats as qs
import yfinance as yf
import pandas as pd

# Extend pandas functionality with QuantStats methods
qs.extend_pandas()

# Download daily returns for a stock using yfinance
ticker = 'AAPL'
stock_data = yf.download(ticker, start='2020-01-01', end='2023-12-31')
returns = stock_data['Adj Close'].pct_change().dropna()

# Optionally, download benchmark returns
benchmark_ticker = 'SPY'
benchmark_data = yf.download(benchmark_ticker, start='2020-01-01', end='2023-12-31')
benchmark_returns = benchmark_data['Adj Close'].pct_change().dropna()

# Calculate and print Sharpe Ratio
sharpe_ratio = returns.sharpe()
print(f"Sharpe Ratio for {ticker}: {sharpe_ratio:.4f}")

# Generate a full HTML report (opens in browser by default)
# Ensure you have matplotlib and seaborn installed for plots
qs.reports.html(returns, benchmark=benchmark_returns, title=f'{ticker} vs {benchmark_ticker} Performance', output='quantstats_report.html')

view raw JSON →