squarequant 0.1.1__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.1.1/squarequant.egg-info → squarequant-0.2.0}/PKG-INFO +64 -17
- {squarequant-0.1.1 → squarequant-0.2.0}/README.md +62 -14
- {squarequant-0.1.1 → squarequant-0.2.0}/setup.py +2 -2
- squarequant-0.2.0/squarequant/__init__.py +59 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/api.py +34 -43
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/constants.py +2 -2
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/core/metrics.py +1 -224
- 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.1.1 → squarequant-0.2.0/squarequant.egg-info}/PKG-INFO +64 -17
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant.egg-info/SOURCES.txt +6 -4
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant.egg-info/requires.txt +1 -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.0}/LICENSE +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/setup.cfg +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/core/__init__.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/core/base.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/utils/__init__.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant/utils/helpers.py +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant.egg-info/dependency_links.txt +0 -0
- {squarequant-0.1.1 → squarequant-0.2.0}/squarequant.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: squarequant
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
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
|
|
@@ -28,12 +28,11 @@ 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
|
|
33
34
|
Provides-Extra: optimization
|
|
34
35
|
Requires-Dist: cvxpy>=1.0.0; extra == "optimization"
|
|
35
|
-
Provides-Extra: advanced
|
|
36
|
-
Requires-Dist: scikit-learn>=1.0.0; extra == "advanced"
|
|
37
36
|
Provides-Extra: dev
|
|
38
37
|
Requires-Dist: pytest>=6.0.0; extra == "dev"
|
|
39
38
|
Requires-Dist: black>=21.5b2; extra == "dev"
|
|
@@ -59,14 +58,13 @@ Dynamic: summary
|
|
|
59
58
|
|
|
60
59
|
# SquareQuant
|
|
61
60
|
|
|
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
|
|
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.
|
|
63
62
|
|
|
64
63
|
## Features
|
|
65
64
|
|
|
66
|
-
- **Financial Data Access**:
|
|
65
|
+
- **Financial Data Access**: Multiple data sources including YFinance and Theta Data
|
|
67
66
|
- **Extensive Risk Metrics**: Comprehensive set of traditional and advanced risk measures
|
|
68
|
-
- **
|
|
69
|
-
- **Visualization**: Built-in plotting functions for visual analysis
|
|
67
|
+
- **Modular Design**: Reorganized into specialized modules (data, var, etc.)
|
|
70
68
|
- **Flexible API**: Consistent function-based and object-oriented interfaces
|
|
71
69
|
|
|
72
70
|
## Installation
|
|
@@ -79,6 +77,7 @@ pip install squarequant
|
|
|
79
77
|
|
|
80
78
|
```python
|
|
81
79
|
import squarequant as sq
|
|
80
|
+
import pandas as pd
|
|
82
81
|
import matplotlib.pyplot as plt
|
|
83
82
|
|
|
84
83
|
# Configure data download
|
|
@@ -86,7 +85,8 @@ config = sq.DownloadConfig(
|
|
|
86
85
|
start_date="2020-01-01",
|
|
87
86
|
end_date="2023-12-31",
|
|
88
87
|
interval='1d',
|
|
89
|
-
columns=['Close']
|
|
88
|
+
columns=['Close'],
|
|
89
|
+
source="yfinance" # Or "theta" for Theta Data
|
|
90
90
|
)
|
|
91
91
|
|
|
92
92
|
# Download data
|
|
@@ -98,7 +98,7 @@ vol = sq.vol(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=21)
|
|
|
98
98
|
# Calculate Sharpe ratio
|
|
99
99
|
sharpe = sq.sharpe(data=data, assets=['AAPL', 'MSFT', 'GOOGL'], window=252)
|
|
100
100
|
|
|
101
|
-
# Visualize results
|
|
101
|
+
# Visualize results (using matplotlib directly)
|
|
102
102
|
plt.figure(figsize=(12, 6))
|
|
103
103
|
plt.plot(vol)
|
|
104
104
|
plt.title('21-Day Rolling Volatility')
|
|
@@ -127,12 +127,41 @@ SquareQuant provides a comprehensive set of risk metrics, including:
|
|
|
127
127
|
| Conditional Drawdown at Risk | `sq.cdar()` | Expected drawdown beyond threshold |
|
|
128
128
|
| Entropic Drawdown at Risk | `sq.edar()` | Drawdown risk using entropy concepts |
|
|
129
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
|
+
|
|
130
158
|
## Advanced Usage Examples
|
|
131
159
|
|
|
132
160
|
### Analyzing Multiple Risk Metrics
|
|
133
161
|
|
|
134
162
|
```python
|
|
135
163
|
import squarequant as sq
|
|
164
|
+
import pandas as pd
|
|
136
165
|
import matplotlib.pyplot as plt
|
|
137
166
|
|
|
138
167
|
# Download data
|
|
@@ -149,12 +178,22 @@ mad = sq.mad(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
|
149
178
|
mdd = sq.mdd(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
150
179
|
ulcer = sq.ulcer(data=data, assets=['SPY', 'QQQ'], window=252)
|
|
151
180
|
|
|
152
|
-
# Visualize comparison
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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()
|
|
158
197
|
```
|
|
159
198
|
|
|
160
199
|
### Portfolio Risk Analysis
|
|
@@ -179,8 +218,8 @@ portfolio_var = sq.var(
|
|
|
179
218
|
holding_period=10
|
|
180
219
|
)
|
|
181
220
|
|
|
182
|
-
#
|
|
183
|
-
|
|
221
|
+
# Print results
|
|
222
|
+
print(f"Latest Portfolio VaR (95%): {portfolio_var.iloc[-1].values[0]:.4f}")
|
|
184
223
|
```
|
|
185
224
|
|
|
186
225
|
### Using Advanced Risk Measures
|
|
@@ -208,6 +247,14 @@ print(f"Latest VaR (95%): {var.iloc[-1, 0]:.4f}")
|
|
|
208
247
|
print(f"Latest CVaR (95%): {cvar.iloc[-1, 0]:.4f}")
|
|
209
248
|
```
|
|
210
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
|
+
|
|
211
258
|
## Documentation
|
|
212
259
|
|
|
213
260
|
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.0",
|
|
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,12 @@ 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",
|
|
43
44
|
],
|
|
44
45
|
extras_require={
|
|
45
46
|
"optimization": ["cvxpy>=1.0.0"],
|
|
46
|
-
"advanced": ["scikit-learn>=1.0.0"],
|
|
47
47
|
"dev": [
|
|
48
48
|
"pytest>=6.0.0",
|
|
49
49
|
"black>=21.5b2",
|
|
@@ -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
|
+
)
|
|
@@ -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'}
|