squarequant 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- squarequant-0.2.0/PKG-INFO +268 -0
- squarequant-0.2.0/README.md +210 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/setup.py +12 -5
- squarequant-0.2.0/squarequant/__init__.py +59 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant/api.py +85 -79
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant/constants.py +3 -3
- squarequant-0.2.0/squarequant/core/__init__.py +6 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant/core/metrics.py +310 -538
- squarequant-0.2.0/squarequant/data/__init__.py +6 -0
- squarequant-0.2.0/squarequant/data/data.py +250 -0
- squarequant-0.2.0/squarequant/var/__init__.py +6 -0
- squarequant-0.2.0/squarequant/var/tetstvar.py +241 -0
- squarequant-0.2.0/squarequant/var/valueatrisk.py +482 -0
- squarequant-0.2.0/squarequant.egg-info/PKG-INFO +268 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant.egg-info/SOURCES.txt +6 -4
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant.egg-info/requires.txt +5 -0
- squarequant-0.1.0/PKG-INFO +0 -217
- squarequant-0.1.0/README.md +0 -167
- squarequant-0.1.0/squarequant/__init__.py +0 -41
- squarequant-0.1.0/squarequant/core/__init__.py +0 -6
- squarequant-0.1.0/squarequant/core/data.py +0 -106
- squarequant-0.1.0/squarequant/plot/__init__.py +0 -7
- squarequant-0.1.0/squarequant/plot/plot.py +0 -972
- squarequant-0.1.0/squarequant.egg-info/PKG-INFO +0 -217
- {squarequant-0.1.0 → squarequant-0.2.0}/LICENSE +0 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/setup.cfg +0 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant/core/base.py +0 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant/utils/__init__.py +0 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant/utils/helpers.py +0 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant.egg-info/dependency_links.txt +0 -0
- {squarequant-0.1.0 → squarequant-0.2.0}/squarequant.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: squarequant
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A Python package for financial risk metrics and stock data analysis
|
|
5
|
+
Home-page: https://github.com/SquareQuant/squarequant-package
|
|
6
|
+
Author: Gabriel Bosch
|
|
7
|
+
Author-email: contact@squarequant.org
|
|
8
|
+
Project-URL: Documentation, https://www.squarequant.org
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/SquareQuant/squarequant-package/issues
|
|
10
|
+
Keywords: finance,risk,portfolio,investment,stocks,analysis,visualization,drawdown,var,cvar,entropic,ulcer index
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
21
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
22
|
+
Classifier: Intended Audience :: Science/Research
|
|
23
|
+
Classifier: Development Status :: 4 - Beta
|
|
24
|
+
Requires-Python: >=3.7
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: pandas>=1.0.0
|
|
28
|
+
Requires-Dist: numpy>=1.18.0
|
|
29
|
+
Requires-Dist: yfinance>=0.1.63
|
|
30
|
+
Requires-Dist: scipy>=1.5.0
|
|
31
|
+
Requires-Dist: scikit-learn>=1.5.0
|
|
32
|
+
Requires-Dist: matplotlib>=3.3.0
|
|
33
|
+
Requires-Dist: statsmodels>=0.12.0
|
|
34
|
+
Provides-Extra: optimization
|
|
35
|
+
Requires-Dist: cvxpy>=1.0.0; extra == "optimization"
|
|
36
|
+
Provides-Extra: dev
|
|
37
|
+
Requires-Dist: pytest>=6.0.0; extra == "dev"
|
|
38
|
+
Requires-Dist: black>=21.5b2; extra == "dev"
|
|
39
|
+
Requires-Dist: flake8>=3.9.0; extra == "dev"
|
|
40
|
+
Requires-Dist: sphinx>=4.0.0; extra == "dev"
|
|
41
|
+
Requires-Dist: sphinx-rtd-theme>=0.5.0; extra == "dev"
|
|
42
|
+
Requires-Dist: sphinx-autodoc-typehints>=1.22.0; extra == "dev"
|
|
43
|
+
Requires-Dist: myst-parser>=1.0.0; extra == "dev"
|
|
44
|
+
Requires-Dist: pytest-cov>=2.12.0; extra == "dev"
|
|
45
|
+
Dynamic: author
|
|
46
|
+
Dynamic: author-email
|
|
47
|
+
Dynamic: classifier
|
|
48
|
+
Dynamic: description
|
|
49
|
+
Dynamic: description-content-type
|
|
50
|
+
Dynamic: home-page
|
|
51
|
+
Dynamic: keywords
|
|
52
|
+
Dynamic: license-file
|
|
53
|
+
Dynamic: project-url
|
|
54
|
+
Dynamic: provides-extra
|
|
55
|
+
Dynamic: requires-dist
|
|
56
|
+
Dynamic: requires-python
|
|
57
|
+
Dynamic: summary
|
|
58
|
+
|
|
59
|
+
# SquareQuant
|
|
60
|
+
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
## Features
|
|
64
|
+
|
|
65
|
+
- **Financial Data Access**: Multiple data sources including YFinance and Theta Data
|
|
66
|
+
- **Extensive Risk Metrics**: Comprehensive set of traditional and advanced risk measures
|
|
67
|
+
- **Modular Design**: Reorganized into specialized modules (data, var, etc.)
|
|
68
|
+
- **Flexible API**: Consistent function-based and object-oriented interfaces
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install squarequant
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Quick Start
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import squarequant as sq
|
|
80
|
+
import pandas as pd
|
|
81
|
+
import matplotlib.pyplot as plt
|
|
82
|
+
|
|
83
|
+
# Configure data download
|
|
84
|
+
config = sq.DownloadConfig(
|
|
85
|
+
start_date="2020-01-01",
|
|
86
|
+
end_date="2023-12-31",
|
|
87
|
+
interval='1d',
|
|
88
|
+
columns=['Close'],
|
|
89
|
+
source="yfinance" # Or "theta" for Theta Data
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
# Download data
|
|
93
|
+
data = sq.download_tickers(['AAPL', 'MSFT', 'GOOGL'], config)
|
|
94
|
+
|
|
95
|
+
# Calculate volatility
|
|
96
|
+
vol = sq.vol(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=21)
|
|
97
|
+
|
|
98
|
+
# Calculate Sharpe ratio
|
|
99
|
+
sharpe = sq.sharpe(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=252)
|
|
100
|
+
|
|
101
|
+
# Visualize results (using matplotlib directly)
|
|
102
|
+
plt.figure(figsize=(12, 6))
|
|
103
|
+
plt.plot(vol)
|
|
104
|
+
plt.title('21-Day Rolling Volatility')
|
|
105
|
+
plt.legend(vol.columns)
|
|
106
|
+
plt.show()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Available Risk Metrics
|
|
110
|
+
|
|
111
|
+
SquareQuant provides a comprehensive set of risk metrics, including:
|
|
112
|
+
|
|
113
|
+
| Metric | Function | Description |
|
|
114
|
+
|--------|----------|-------------|
|
|
115
|
+
| Sharpe Ratio | `sq.sharpe()` | Risk-adjusted return using standard deviation |
|
|
116
|
+
| Sortino Ratio | `sq.sortino()` | Risk-adjusted return using downside deviation |
|
|
117
|
+
| Volatility | `sq.vol()` | Standard deviation of returns |
|
|
118
|
+
| Maximum Drawdown | `sq.mdd()` | Largest peak-to-trough decline |
|
|
119
|
+
| Value at Risk | `sq.var()` | Maximum expected loss at given confidence level |
|
|
120
|
+
| Conditional Value at Risk | `sq.cvar()` | Expected loss beyond the VaR threshold |
|
|
121
|
+
| Semi-Deviation | `sq.semidev()` | Downside volatility below target return |
|
|
122
|
+
| Average Drawdown | `sq.avgdd()` | Mean of all drawdowns in a period |
|
|
123
|
+
| Ulcer Index | `sq.ulcer()` | Square root of mean squared drawdown percentage |
|
|
124
|
+
| Mean Absolute Deviation | `sq.mad()` | Average absolute deviation from mean |
|
|
125
|
+
| Entropic Risk Measure | `sq.erm()` | Risk measure based on information theory |
|
|
126
|
+
| Entropic Value at Risk | `sq.evar()` | VAR using entropy concepts |
|
|
127
|
+
| Conditional Drawdown at Risk | `sq.cdar()` | Expected drawdown beyond threshold |
|
|
128
|
+
| Entropic Drawdown at Risk | `sq.edar()` | Drawdown risk using entropy concepts |
|
|
129
|
+
|
|
130
|
+
## Data Sources
|
|
131
|
+
|
|
132
|
+
SquareQuant now supports multiple data providers:
|
|
133
|
+
|
|
134
|
+
### YFinance
|
|
135
|
+
|
|
136
|
+
The default data provider for historical price data:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
config = sq.DownloadConfig(
|
|
140
|
+
start_date="2020-01-01",
|
|
141
|
+
end_date="2023-12-31",
|
|
142
|
+
source="yfinance" # Default
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Theta Data
|
|
147
|
+
|
|
148
|
+
A new alternative data provider with the same interface:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
config = sq.DownloadConfig(
|
|
152
|
+
start_date="2020-01-01",
|
|
153
|
+
end_date="2023-12-31",
|
|
154
|
+
source="theta"
|
|
155
|
+
)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Advanced Usage Examples
|
|
159
|
+
|
|
160
|
+
### Analyzing Multiple Risk Metrics
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
import squarequant as sq
|
|
164
|
+
import pandas as pd
|
|
165
|
+
import matplotlib.pyplot as plt
|
|
166
|
+
|
|
167
|
+
# Download data
|
|
168
|
+
config = sq.DownloadConfig(
|
|
169
|
+
start_date="2018-01-01",
|
|
170
|
+
end_date="2023-12-31",
|
|
171
|
+
interval='1d'
|
|
172
|
+
)
|
|
173
|
+
data = sq.download_tickers(['SPY', 'QQQ'], config)
|
|
174
|
+
|
|
175
|
+
# Calculate multiple risk metrics
|
|
176
|
+
vol = sq.vol(data=data, assets=['SPY', 'QQQ'], window=21)
|
|
177
|
+
mad = sq.mad(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
178
|
+
mdd = sq.mdd(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
179
|
+
ulcer = sq.ulcer(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
180
|
+
|
|
181
|
+
# Visualize comparison (using matplotlib directly)
|
|
182
|
+
plt.figure(figsize=(12, 8))
|
|
183
|
+
plt.subplot(3, 1, 1)
|
|
184
|
+
plt.plot(vol['SPY_Vol'])
|
|
185
|
+
plt.title('SPY Volatility')
|
|
186
|
+
|
|
187
|
+
plt.subplot(3, 1, 2)
|
|
188
|
+
plt.plot(mdd['SPY_MDD'])
|
|
189
|
+
plt.title('SPY Max Drawdown')
|
|
190
|
+
|
|
191
|
+
plt.subplot(3, 1, 3)
|
|
192
|
+
plt.plot(ulcer['SPY_Ulcer'])
|
|
193
|
+
plt.title('SPY Ulcer Index')
|
|
194
|
+
|
|
195
|
+
plt.tight_layout()
|
|
196
|
+
plt.show()
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Portfolio Risk Analysis
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
import squarequant as sq
|
|
203
|
+
import pandas as pd
|
|
204
|
+
|
|
205
|
+
# Example portfolio weights
|
|
206
|
+
weights = pd.Series({'AAPL': 0.3, 'MSFT': 0.3, 'AMZN': 0.2, 'GOOGL': 0.2})
|
|
207
|
+
|
|
208
|
+
# Download data
|
|
209
|
+
config = sq.DownloadConfig(start_date="2020-01-01", end_date="2023-12-31")
|
|
210
|
+
data = sq.download_tickers(weights.index.tolist(), config)
|
|
211
|
+
|
|
212
|
+
# Calculate portfolio risk measures
|
|
213
|
+
portfolio_var = sq.var(
|
|
214
|
+
data=data,
|
|
215
|
+
assets=weights.index.tolist(),
|
|
216
|
+
confidence=0.95,
|
|
217
|
+
window=252,
|
|
218
|
+
holding_period=10
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
# Print results
|
|
222
|
+
print(f"Latest Portfolio VaR (95%): {portfolio_var.iloc[-1].values[0]:.4f}")
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Using Advanced Risk Measures
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
import squarequant as sq
|
|
229
|
+
|
|
230
|
+
# Download data
|
|
231
|
+
config = sq.DownloadConfig(start_date="2018-01-01", end_date="2023-12-31")
|
|
232
|
+
data = sq.download_tickers(['SPY'], config)
|
|
233
|
+
|
|
234
|
+
# Calculate Entropic Risk Measure with different risk aversion parameters
|
|
235
|
+
erm1 = sq.erm(data=data, assets=['SPY'], z=0.5, confidence=0.95)
|
|
236
|
+
erm2 = sq.erm(data=data, assets=['SPY'], z=1.0, confidence=0.95)
|
|
237
|
+
erm3 = sq.erm(data=data, assets=['SPY'], z=2.0, confidence=0.95)
|
|
238
|
+
|
|
239
|
+
# Compare with traditional VaR and CVaR
|
|
240
|
+
var = sq.var(data=data, assets=['SPY'], confidence=0.95)
|
|
241
|
+
cvar = sq.cvar(data=data, assets=['SPY'], confidence=0.95)
|
|
242
|
+
|
|
243
|
+
print(f"Latest ERM (z=0.5): {erm1.iloc[-1, 0]:.4f}")
|
|
244
|
+
print(f"Latest ERM (z=1.0): {erm2.iloc[-1, 0]:.4f}")
|
|
245
|
+
print(f"Latest ERM (z=2.0): {erm3.iloc[-1, 0]:.4f}")
|
|
246
|
+
print(f"Latest VaR (95%): {var.iloc[-1, 0]:.4f}")
|
|
247
|
+
print(f"Latest CVaR (95%): {cvar.iloc[-1, 0]:.4f}")
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Library Structure
|
|
251
|
+
|
|
252
|
+
The package has been reorganized into specialized modules:
|
|
253
|
+
|
|
254
|
+
- `squarequant.data`: Data retrieval functionality (YFinance, Theta Data)
|
|
255
|
+
- `squarequant.var`: Value at Risk and related risk measures
|
|
256
|
+
- `squarequant.core`: Core risk metrics and calculations
|
|
257
|
+
|
|
258
|
+
## Documentation
|
|
259
|
+
|
|
260
|
+
For detailed documentation on each function and class, please see the [official documentation](https://www.squarequant.org).
|
|
261
|
+
|
|
262
|
+
## License
|
|
263
|
+
|
|
264
|
+
MIT License
|
|
265
|
+
|
|
266
|
+
## Credits
|
|
267
|
+
|
|
268
|
+
SquareQuant was developed to provide financial analysts, quantitative researchers, and portfolio managers with a comprehensive toolkit for risk assessment and performance analysis.
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# SquareQuant
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Financial Data Access**: Multiple data sources including YFinance and Theta Data
|
|
8
|
+
- **Extensive Risk Metrics**: Comprehensive set of traditional and advanced risk measures
|
|
9
|
+
- **Modular Design**: Reorganized into specialized modules (data, var, etc.)
|
|
10
|
+
- **Flexible API**: Consistent function-based and object-oriented interfaces
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install squarequant
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import squarequant as sq
|
|
22
|
+
import pandas as pd
|
|
23
|
+
import matplotlib.pyplot as plt
|
|
24
|
+
|
|
25
|
+
# Configure data download
|
|
26
|
+
config = sq.DownloadConfig(
|
|
27
|
+
start_date="2020-01-01",
|
|
28
|
+
end_date="2023-12-31",
|
|
29
|
+
interval='1d',
|
|
30
|
+
columns=['Close'],
|
|
31
|
+
source="yfinance" # Or "theta" for Theta Data
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# Download data
|
|
35
|
+
data = sq.download_tickers(['AAPL', 'MSFT', 'GOOGL'], config)
|
|
36
|
+
|
|
37
|
+
# Calculate volatility
|
|
38
|
+
vol = sq.vol(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=21)
|
|
39
|
+
|
|
40
|
+
# Calculate Sharpe ratio
|
|
41
|
+
sharpe = sq.sharpe(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=252)
|
|
42
|
+
|
|
43
|
+
# Visualize results (using matplotlib directly)
|
|
44
|
+
plt.figure(figsize=(12, 6))
|
|
45
|
+
plt.plot(vol)
|
|
46
|
+
plt.title('21-Day Rolling Volatility')
|
|
47
|
+
plt.legend(vol.columns)
|
|
48
|
+
plt.show()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Available Risk Metrics
|
|
52
|
+
|
|
53
|
+
SquareQuant provides a comprehensive set of risk metrics, including:
|
|
54
|
+
|
|
55
|
+
| Metric | Function | Description |
|
|
56
|
+
|--------|----------|-------------|
|
|
57
|
+
| Sharpe Ratio | `sq.sharpe()` | Risk-adjusted return using standard deviation |
|
|
58
|
+
| Sortino Ratio | `sq.sortino()` | Risk-adjusted return using downside deviation |
|
|
59
|
+
| Volatility | `sq.vol()` | Standard deviation of returns |
|
|
60
|
+
| Maximum Drawdown | `sq.mdd()` | Largest peak-to-trough decline |
|
|
61
|
+
| Value at Risk | `sq.var()` | Maximum expected loss at given confidence level |
|
|
62
|
+
| Conditional Value at Risk | `sq.cvar()` | Expected loss beyond the VaR threshold |
|
|
63
|
+
| Semi-Deviation | `sq.semidev()` | Downside volatility below target return |
|
|
64
|
+
| Average Drawdown | `sq.avgdd()` | Mean of all drawdowns in a period |
|
|
65
|
+
| Ulcer Index | `sq.ulcer()` | Square root of mean squared drawdown percentage |
|
|
66
|
+
| Mean Absolute Deviation | `sq.mad()` | Average absolute deviation from mean |
|
|
67
|
+
| Entropic Risk Measure | `sq.erm()` | Risk measure based on information theory |
|
|
68
|
+
| Entropic Value at Risk | `sq.evar()` | VAR using entropy concepts |
|
|
69
|
+
| Conditional Drawdown at Risk | `sq.cdar()` | Expected drawdown beyond threshold |
|
|
70
|
+
| Entropic Drawdown at Risk | `sq.edar()` | Drawdown risk using entropy concepts |
|
|
71
|
+
|
|
72
|
+
## Data Sources
|
|
73
|
+
|
|
74
|
+
SquareQuant now supports multiple data providers:
|
|
75
|
+
|
|
76
|
+
### YFinance
|
|
77
|
+
|
|
78
|
+
The default data provider for historical price data:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
config = sq.DownloadConfig(
|
|
82
|
+
start_date="2020-01-01",
|
|
83
|
+
end_date="2023-12-31",
|
|
84
|
+
source="yfinance" # Default
|
|
85
|
+
)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Theta Data
|
|
89
|
+
|
|
90
|
+
A new alternative data provider with the same interface:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
config = sq.DownloadConfig(
|
|
94
|
+
start_date="2020-01-01",
|
|
95
|
+
end_date="2023-12-31",
|
|
96
|
+
source="theta"
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Advanced Usage Examples
|
|
101
|
+
|
|
102
|
+
### Analyzing Multiple Risk Metrics
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
import squarequant as sq
|
|
106
|
+
import pandas as pd
|
|
107
|
+
import matplotlib.pyplot as plt
|
|
108
|
+
|
|
109
|
+
# Download data
|
|
110
|
+
config = sq.DownloadConfig(
|
|
111
|
+
start_date="2018-01-01",
|
|
112
|
+
end_date="2023-12-31",
|
|
113
|
+
interval='1d'
|
|
114
|
+
)
|
|
115
|
+
data = sq.download_tickers(['SPY', 'QQQ'], config)
|
|
116
|
+
|
|
117
|
+
# Calculate multiple risk metrics
|
|
118
|
+
vol = sq.vol(data=data, assets=['SPY', 'QQQ'], window=21)
|
|
119
|
+
mad = sq.mad(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
120
|
+
mdd = sq.mdd(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
121
|
+
ulcer = sq.ulcer(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
122
|
+
|
|
123
|
+
# Visualize comparison (using matplotlib directly)
|
|
124
|
+
plt.figure(figsize=(12, 8))
|
|
125
|
+
plt.subplot(3, 1, 1)
|
|
126
|
+
plt.plot(vol['SPY_Vol'])
|
|
127
|
+
plt.title('SPY Volatility')
|
|
128
|
+
|
|
129
|
+
plt.subplot(3, 1, 2)
|
|
130
|
+
plt.plot(mdd['SPY_MDD'])
|
|
131
|
+
plt.title('SPY Max Drawdown')
|
|
132
|
+
|
|
133
|
+
plt.subplot(3, 1, 3)
|
|
134
|
+
plt.plot(ulcer['SPY_Ulcer'])
|
|
135
|
+
plt.title('SPY Ulcer Index')
|
|
136
|
+
|
|
137
|
+
plt.tight_layout()
|
|
138
|
+
plt.show()
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Portfolio Risk Analysis
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
import squarequant as sq
|
|
145
|
+
import pandas as pd
|
|
146
|
+
|
|
147
|
+
# Example portfolio weights
|
|
148
|
+
weights = pd.Series({'AAPL': 0.3, 'MSFT': 0.3, 'AMZN': 0.2, 'GOOGL': 0.2})
|
|
149
|
+
|
|
150
|
+
# Download data
|
|
151
|
+
config = sq.DownloadConfig(start_date="2020-01-01", end_date="2023-12-31")
|
|
152
|
+
data = sq.download_tickers(weights.index.tolist(), config)
|
|
153
|
+
|
|
154
|
+
# Calculate portfolio risk measures
|
|
155
|
+
portfolio_var = sq.var(
|
|
156
|
+
data=data,
|
|
157
|
+
assets=weights.index.tolist(),
|
|
158
|
+
confidence=0.95,
|
|
159
|
+
window=252,
|
|
160
|
+
holding_period=10
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
# Print results
|
|
164
|
+
print(f"Latest Portfolio VaR (95%): {portfolio_var.iloc[-1].values[0]:.4f}")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Using Advanced Risk Measures
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import squarequant as sq
|
|
171
|
+
|
|
172
|
+
# Download data
|
|
173
|
+
config = sq.DownloadConfig(start_date="2018-01-01", end_date="2023-12-31")
|
|
174
|
+
data = sq.download_tickers(['SPY'], config)
|
|
175
|
+
|
|
176
|
+
# Calculate Entropic Risk Measure with different risk aversion parameters
|
|
177
|
+
erm1 = sq.erm(data=data, assets=['SPY'], z=0.5, confidence=0.95)
|
|
178
|
+
erm2 = sq.erm(data=data, assets=['SPY'], z=1.0, confidence=0.95)
|
|
179
|
+
erm3 = sq.erm(data=data, assets=['SPY'], z=2.0, confidence=0.95)
|
|
180
|
+
|
|
181
|
+
# Compare with traditional VaR and CVaR
|
|
182
|
+
var = sq.var(data=data, assets=['SPY'], confidence=0.95)
|
|
183
|
+
cvar = sq.cvar(data=data, assets=['SPY'], confidence=0.95)
|
|
184
|
+
|
|
185
|
+
print(f"Latest ERM (z=0.5): {erm1.iloc[-1, 0]:.4f}")
|
|
186
|
+
print(f"Latest ERM (z=1.0): {erm2.iloc[-1, 0]:.4f}")
|
|
187
|
+
print(f"Latest ERM (z=2.0): {erm3.iloc[-1, 0]:.4f}")
|
|
188
|
+
print(f"Latest VaR (95%): {var.iloc[-1, 0]:.4f}")
|
|
189
|
+
print(f"Latest CVaR (95%): {cvar.iloc[-1, 0]:.4f}")
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Library Structure
|
|
193
|
+
|
|
194
|
+
The package has been reorganized into specialized modules:
|
|
195
|
+
|
|
196
|
+
- `squarequant.data`: Data retrieval functionality (YFinance, Theta Data)
|
|
197
|
+
- `squarequant.var`: Value at Risk and related risk measures
|
|
198
|
+
- `squarequant.core`: Core risk metrics and calculations
|
|
199
|
+
|
|
200
|
+
## Documentation
|
|
201
|
+
|
|
202
|
+
For detailed documentation on each function and class, please see the [official documentation](https://www.squarequant.org).
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
MIT License
|
|
207
|
+
|
|
208
|
+
## Credits
|
|
209
|
+
|
|
210
|
+
SquareQuant was developed to provide financial analysts, quantitative researchers, and portfolio managers with a comprehensive toolkit for risk assessment and performance analysis.
|
|
@@ -5,15 +5,15 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|
|
5
5
|
|
|
6
6
|
setup(
|
|
7
7
|
name="squarequant",
|
|
8
|
-
version="0.
|
|
8
|
+
version="0.2.0",
|
|
9
9
|
author="Gabriel Bosch",
|
|
10
|
-
author_email="contact@
|
|
11
|
-
description="A Python package for
|
|
10
|
+
author_email="contact@squarequant.org",
|
|
11
|
+
description="A Python package for financial risk metrics and stock data analysis",
|
|
12
12
|
long_description=long_description,
|
|
13
13
|
long_description_content_type="text/markdown",
|
|
14
14
|
url="https://github.com/SquareQuant/squarequant-package",
|
|
15
15
|
project_urls={
|
|
16
|
-
"Documentation": "https://squarequant.
|
|
16
|
+
"Documentation": "https://www.squarequant.org",
|
|
17
17
|
"Bug Tracker": "https://github.com/SquareQuant/squarequant-package/issues",
|
|
18
18
|
},
|
|
19
19
|
packages=find_packages(),
|
|
@@ -22,6 +22,8 @@ setup(
|
|
|
22
22
|
"Programming Language :: Python :: 3.7",
|
|
23
23
|
"Programming Language :: Python :: 3.8",
|
|
24
24
|
"Programming Language :: Python :: 3.9",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
25
27
|
"License :: OSI Approved :: MIT License",
|
|
26
28
|
"Operating System :: OS Independent",
|
|
27
29
|
"Topic :: Office/Business :: Financial :: Investment",
|
|
@@ -36,7 +38,9 @@ setup(
|
|
|
36
38
|
"numpy>=1.18.0",
|
|
37
39
|
"yfinance>=0.1.63",
|
|
38
40
|
"scipy>=1.5.0",
|
|
41
|
+
"scikit-learn>=1.5.0",
|
|
39
42
|
"matplotlib>=3.3.0",
|
|
43
|
+
"statsmodels>=0.12.0",
|
|
40
44
|
],
|
|
41
45
|
extras_require={
|
|
42
46
|
"optimization": ["cvxpy>=1.0.0"],
|
|
@@ -46,7 +50,10 @@ setup(
|
|
|
46
50
|
"flake8>=3.9.0",
|
|
47
51
|
"sphinx>=4.0.0",
|
|
48
52
|
"sphinx-rtd-theme>=0.5.0",
|
|
53
|
+
"sphinx-autodoc-typehints>=1.22.0",
|
|
54
|
+
"myst-parser>=1.0.0",
|
|
55
|
+
"pytest-cov>=2.12.0",
|
|
49
56
|
],
|
|
50
57
|
},
|
|
51
|
-
keywords="finance, risk, portfolio, investment, stocks, analysis, visualization",
|
|
58
|
+
keywords="finance, risk, portfolio, investment, stocks, analysis, visualization, drawdown, var, cvar, entropic, ulcer index",
|
|
52
59
|
)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
with open("README.md", "r", encoding="utf-8") as fh:
|
|
4
|
+
long_description = fh.read()
|
|
5
|
+
|
|
6
|
+
setup(
|
|
7
|
+
name="squarequant",
|
|
8
|
+
version="0.2.0", # Updated version
|
|
9
|
+
author="Gabriel Bosch",
|
|
10
|
+
author_email="contact@squarequant.org",
|
|
11
|
+
description="A Python package for financial risk metrics and stock data analysis",
|
|
12
|
+
long_description=long_description,
|
|
13
|
+
long_description_content_type="text/markdown",
|
|
14
|
+
url="https://github.com/SquareQuant/squarequant-package",
|
|
15
|
+
project_urls={
|
|
16
|
+
"Documentation": "https://www.squarequant.org",
|
|
17
|
+
"Bug Tracker": "https://github.com/SquareQuant/squarequant-package/issues",
|
|
18
|
+
},
|
|
19
|
+
packages=find_packages(),
|
|
20
|
+
classifiers=[
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.7",
|
|
23
|
+
"Programming Language :: Python :: 3.8",
|
|
24
|
+
"Programming Language :: Python :: 3.9",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Topic :: Office/Business :: Financial :: Investment",
|
|
30
|
+
"Topic :: Scientific/Engineering :: Information Analysis",
|
|
31
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
32
|
+
"Intended Audience :: Science/Research",
|
|
33
|
+
"Development Status :: 4 - Beta",
|
|
34
|
+
],
|
|
35
|
+
python_requires=">=3.7",
|
|
36
|
+
install_requires=[
|
|
37
|
+
"pandas>=1.0.0",
|
|
38
|
+
"numpy>=1.18.0",
|
|
39
|
+
"yfinance>=0.1.63",
|
|
40
|
+
"scipy>=1.5.0",
|
|
41
|
+
"scikit-learn>=1.5.0",
|
|
42
|
+
"requests>=2.25.0", # Added for Theta data provider
|
|
43
|
+
"statsmodels>=0.12.0",
|
|
44
|
+
],
|
|
45
|
+
extras_require={
|
|
46
|
+
"optimization": ["cvxpy>=1.0.0"],
|
|
47
|
+
"dev": [
|
|
48
|
+
"pytest>=6.0.0",
|
|
49
|
+
"black>=21.5b2",
|
|
50
|
+
"flake8>=3.9.0",
|
|
51
|
+
"sphinx>=4.0.0",
|
|
52
|
+
"sphinx-rtd-theme>=0.5.0",
|
|
53
|
+
"sphinx-autodoc-typehints>=1.22.0",
|
|
54
|
+
"myst-parser>=1.0.0",
|
|
55
|
+
"pytest-cov>=2.12.0",
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
keywords="finance, risk, portfolio, investment, stocks, analysis, var, cvar, entropic, ulcer index, drawdown, theta data",
|
|
59
|
+
)
|