openseries 1.5.6__py3-none-any.whl → 1.5.7__py3-none-any.whl
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.
- openseries/_risk.py +0 -61
- openseries/datefixer.py +11 -10
- openseries/frame.py +14 -17
- openseries/series.py +4 -9
- {openseries-1.5.6.dist-info → openseries-1.5.7.dist-info}/METADATA +3 -3
- {openseries-1.5.6.dist-info → openseries-1.5.7.dist-info}/RECORD +8 -8
- {openseries-1.5.6.dist-info → openseries-1.5.7.dist-info}/LICENSE.md +0 -0
- {openseries-1.5.6.dist-info → openseries-1.5.7.dist-info}/WHEEL +0 -0
openseries/_risk.py
CHANGED
@@ -6,19 +6,11 @@ from math import ceil
|
|
6
6
|
from typing import Union, cast
|
7
7
|
|
8
8
|
from numpy import (
|
9
|
-
divide,
|
10
|
-
float64,
|
11
|
-
isinf,
|
12
9
|
mean,
|
13
|
-
nan,
|
14
10
|
nan_to_num,
|
15
11
|
quantile,
|
16
12
|
sort,
|
17
|
-
sqrt,
|
18
|
-
square,
|
19
|
-
std,
|
20
13
|
)
|
21
|
-
from numpy.typing import NDArray
|
22
14
|
from pandas import DataFrame, Series
|
23
15
|
|
24
16
|
from openseries.types import LiteralQuantileInterp
|
@@ -87,56 +79,3 @@ def _var_down_calc(
|
|
87
79
|
clean = nan_to_num(data)
|
88
80
|
ret = clean[1:] / clean[:-1] - 1
|
89
81
|
return cast(float, quantile(ret, 1 - level, method=interpolation))
|
90
|
-
|
91
|
-
|
92
|
-
def _ewma_calc(
|
93
|
-
reeturn: float,
|
94
|
-
prev_ewma: float,
|
95
|
-
time_factor: float,
|
96
|
-
lmbda: float = 0.94,
|
97
|
-
) -> float:
|
98
|
-
"""
|
99
|
-
Calculate Exponentially Weighted Moving Average volatility.
|
100
|
-
|
101
|
-
Parameters
|
102
|
-
----------
|
103
|
-
reeturn : float
|
104
|
-
Return value
|
105
|
-
prev_ewma : float
|
106
|
-
Previous EWMA volatility value
|
107
|
-
time_factor : float
|
108
|
-
Scaling factor to annualize
|
109
|
-
lmbda: float, default: 0.94
|
110
|
-
Scaling factor to determine weighting.
|
111
|
-
|
112
|
-
Returns
|
113
|
-
-------
|
114
|
-
float
|
115
|
-
EWMA volatility value
|
116
|
-
|
117
|
-
"""
|
118
|
-
return cast(
|
119
|
-
float,
|
120
|
-
sqrt(square(reeturn) * time_factor * (1 - lmbda) + square(prev_ewma) * lmbda),
|
121
|
-
)
|
122
|
-
|
123
|
-
|
124
|
-
def _calc_inv_vol_weights(returns: DataFrame) -> NDArray[float64]:
|
125
|
-
"""
|
126
|
-
Calculate weights proportional to inverse volatility.
|
127
|
-
|
128
|
-
Parameters
|
129
|
-
----------
|
130
|
-
returns: pandas.DataFrame
|
131
|
-
returns data
|
132
|
-
|
133
|
-
Returns
|
134
|
-
-------
|
135
|
-
NDArray[float64]
|
136
|
-
Calculated weights
|
137
|
-
|
138
|
-
"""
|
139
|
-
vol = divide(1.0, std(returns, axis=0, ddof=1))
|
140
|
-
vol[isinf(vol)] = nan
|
141
|
-
volsum = vol.sum()
|
142
|
-
return cast(NDArray[float64], divide(vol, volsum))
|
openseries/datefixer.py
CHANGED
@@ -62,7 +62,7 @@ def holiday_calendar(
|
|
62
62
|
endyear += 1
|
63
63
|
if startyear == endyear:
|
64
64
|
endyear += 1
|
65
|
-
years = list(range(
|
65
|
+
years = list(range(startyear, endyear))
|
66
66
|
|
67
67
|
if isinstance(countries, str) and countries in list_supported_countries():
|
68
68
|
staging = country_holidays(country=countries, years=years)
|
@@ -116,15 +116,11 @@ def date_fix(
|
|
116
116
|
if isinstance(fixerdate, datetime64):
|
117
117
|
return (
|
118
118
|
dt.datetime.strptime(str(fixerdate)[:10], "%Y-%m-%d")
|
119
|
-
.
|
119
|
+
.astimezone()
|
120
120
|
.date()
|
121
121
|
)
|
122
122
|
if isinstance(fixerdate, str):
|
123
|
-
return (
|
124
|
-
dt.datetime.strptime(fixerdate, "%Y-%m-%d")
|
125
|
-
.replace(tzinfo=dt.timezone.utc)
|
126
|
-
.date()
|
127
|
-
)
|
123
|
+
return dt.datetime.strptime(fixerdate, "%Y-%m-%d").astimezone().date()
|
128
124
|
msg = f"Unknown date format {fixerdate!s} of type {type(fixerdate)!s} encountered"
|
129
125
|
raise TypeError(
|
130
126
|
msg,
|
@@ -212,7 +208,7 @@ def get_previous_business_day_before_today(
|
|
212
208
|
|
213
209
|
"""
|
214
210
|
if today is None:
|
215
|
-
today = dt.datetime.now(
|
211
|
+
today = dt.datetime.now().astimezone().date()
|
216
212
|
|
217
213
|
return date_offset_foll(
|
218
214
|
today - dt.timedelta(days=1),
|
@@ -241,8 +237,8 @@ def offset_business_days(
|
|
241
237
|
ddate: datetime.date
|
242
238
|
A starting date that does not have to be a business day
|
243
239
|
days: int
|
244
|
-
The number of business days to offset from the business day that is
|
245
|
-
|
240
|
+
The number of business days to offset from the business day that is given
|
241
|
+
If days is set as anything other than an integer its value is set to zero
|
246
242
|
countries: CountriesType, default: "SE"
|
247
243
|
(List of) country code(s) according to ISO 3166-1 alpha-2
|
248
244
|
custom_holidays: HolidayType, optional
|
@@ -255,6 +251,11 @@ def offset_business_days(
|
|
255
251
|
The new offset business day
|
256
252
|
|
257
253
|
"""
|
254
|
+
try:
|
255
|
+
days = int(days)
|
256
|
+
except TypeError:
|
257
|
+
days = 0
|
258
|
+
|
258
259
|
if days <= 0:
|
259
260
|
scaledtoyeardays = int((days * 372 / 250) // 1) - 365
|
260
261
|
ndate = ddate + dt.timedelta(days=scaledtoyeardays)
|
openseries/frame.py
CHANGED
@@ -17,13 +17,17 @@ from numpy import (
|
|
17
17
|
array,
|
18
18
|
cov,
|
19
19
|
cumprod,
|
20
|
+
divide,
|
20
21
|
dot,
|
21
22
|
float64,
|
22
23
|
inf,
|
24
|
+
isinf,
|
23
25
|
linspace,
|
24
26
|
log,
|
25
27
|
nan,
|
26
28
|
sqrt,
|
29
|
+
square,
|
30
|
+
std,
|
27
31
|
zeros,
|
28
32
|
)
|
29
33
|
from numpy import (
|
@@ -53,10 +57,6 @@ from statsmodels.regression.linear_model import ( # type: ignore[import-untyped
|
|
53
57
|
from typing_extensions import Self
|
54
58
|
|
55
59
|
from openseries._common_model import _CommonModel
|
56
|
-
from openseries._risk import (
|
57
|
-
_calc_inv_vol_weights,
|
58
|
-
_ewma_calc,
|
59
|
-
)
|
60
60
|
from openseries.datefixer import do_resample_to_business_period_ends
|
61
61
|
from openseries.load_plotly import load_plotly_dict
|
62
62
|
from openseries.series import OpenTimeSeries
|
@@ -591,17 +591,13 @@ class OpenFrame(_CommonModel):
|
|
591
591
|
raw_corr = [raw_cov[0] / (2 * raw_one[0] * raw_two[0])]
|
592
592
|
|
593
593
|
for _, row in data.iloc[1:].iterrows():
|
594
|
-
tmp_raw_one =
|
595
|
-
|
596
|
-
|
597
|
-
time_factor=time_factor,
|
598
|
-
lmbda=lmbda,
|
594
|
+
tmp_raw_one = sqrt(
|
595
|
+
square(row.loc[cols[0], ValueType.RTRN]) * time_factor * (1 - lmbda)
|
596
|
+
+ square(raw_one[-1]) * lmbda,
|
599
597
|
)
|
600
|
-
tmp_raw_two =
|
601
|
-
|
602
|
-
|
603
|
-
time_factor=time_factor,
|
604
|
-
lmbda=lmbda,
|
598
|
+
tmp_raw_two = sqrt(
|
599
|
+
square(row.loc[cols[1], ValueType.RTRN]) * time_factor * (1 - lmbda)
|
600
|
+
+ square(raw_two[-1]) * lmbda,
|
605
601
|
)
|
606
602
|
tmp_raw_cov = (
|
607
603
|
row.loc[cols[0], ValueType.RTRN]
|
@@ -1508,8 +1504,9 @@ class OpenFrame(_CommonModel):
|
|
1508
1504
|
if weight_strat == "eq_weights":
|
1509
1505
|
self.weights = [1.0 / self.item_count] * self.item_count
|
1510
1506
|
elif weight_strat == "inv_vol":
|
1511
|
-
|
1512
|
-
|
1507
|
+
vol = divide(1.0, std(dframe, axis=0, ddof=1))
|
1508
|
+
vol[isinf(vol)] = nan
|
1509
|
+
self.weights = list(divide(vol, vol.sum()))
|
1513
1510
|
else:
|
1514
1511
|
msg = "Weight strategy not implemented"
|
1515
1512
|
raise NotImplementedError(msg)
|
@@ -2188,7 +2185,7 @@ def sharpeplot( # noqa: C901
|
|
2188
2185
|
xhoverformat=".2%",
|
2189
2186
|
yhoverformat=".2%",
|
2190
2187
|
hovertext=[point_frame.loc["text", col]],
|
2191
|
-
hovertemplate=
|
2188
|
+
hovertemplate="Return %{y}<br>Vol %{x}%{hovertext}",
|
2192
2189
|
hoverlabel_align="right",
|
2193
2190
|
marker={"size": 20, "color": clr},
|
2194
2191
|
mode=point_frame_mode,
|
openseries/series.py
CHANGED
@@ -15,6 +15,7 @@ from numpy import (
|
|
15
15
|
isnan,
|
16
16
|
log,
|
17
17
|
sqrt,
|
18
|
+
square,
|
18
19
|
)
|
19
20
|
from pandas import (
|
20
21
|
DataFrame,
|
@@ -28,9 +29,6 @@ from pydantic import model_validator
|
|
28
29
|
from typing_extensions import Self
|
29
30
|
|
30
31
|
from openseries._common_model import _CommonModel
|
31
|
-
from openseries._risk import (
|
32
|
-
_ewma_calc,
|
33
|
-
)
|
34
32
|
from openseries.datefixer import date_fix, do_resample_to_business_period_ends
|
35
33
|
from openseries.types import (
|
36
34
|
Countries,
|
@@ -675,13 +673,10 @@ class OpenTimeSeries(_CommonModel):
|
|
675
673
|
]
|
676
674
|
|
677
675
|
for item in data.loc[:, cast(int, (self.label, ValueType.RTRN))].iloc[1:]:
|
678
|
-
|
676
|
+
prev = rawdata[-1]
|
679
677
|
rawdata.append(
|
680
|
-
|
681
|
-
|
682
|
-
prev_ewma=previous,
|
683
|
-
time_factor=time_factor,
|
684
|
-
lmbda=lmbda,
|
678
|
+
sqrt(
|
679
|
+
square(item) * time_factor * (1 - lmbda) + square(prev) * lmbda,
|
685
680
|
),
|
686
681
|
)
|
687
682
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: openseries
|
3
|
-
Version: 1.5.
|
3
|
+
Version: 1.5.7
|
4
4
|
Summary: Tools for analyzing financial timeseries.
|
5
5
|
Home-page: https://github.com/CaptorAB/openseries
|
6
6
|
License: BSD-3-Clause
|
@@ -20,8 +20,8 @@ Classifier: Programming Language :: Python :: 3.10
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.11
|
21
21
|
Classifier: Programming Language :: Python :: 3.12
|
22
22
|
Classifier: Topic :: Office/Business :: Financial :: Investment
|
23
|
-
Requires-Dist: holidays (>=0.30,<0
|
24
|
-
Requires-Dist: numpy (>=1.23.2,<=
|
23
|
+
Requires-Dist: holidays (>=0.30,<1.0)
|
24
|
+
Requires-Dist: numpy (>=1.23.2,<=3.0.0)
|
25
25
|
Requires-Dist: openpyxl (>=3.1.2,<4.0.0)
|
26
26
|
Requires-Dist: pandas (>=2.1.2,<3.0.0)
|
27
27
|
Requires-Dist: plotly (>=5.18.0,<6.0.0)
|
@@ -1,15 +1,15 @@
|
|
1
1
|
openseries/__init__.py,sha256=W429Ojwa-wPgHV5PDAOQOBOAzPOR4wrVHRwdZQjRKcQ,41
|
2
2
|
openseries/_common_model.py,sha256=gHJfvxsOjrjGnuoTm62LRYho_VeEhRKmbY9xM6tiRUk,75497
|
3
|
-
openseries/_risk.py,sha256=
|
4
|
-
openseries/datefixer.py,sha256=
|
5
|
-
openseries/frame.py,sha256=
|
3
|
+
openseries/_risk.py,sha256=etureOLszoEgD48NEOZkx24gaK8I-PfjRXV4Ymjkh7w,2072
|
4
|
+
openseries/datefixer.py,sha256=YUHIagBteiXp0vy-e3isi3o5_e-1Qoh3weTog0sB_wk,12485
|
5
|
+
openseries/frame.py,sha256=GBe3zH-Zk8XuVYEJ9gmMhpG1z1iUUYNwOlJFUqPGdgE,73947
|
6
6
|
openseries/load_plotly.py,sha256=L4A3Fa5Jk47FY7pcg0NPZ0gm0JD_uCXjVlGC7FWVSJg,1856
|
7
7
|
openseries/plotly_captor_logo.json,sha256=F5nhMzEyxKywtjvQqMTKgKRCJQYMDIiBgDSxdte8Clo,178
|
8
8
|
openseries/plotly_layouts.json,sha256=ahx8-dL4_RPzvHtBOX0SiL0AH7xQJzNRSDhGrSmU-Og,1429
|
9
|
-
openseries/series.py,sha256=
|
9
|
+
openseries/series.py,sha256=fN5u07n2f9ZVef3eFlpzY_Qo2E5qnkwEst5w3wy9FGU,28186
|
10
10
|
openseries/simulation.py,sha256=VYxc-e5VSyC55DdfACpQen-necYbhso-6RMyOhYX-5k,13905
|
11
11
|
openseries/types.py,sha256=yJmGZcBFwvMQQEaRVb0vhVMP463xu7MSydjWr12X38M,7689
|
12
|
-
openseries-1.5.
|
13
|
-
openseries-1.5.
|
14
|
-
openseries-1.5.
|
15
|
-
openseries-1.5.
|
12
|
+
openseries-1.5.7.dist-info/LICENSE.md,sha256=cPUabMxJ6-ziqzqS6aLGkR-ilIOKe_s3Qtyp0ioTmo0,1521
|
13
|
+
openseries-1.5.7.dist-info/METADATA,sha256=QgoElzzhkgZ4Bt6NZtjA4SGJcC1gL9Ho8LpWdkG4-zM,44222
|
14
|
+
openseries-1.5.7.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
15
|
+
openseries-1.5.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|