squarequant 0.1.1__tar.gz → 0.2.1__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.1.1/squarequant.egg-info → squarequant-0.2.1}/PKG-INFO +66 -18
- {squarequant-0.1.1 → squarequant-0.2.1}/README.md +62 -14
- {squarequant-0.1.1 → squarequant-0.2.1}/setup.py +4 -3
- squarequant-0.2.1/squarequant/__init__.py +52 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/api.py +34 -43
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/constants.py +2 -2
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/core/metrics.py +1 -224
- squarequant-0.2.1/squarequant/data/__init__.py +6 -0
- squarequant-0.2.1/squarequant/data/data.py +250 -0
- squarequant-0.2.1/squarequant/var/__init__.py +6 -0
- squarequant-0.2.1/squarequant/var/tetstvar.py +241 -0
- squarequant-0.2.1/squarequant/var/valueatrisk.py +482 -0
- {squarequant-0.1.1 → squarequant-0.2.1/squarequant.egg-info}/PKG-INFO +66 -18
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant.egg-info/SOURCES.txt +6 -4
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant.egg-info/requires.txt +2 -3
- squarequant-0.1.1/squarequant/__init__.py +0 -41
- squarequant-0.1.1/squarequant/core/data.py +0 -106
- squarequant-0.1.1/squarequant/plot/__init__.py +0 -7
- squarequant-0.1.1/squarequant/plot/plot.py +0 -972
- {squarequant-0.1.1 → squarequant-0.2.1}/LICENSE +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/setup.cfg +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/core/__init__.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/core/base.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/utils/__init__.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant/utils/helpers.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant.egg-info/dependency_links.txt +0 -0
- {squarequant-0.1.1 → squarequant-0.2.1}/squarequant.egg-info/top_level.txt +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: squarequant
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A Python package for financial risk metrics and stock data analysis
|
|
5
5
|
Home-page: https://github.com/SquareQuant/squarequant-package
|
|
6
6
|
Author: Gabriel Bosch
|
|
7
7
|
Author-email: contact@squarequant.org
|
|
8
8
|
Project-URL: Documentation, https://www.squarequant.org
|
|
9
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
|
|
10
|
+
Keywords: finance,risk,portfolio,investment,stocks,analysis,visualization,drawdown,var,cvar,entropic,ulcer index,theta data
|
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.7
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.8
|
|
@@ -28,12 +28,12 @@ Requires-Dist: pandas>=1.0.0
|
|
|
28
28
|
Requires-Dist: numpy>=1.18.0
|
|
29
29
|
Requires-Dist: yfinance>=0.1.63
|
|
30
30
|
Requires-Dist: scipy>=1.5.0
|
|
31
|
+
Requires-Dist: scikit-learn>=1.5.0
|
|
31
32
|
Requires-Dist: matplotlib>=3.3.0
|
|
32
33
|
Requires-Dist: statsmodels>=0.12.0
|
|
34
|
+
Requires-Dist: requests>=2.25.0
|
|
33
35
|
Provides-Extra: optimization
|
|
34
36
|
Requires-Dist: cvxpy>=1.0.0; extra == "optimization"
|
|
35
|
-
Provides-Extra: advanced
|
|
36
|
-
Requires-Dist: scikit-learn>=1.0.0; extra == "advanced"
|
|
37
37
|
Provides-Extra: dev
|
|
38
38
|
Requires-Dist: pytest>=6.0.0; extra == "dev"
|
|
39
39
|
Requires-Dist: black>=21.5b2; extra == "dev"
|
|
@@ -59,14 +59,13 @@ Dynamic: summary
|
|
|
59
59
|
|
|
60
60
|
# SquareQuant
|
|
61
61
|
|
|
62
|
-
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
|
|
62
|
+
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.
|
|
63
63
|
|
|
64
64
|
## Features
|
|
65
65
|
|
|
66
|
-
- **Financial Data Access**:
|
|
66
|
+
- **Financial Data Access**: Multiple data sources including YFinance and Theta Data
|
|
67
67
|
- **Extensive Risk Metrics**: Comprehensive set of traditional and advanced risk measures
|
|
68
|
-
- **
|
|
69
|
-
- **Visualization**: Built-in plotting functions for visual analysis
|
|
68
|
+
- **Modular Design**: Reorganized into specialized modules (data, var, etc.)
|
|
70
69
|
- **Flexible API**: Consistent function-based and object-oriented interfaces
|
|
71
70
|
|
|
72
71
|
## Installation
|
|
@@ -79,6 +78,7 @@ pip install squarequant
|
|
|
79
78
|
|
|
80
79
|
```python
|
|
81
80
|
import squarequant as sq
|
|
81
|
+
import pandas as pd
|
|
82
82
|
import matplotlib.pyplot as plt
|
|
83
83
|
|
|
84
84
|
# Configure data download
|
|
@@ -86,7 +86,8 @@ config = sq.DownloadConfig(
|
|
|
86
86
|
start_date="2020-01-01",
|
|
87
87
|
end_date="2023-12-31",
|
|
88
88
|
interval='1d',
|
|
89
|
-
columns=['Close']
|
|
89
|
+
columns=['Close'],
|
|
90
|
+
source="yfinance" # Or "theta" for Theta Data
|
|
90
91
|
)
|
|
91
92
|
|
|
92
93
|
# Download data
|
|
@@ -98,7 +99,7 @@ vol = sq.vol(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=21)
|
|
|
98
99
|
# Calculate Sharpe ratio
|
|
99
100
|
sharpe = sq.sharpe(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=252)
|
|
100
101
|
|
|
101
|
-
# Visualize results
|
|
102
|
+
# Visualize results (using matplotlib directly)
|
|
102
103
|
plt.figure(figsize=(12, 6))
|
|
103
104
|
plt.plot(vol)
|
|
104
105
|
plt.title('21-Day Rolling Volatility')
|
|
@@ -127,12 +128,41 @@ SquareQuant provides a comprehensive set of risk metrics, including:
|
|
|
127
128
|
| Conditional Drawdown at Risk | `sq.cdar()` | Expected drawdown beyond threshold |
|
|
128
129
|
| Entropic Drawdown at Risk | `sq.edar()` | Drawdown risk using entropy concepts |
|
|
129
130
|
|
|
131
|
+
## Data Sources
|
|
132
|
+
|
|
133
|
+
SquareQuant now supports multiple data providers:
|
|
134
|
+
|
|
135
|
+
### YFinance
|
|
136
|
+
|
|
137
|
+
The default data provider for historical price data:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
config = sq.DownloadConfig(
|
|
141
|
+
start_date="2020-01-01",
|
|
142
|
+
end_date="2023-12-31",
|
|
143
|
+
source="yfinance" # Default
|
|
144
|
+
)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Theta Data
|
|
148
|
+
|
|
149
|
+
A new alternative data provider with the same interface:
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
config = sq.DownloadConfig(
|
|
153
|
+
start_date="2020-01-01",
|
|
154
|
+
end_date="2023-12-31",
|
|
155
|
+
source="theta"
|
|
156
|
+
)
|
|
157
|
+
```
|
|
158
|
+
|
|
130
159
|
## Advanced Usage Examples
|
|
131
160
|
|
|
132
161
|
### Analyzing Multiple Risk Metrics
|
|
133
162
|
|
|
134
163
|
```python
|
|
135
164
|
import squarequant as sq
|
|
165
|
+
import pandas as pd
|
|
136
166
|
import matplotlib.pyplot as plt
|
|
137
167
|
|
|
138
168
|
# Download data
|
|
@@ -149,12 +179,22 @@ mad = sq.mad(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
|
149
179
|
mdd = sq.mdd(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
150
180
|
ulcer = sq.ulcer(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
151
181
|
|
|
152
|
-
# Visualize comparison
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
182
|
+
# Visualize comparison (using matplotlib directly)
|
|
183
|
+
plt.figure(figsize=(12, 8))
|
|
184
|
+
plt.subplot(3, 1, 1)
|
|
185
|
+
plt.plot(vol['SPY_Vol'])
|
|
186
|
+
plt.title('SPY Volatility')
|
|
187
|
+
|
|
188
|
+
plt.subplot(3, 1, 2)
|
|
189
|
+
plt.plot(mdd['SPY_MDD'])
|
|
190
|
+
plt.title('SPY Max Drawdown')
|
|
191
|
+
|
|
192
|
+
plt.subplot(3, 1, 3)
|
|
193
|
+
plt.plot(ulcer['SPY_Ulcer'])
|
|
194
|
+
plt.title('SPY Ulcer Index')
|
|
195
|
+
|
|
196
|
+
plt.tight_layout()
|
|
197
|
+
plt.show()
|
|
158
198
|
```
|
|
159
199
|
|
|
160
200
|
### Portfolio Risk Analysis
|
|
@@ -179,8 +219,8 @@ portfolio_var = sq.var(
|
|
|
179
219
|
holding_period=10
|
|
180
220
|
)
|
|
181
221
|
|
|
182
|
-
#
|
|
183
|
-
|
|
222
|
+
# Print results
|
|
223
|
+
print(f"Latest Portfolio VaR (95%): {portfolio_var.iloc[-1].values[0]:.4f}")
|
|
184
224
|
```
|
|
185
225
|
|
|
186
226
|
### Using Advanced Risk Measures
|
|
@@ -208,6 +248,14 @@ print(f"Latest VaR (95%): {var.iloc[-1, 0]:.4f}")
|
|
|
208
248
|
print(f"Latest CVaR (95%): {cvar.iloc[-1, 0]:.4f}")
|
|
209
249
|
```
|
|
210
250
|
|
|
251
|
+
## Library Structure
|
|
252
|
+
|
|
253
|
+
The package has been reorganized into specialized modules:
|
|
254
|
+
|
|
255
|
+
- `squarequant.data`: Data retrieval functionality (YFinance, Theta Data)
|
|
256
|
+
- `squarequant.var`: Value at Risk and related risk measures
|
|
257
|
+
- `squarequant.core`: Core risk metrics and calculations
|
|
258
|
+
|
|
211
259
|
## Documentation
|
|
212
260
|
|
|
213
261
|
For detailed documentation on each function and class, please see the [official documentation](https://www.squarequant.org).
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# SquareQuant
|
|
2
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
|
|
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
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **Financial Data Access**:
|
|
7
|
+
- **Financial Data Access**: Multiple data sources including YFinance and Theta Data
|
|
8
8
|
- **Extensive Risk Metrics**: Comprehensive set of traditional and advanced risk measures
|
|
9
|
-
- **
|
|
10
|
-
- **Visualization**: Built-in plotting functions for visual analysis
|
|
9
|
+
- **Modular Design**: Reorganized into specialized modules (data, var, etc.)
|
|
11
10
|
- **Flexible API**: Consistent function-based and object-oriented interfaces
|
|
12
11
|
|
|
13
12
|
## Installation
|
|
@@ -20,6 +19,7 @@ pip install squarequant
|
|
|
20
19
|
|
|
21
20
|
```python
|
|
22
21
|
import squarequant as sq
|
|
22
|
+
import pandas as pd
|
|
23
23
|
import matplotlib.pyplot as plt
|
|
24
24
|
|
|
25
25
|
# Configure data download
|
|
@@ -27,7 +27,8 @@ config = sq.DownloadConfig(
|
|
|
27
27
|
start_date="2020-01-01",
|
|
28
28
|
end_date="2023-12-31",
|
|
29
29
|
interval='1d',
|
|
30
|
-
columns=['Close']
|
|
30
|
+
columns=['Close'],
|
|
31
|
+
source="yfinance" # Or "theta" for Theta Data
|
|
31
32
|
)
|
|
32
33
|
|
|
33
34
|
# Download data
|
|
@@ -39,7 +40,7 @@ vol = sq.vol(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=21)
|
|
|
39
40
|
# Calculate Sharpe ratio
|
|
40
41
|
sharpe = sq.sharpe(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=252)
|
|
41
42
|
|
|
42
|
-
# Visualize results
|
|
43
|
+
# Visualize results (using matplotlib directly)
|
|
43
44
|
plt.figure(figsize=(12, 6))
|
|
44
45
|
plt.plot(vol)
|
|
45
46
|
plt.title('21-Day Rolling Volatility')
|
|
@@ -68,12 +69,41 @@ SquareQuant provides a comprehensive set of risk metrics, including:
|
|
|
68
69
|
| Conditional Drawdown at Risk | `sq.cdar()` | Expected drawdown beyond threshold |
|
|
69
70
|
| Entropic Drawdown at Risk | `sq.edar()` | Drawdown risk using entropy concepts |
|
|
70
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
|
+
|
|
71
100
|
## Advanced Usage Examples
|
|
72
101
|
|
|
73
102
|
### Analyzing Multiple Risk Metrics
|
|
74
103
|
|
|
75
104
|
```python
|
|
76
105
|
import squarequant as sq
|
|
106
|
+
import pandas as pd
|
|
77
107
|
import matplotlib.pyplot as plt
|
|
78
108
|
|
|
79
109
|
# Download data
|
|
@@ -90,12 +120,22 @@ mad = sq.mad(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
|
90
120
|
mdd = sq.mdd(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
91
121
|
ulcer = sq.ulcer(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
92
122
|
|
|
93
|
-
# Visualize comparison
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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()
|
|
99
139
|
```
|
|
100
140
|
|
|
101
141
|
### Portfolio Risk Analysis
|
|
@@ -120,8 +160,8 @@ portfolio_var = sq.var(
|
|
|
120
160
|
holding_period=10
|
|
121
161
|
)
|
|
122
162
|
|
|
123
|
-
#
|
|
124
|
-
|
|
163
|
+
# Print results
|
|
164
|
+
print(f"Latest Portfolio VaR (95%): {portfolio_var.iloc[-1].values[0]:.4f}")
|
|
125
165
|
```
|
|
126
166
|
|
|
127
167
|
### Using Advanced Risk Measures
|
|
@@ -149,6 +189,14 @@ print(f"Latest VaR (95%): {var.iloc[-1, 0]:.4f}")
|
|
|
149
189
|
print(f"Latest CVaR (95%): {cvar.iloc[-1, 0]:.4f}")
|
|
150
190
|
```
|
|
151
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
|
+
|
|
152
200
|
## Documentation
|
|
153
201
|
|
|
154
202
|
For detailed documentation on each function and class, please see the [official documentation](https://www.squarequant.org).
|
|
@@ -5,7 +5,7 @@ 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.1",
|
|
9
9
|
author="Gabriel Bosch",
|
|
10
10
|
author_email="contact@squarequant.org",
|
|
11
11
|
description="A Python package for financial risk metrics and stock data analysis",
|
|
@@ -38,12 +38,13 @@ setup(
|
|
|
38
38
|
"numpy>=1.18.0",
|
|
39
39
|
"yfinance>=0.1.63",
|
|
40
40
|
"scipy>=1.5.0",
|
|
41
|
+
"scikit-learn>=1.5.0",
|
|
41
42
|
"matplotlib>=3.3.0",
|
|
42
43
|
"statsmodels>=0.12.0",
|
|
44
|
+
"requests>=2.25.0", # Added for Theta data provider
|
|
43
45
|
],
|
|
44
46
|
extras_require={
|
|
45
47
|
"optimization": ["cvxpy>=1.0.0"],
|
|
46
|
-
"advanced": ["scikit-learn>=1.0.0"],
|
|
47
48
|
"dev": [
|
|
48
49
|
"pytest>=6.0.0",
|
|
49
50
|
"black>=21.5b2",
|
|
@@ -55,5 +56,5 @@ setup(
|
|
|
55
56
|
"pytest-cov>=2.12.0",
|
|
56
57
|
],
|
|
57
58
|
},
|
|
58
|
-
keywords="finance, risk, portfolio, investment, stocks, analysis, visualization, drawdown, var, cvar, entropic, ulcer index",
|
|
59
|
+
keywords="finance, risk, portfolio, investment, stocks, analysis, visualization, drawdown, var, cvar, entropic, ulcer index, theta data",
|
|
59
60
|
)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SquareQuant - A Python package for financial risk metrics and stock data analysis
|
|
3
|
+
|
|
4
|
+
A comprehensive toolkit for quantitative finance, including risk metrics calculation
|
|
5
|
+
and data retrieval from multiple sources (YFinance and Theta Data).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
# Version information
|
|
9
|
+
__version__ = "0.2.1"
|
|
10
|
+
__author__ = "Gabriel Bosch"
|
|
11
|
+
__email__ = "contact@squarequant.org"
|
|
12
|
+
|
|
13
|
+
# Import main functionality to make it available at the package level
|
|
14
|
+
from .data.data import download_tickers, DownloadConfig
|
|
15
|
+
from .api import (
|
|
16
|
+
sharpe,
|
|
17
|
+
sortino,
|
|
18
|
+
vol,
|
|
19
|
+
mdd,
|
|
20
|
+
calmar,
|
|
21
|
+
var,
|
|
22
|
+
cvar,
|
|
23
|
+
semidev,
|
|
24
|
+
avgdd,
|
|
25
|
+
ulcer,
|
|
26
|
+
mad,
|
|
27
|
+
erm,
|
|
28
|
+
evar,
|
|
29
|
+
cdar,
|
|
30
|
+
edar
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Define what's available when using "from squarequant import *"
|
|
34
|
+
__all__ = [
|
|
35
|
+
'download_tickers',
|
|
36
|
+
'DownloadConfig',
|
|
37
|
+
'sharpe',
|
|
38
|
+
'sortino',
|
|
39
|
+
'vol',
|
|
40
|
+
'mdd',
|
|
41
|
+
'calmar',
|
|
42
|
+
'var',
|
|
43
|
+
'cvar',
|
|
44
|
+
'semidev',
|
|
45
|
+
'avgdd',
|
|
46
|
+
'ulcer',
|
|
47
|
+
'mad',
|
|
48
|
+
'erm',
|
|
49
|
+
'evar',
|
|
50
|
+
'cdar',
|
|
51
|
+
'edar'
|
|
52
|
+
]
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Function-based API for the SquareQuant package
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
|
-
from typing import List, Optional
|
|
5
|
+
from typing import List, Optional, Union, Dict
|
|
6
6
|
import pandas as pd
|
|
7
7
|
|
|
8
8
|
from squarequant.constants import (
|
|
@@ -32,7 +32,6 @@ from squarequant.core.metrics import (
|
|
|
32
32
|
SortinoRatio,
|
|
33
33
|
Volatility,
|
|
34
34
|
MaximumDrawdown,
|
|
35
|
-
ValueAtRisk,
|
|
36
35
|
CalmarRatio,
|
|
37
36
|
ConditionalValueAtRisk,
|
|
38
37
|
SemiDeviation,
|
|
@@ -45,6 +44,10 @@ from squarequant.core.metrics import (
|
|
|
45
44
|
EntropicDrawdownAtRisk
|
|
46
45
|
)
|
|
47
46
|
|
|
47
|
+
from squarequant.var.valueatrisk import (
|
|
48
|
+
ValueAtRisk
|
|
49
|
+
)
|
|
50
|
+
|
|
48
51
|
|
|
49
52
|
def sharpe(data: pd.DataFrame,
|
|
50
53
|
assets: List[str],
|
|
@@ -169,47 +172,6 @@ def mdd(data: pd.DataFrame,
|
|
|
169
172
|
)
|
|
170
173
|
return calculator.calculate()
|
|
171
174
|
|
|
172
|
-
|
|
173
|
-
def var(data: pd.DataFrame,
|
|
174
|
-
assets: List[str],
|
|
175
|
-
confidence: float = DEFAULT_VAR_CONFIDENCE,
|
|
176
|
-
window: int = DEFAULT_VAR_WINDOW,
|
|
177
|
-
holding_period: int = DEFAULT_VAR_HOLDING_PERIOD,
|
|
178
|
-
start: Optional[str] = None,
|
|
179
|
-
end: Optional[str] = None,
|
|
180
|
-
method: str = 'historical',
|
|
181
|
-
scaling_method: str = 'sqrt_time') -> pd.DataFrame:
|
|
182
|
-
"""
|
|
183
|
-
Calculate rolling Value at Risk (VaR) for specified assets
|
|
184
|
-
|
|
185
|
-
Parameters:
|
|
186
|
-
data (DataFrame): DataFrame with asset price data
|
|
187
|
-
assets (List[str]): List of asset columns to calculate VaR for
|
|
188
|
-
confidence (float): Confidence level (0-1), default is 0.99 (99%)
|
|
189
|
-
window (int): Rolling window size in trading days, default is 252 (1 year)
|
|
190
|
-
holding_period (int): Holding period in days, default is 10 days
|
|
191
|
-
start (str, optional): Start date in format 'YYYY-MM-DD'
|
|
192
|
-
end (str, optional): End date in format 'YYYY-MM-DD'
|
|
193
|
-
method (str): Method to calculate VaR - 'historical', 'parametric', default is 'historical'
|
|
194
|
-
scaling_method (str): Method to scale 1-day VaR to holding_period-day VaR, default is 'sqrt_time'
|
|
195
|
-
|
|
196
|
-
Returns:
|
|
197
|
-
DataFrame: Rolling VaR for specified assets
|
|
198
|
-
"""
|
|
199
|
-
calculator = ValueAtRisk(
|
|
200
|
-
data=data,
|
|
201
|
-
assets=assets,
|
|
202
|
-
confidence=confidence,
|
|
203
|
-
window=window,
|
|
204
|
-
holding_period=holding_period, # Pass the new parameter
|
|
205
|
-
start=start,
|
|
206
|
-
end=end,
|
|
207
|
-
method=method,
|
|
208
|
-
scaling_method=scaling_method # Pass the new parameter
|
|
209
|
-
)
|
|
210
|
-
return calculator.calculate()
|
|
211
|
-
|
|
212
|
-
|
|
213
175
|
def calmar(data: pd.DataFrame,
|
|
214
176
|
assets: List[str],
|
|
215
177
|
window: int = DEFAULT_CALMAR_WINDOW,
|
|
@@ -237,6 +199,35 @@ def calmar(data: pd.DataFrame,
|
|
|
237
199
|
)
|
|
238
200
|
return calculator.calculate()
|
|
239
201
|
|
|
202
|
+
def var(
|
|
203
|
+
data: pd.DataFrame,
|
|
204
|
+
assets: List[str],
|
|
205
|
+
confidence: float = 0.95,
|
|
206
|
+
window: int = 252,
|
|
207
|
+
start: Optional[str] = None,
|
|
208
|
+
end: Optional[str] = None,
|
|
209
|
+
method: str = 'historical',
|
|
210
|
+
holding_period: int = 1,
|
|
211
|
+
scaling_method: str = 'sqrt_time',
|
|
212
|
+
weights: Optional[Union[List[float], dict]] = None,
|
|
213
|
+
decay_factor: Optional[float] = None
|
|
214
|
+
) -> pd.DataFrame:
|
|
215
|
+
# Initialize ValueAtRisk object
|
|
216
|
+
var_calculator = ValueAtRisk(
|
|
217
|
+
data=data,
|
|
218
|
+
assets=assets,
|
|
219
|
+
confidence=confidence,
|
|
220
|
+
window=window,
|
|
221
|
+
start=start,
|
|
222
|
+
end=end,
|
|
223
|
+
method=method,
|
|
224
|
+
holding_period=holding_period,
|
|
225
|
+
scaling_method=scaling_method,
|
|
226
|
+
weights=weights
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
# Calculate VaR
|
|
230
|
+
return var_calculator.calculate()
|
|
240
231
|
|
|
241
232
|
def cvar(data: pd.DataFrame,
|
|
242
233
|
assets: List[str],
|
|
@@ -24,12 +24,12 @@ DEFAULT_EDAR_WINDOW = 181
|
|
|
24
24
|
DEFAULT_VAR_HOLDING_PERIOD = 10
|
|
25
25
|
DEFAULT_VAR_CONFIDENCE = 0.99
|
|
26
26
|
VALID_VAR_SCALING_METHODS = {'sqrt_time', 'overlapping'}
|
|
27
|
+
VALID_VAR_METHODS = {'historical', 'parametric'}
|
|
27
28
|
|
|
28
29
|
# Default values
|
|
29
30
|
DEFAULT_CONFIDENCE = 0.99
|
|
30
31
|
|
|
31
32
|
|
|
32
|
-
# Valid options
|
|
33
|
+
# Valid options for data
|
|
33
34
|
VALID_INTERVALS = {'1m', '15m', '60m', '1h', '1d', '1wk', '1mo'}
|
|
34
35
|
VALID_COLUMNS = {'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'}
|
|
35
|
-
VALID_VAR_METHODS = {'historical', 'parametric'}
|