A Python package for financial risk metrics and stock data analysis
Project description
SquareQuant
SquareQuant is a comprehensive Python package for financial risk metrics and stock data analysis. It provides a suite of tools for quantitative finance, including risk metrics calculation and data retrieval from multiple sources.
Features
- Financial Data Access: Multiple data sources including YFinance and Theta Data
- Extensive Risk Metrics: Comprehensive set of traditional and advanced risk measures
- Modular Design: Reorganized into specialized modules (data, var, etc.)
- Flexible API: Consistent function-based and object-oriented interfaces
Installation
pip install squarequant
Quick Start
import squarequant as sq
import pandas as pd
import matplotlib.pyplot as plt
# Configure data download
config = sq.DownloadConfig(
start_date="2020-01-01",
end_date="2023-12-31",
interval='1d',
columns=['Close'],
source="yfinance" # Or "theta" for Theta Data
)
# Download data
data = sq.download_tickers(['AAPL', 'MSFT', 'GOOGL'], config)
# Calculate volatility
vol = sq.vol(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=21)
# Calculate Sharpe ratio
sharpe = sq.sharpe(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=252)
# Visualize results (using matplotlib directly)
plt.figure(figsize=(12, 6))
plt.plot(vol)
plt.title('21-Day Rolling Volatility')
plt.legend(vol.columns)
plt.show()
Available Risk Metrics
SquareQuant provides a comprehensive set of risk metrics, including:
| Metric | Function | Description |
|---|---|---|
| Sharpe Ratio | sq.sharpe() |
Risk-adjusted return using standard deviation |
| Sortino Ratio | sq.sortino() |
Risk-adjusted return using downside deviation |
| Volatility | sq.vol() |
Standard deviation of returns |
| Maximum Drawdown | sq.mdd() |
Largest peak-to-trough decline |
| Value at Risk | sq.var() |
Maximum expected loss at given confidence level |
| Conditional Value at Risk | sq.cvar() |
Expected loss beyond the VaR threshold |
| Semi-Deviation | sq.semidev() |
Downside volatility below target return |
| Average Drawdown | sq.avgdd() |
Mean of all drawdowns in a period |
| Ulcer Index | sq.ulcer() |
Square root of mean squared drawdown percentage |
| Mean Absolute Deviation | sq.mad() |
Average absolute deviation from mean |
| Entropic Risk Measure | sq.erm() |
Risk measure based on information theory |
| Entropic Value at Risk | sq.evar() |
VAR using entropy concepts |
| Conditional Drawdown at Risk | sq.cdar() |
Expected drawdown beyond threshold |
| Entropic Drawdown at Risk | sq.edar() |
Drawdown risk using entropy concepts |
Data Sources
SquareQuant now supports multiple data providers:
YFinance
The default data provider for historical price data:
config = sq.DownloadConfig(
start_date="2020-01-01",
end_date="2023-12-31",
source="yfinance" # Default
)
Theta Data
A new alternative data provider with the same interface:
config = sq.DownloadConfig(
start_date="2020-01-01",
end_date="2023-12-31",
source="theta"
)
Advanced Usage Examples
Analyzing Multiple Risk Metrics
import squarequant as sq
import pandas as pd
import matplotlib.pyplot as plt
# Download data
config = sq.DownloadConfig(
start_date="2018-01-01",
end_date="2023-12-31",
interval='1d'
)
data = sq.download_tickers(['SPY', 'QQQ'], config)
# Calculate multiple risk metrics
vol = sq.vol(data=data, assets=['SPY', 'QQQ'], window=21)
mad = sq.mad(data=data, assets=['SPY', 'QQQ'], window=252)
mdd = sq.mdd(data=data, assets=['SPY', 'QQQ'], window=252)
ulcer = sq.ulcer(data=data, assets=['SPY', 'QQQ'], window=252)
# Visualize comparison (using matplotlib directly)
plt.figure(figsize=(12, 8))
plt.subplot(3, 1, 1)
plt.plot(vol['SPY_Vol'])
plt.title('SPY Volatility')
plt.subplot(3, 1, 2)
plt.plot(mdd['SPY_MDD'])
plt.title('SPY Max Drawdown')
plt.subplot(3, 1, 3)
plt.plot(ulcer['SPY_Ulcer'])
plt.title('SPY Ulcer Index')
plt.tight_layout()
plt.show()
Portfolio Risk Analysis
import squarequant as sq
import pandas as pd
# Example portfolio weights
weights = pd.Series({'AAPL': 0.3, 'MSFT': 0.3, 'AMZN': 0.2, 'GOOGL': 0.2})
# Download data
config = sq.DownloadConfig(start_date="2020-01-01", end_date="2023-12-31")
data = sq.download_tickers(weights.index.tolist(), config)
# Calculate portfolio risk measures
portfolio_var = sq.var(
data=data,
assets=weights.index.tolist(),
confidence=0.95,
window=252,
holding_period=10
)
# Print results
print(f"Latest Portfolio VaR (95%): {portfolio_var.iloc[-1].values[0]:.4f}")
Using Advanced Risk Measures
import squarequant as sq
# Download data
config = sq.DownloadConfig(start_date="2018-01-01", end_date="2023-12-31")
data = sq.download_tickers(['SPY'], config)
# Calculate Entropic Risk Measure with different risk aversion parameters
erm1 = sq.erm(data=data, assets=['SPY'], z=0.5, confidence=0.95)
erm2 = sq.erm(data=data, assets=['SPY'], z=1.0, confidence=0.95)
erm3 = sq.erm(data=data, assets=['SPY'], z=2.0, confidence=0.95)
# Compare with traditional VaR and CVaR
var = sq.var(data=data, assets=['SPY'], confidence=0.95)
cvar = sq.cvar(data=data, assets=['SPY'], confidence=0.95)
print(f"Latest ERM (z=0.5): {erm1.iloc[-1, 0]:.4f}")
print(f"Latest ERM (z=1.0): {erm2.iloc[-1, 0]:.4f}")
print(f"Latest ERM (z=2.0): {erm3.iloc[-1, 0]:.4f}")
print(f"Latest VaR (95%): {var.iloc[-1, 0]:.4f}")
print(f"Latest CVaR (95%): {cvar.iloc[-1, 0]:.4f}")
Library Structure
The package has been reorganized into specialized modules:
squarequant.data: Data retrieval functionality (YFinance, Theta Data)squarequant.var: Value at Risk and related risk measuressquarequant.core: Core risk metrics and calculations
Documentation
For detailed documentation on each function and class, please see the official documentation.
License
MIT License
Credits
SquareQuant was developed to provide financial analysts, quantitative researchers, and portfolio managers with a comprehensive toolkit for risk assessment and performance analysis.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file squarequant-0.2.1.tar.gz.
File metadata
- Download URL: squarequant-0.2.1.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3aee34b31537538e2145b3cd415f6bf62bd72fbcac12b98a1b1ae93debd3c7bf
|
|
| MD5 |
186c805f837cca9918da04fa3bd25e5e
|
|
| BLAKE2b-256 |
0498b1b8aca7550289c9792715a10e41407349448cbbbb9bc88312b99f22b38d
|
File details
Details for the file squarequant-0.2.1-py3-none-any.whl.
File metadata
- Download URL: squarequant-0.2.1-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f60c7df94f4d692acc8818c7ecc23567a511582f5aaffaa2d1b0b37fd7ccf1a
|
|
| MD5 |
b3bd1578f35f265cff77dcd338b8d23d
|
|
| BLAKE2b-256 |
4650fc21ae766e9efead5b48f905b29acb269465bbb6a28d73c802ed4f44c30c
|