jQuantStats: Portfolio Analytics for Quants
jQuantStats is a Python library for portfolio analytics that helps quants and portfolio managers understand their performance through in-depth analytics and risk metrics. It provides tools for calculating various performance metrics and visualizing portfolio performance using interactive Plotly charts.
The library is inspired by QuantStats, but focuses on providing a clean, modern API with enhanced visualization capabilities. Key improvements include:
- Support for both pandas and polars DataFrames
- Modern interactive visualizations using Plotly
- Comprehensive test coverage with pytest
- Clean, well-documented API
- Efficient data processing with polars
- Performance Metrics: Calculate key metrics like Sharpe ratio, Sortino ratio, drawdowns, volatility, and more
- Risk Analysis: Analyze risk through metrics like Value at Risk (VaR), Conditional VaR, and drawdown analysis
- Interactive Visualizations: Create interactive plots for portfolio performance, drawdowns, and return distributions
- Benchmark Comparison: Compare your portfolio performance against benchmarks
- Pandas & Polars Support: Work with either pandas or polars DataFrames as input
pip install jquantstatsFor development:
pip install jquantstats[dev]>>> # Import jquantstats
>>> import polars as pl
>>> from jquantstats import build_data
>>> # Create sample returns data
>>> returns = pl.DataFrame({
... "Date": ["2023-01-01", "2023-01-02", "2023-01-03"],
... "Asset1": [0.01, -0.02, 0.03],
... "Asset2": [0.02, 0.01, -0.01]
... }).with_columns(pl.col("Date").str.to_date())
>>> returns
shape: (3, 3)
ββββββββββββββ¬βββββββββ¬βββββββββ
β Date β Asset1 β Asset2 β
β --- β --- β --- β
β date β f64 β f64 β
ββββββββββββββͺβββββββββͺβββββββββ‘
β 2023-01-01 β 0.01 β 0.02 β
β 2023-01-02 β -0.02 β 0.01 β
β 2023-01-03 β 0.03 β -0.01 β
ββββββββββββββ΄βββββββββ΄βββββββββ
>>> # Basic usage
>>> data = build_data(returns=returns)
>>>
>>> # With benchmark and risk-free rate
>>> benchmark = pl.DataFrame({
... "Date": ["2023-01-01", "2023-01-02", "2023-01-03"],
... "Market": [0.005, -0.01, 0.02]
... }).with_columns(pl.col("Date").str.to_date())
>>> benchmark
shape: (3, 2)
ββββββββββββββ¬βββββββββ
β Date β Market β
β --- β --- β
β date β f64 β
ββββββββββββββͺβββββββββ‘
β 2023-01-01 β 0.005 β
β 2023-01-02 β -0.01 β
β 2023-01-03 β 0.02 β
ββββββββββββββ΄βββββββββ
>>> data = build_data(
... returns=returns,
... benchmark=benchmark,
... rf=0.0002, # risk-free rate (e.g., 0.02% per day)
... )
>>> # Calculate statistics
>>> sharpe = data.stats.sharpe()
>>> sharpe
{'Asset1': np.float64(4.909200099205072), 'Asset2': np.float64(8.08795106197808), 'Market': np.float64(6.113591415853696)}
>>> volatility = data.stats.volatility()
>>> volatility
{'Asset1': np.float64(0.4807979478602905), 'Asset2': np.float64(0.2918332857414772), 'Market': np.float64(0.286574597618142)}
>>> # Create visualizations
>>> fig = data.plots.plot_snapshot(title="Portfolio Performance")
>>> type(fig)
<class 'plotly.graph_objs._figure.Figure'>
>>> # End of exampleFor detailed documentation, visit jQuantStats Documentation.
- Python 3.10+
- numpy
- polars
- pandas
- plotly
- scipy
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE.txt file for details.