sabia 0.1.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.
- sabia-0.1.0/LICENSE +21 -0
- sabia-0.1.0/PKG-INFO +91 -0
- sabia-0.1.0/README.md +55 -0
- sabia-0.1.0/pyproject.toml +97 -0
- sabia-0.1.0/src/sabia/__init__.py +87 -0
- sabia-0.1.0/src/sabia/_math.py +25 -0
- sabia-0.1.0/src/sabia/cross_sectional.py +94 -0
- sabia-0.1.0/src/sabia/distribution.py +97 -0
- sabia-0.1.0/src/sabia/mean_reversion.py +134 -0
- sabia-0.1.0/src/sabia/momentum.py +226 -0
- sabia-0.1.0/src/sabia/normalize.py +90 -0
- sabia-0.1.0/src/sabia/registry.py +210 -0
- sabia-0.1.0/src/sabia/returns.py +72 -0
- sabia-0.1.0/src/sabia/seasonality.py +88 -0
- sabia-0.1.0/src/sabia/spec.py +222 -0
- sabia-0.1.0/src/sabia/trend.py +151 -0
- sabia-0.1.0/src/sabia/validate.py +143 -0
- sabia-0.1.0/src/sabia/volatility.py +212 -0
- sabia-0.1.0/src/sabia/volume.py +200 -0
sabia-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Antônio Salomão
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
sabia-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sabia
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Polars-native technical features for trading pipelines — pure, point-in-time, online-ready
|
|
5
|
+
Keywords: finance,features,trading,technical-analysis,polars,backtesting
|
|
6
|
+
Author: Antônio Salomão
|
|
7
|
+
Author-email: Antônio Salomão <83646729+aexsalomao@users.noreply.github.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
16
|
+
Requires-Dist: polars==1.39.3
|
|
17
|
+
Requires-Dist: numpy>=1.26
|
|
18
|
+
Requires-Dist: tzdata
|
|
19
|
+
Requires-Dist: pytest>=8.0 ; extra == 'dev'
|
|
20
|
+
Requires-Dist: pytest-cov>=5.0 ; extra == 'dev'
|
|
21
|
+
Requires-Dist: pytest-benchmark>=4.0 ; extra == 'dev'
|
|
22
|
+
Requires-Dist: hypothesis>=6.100 ; extra == 'dev'
|
|
23
|
+
Requires-Dist: ruff>=0.8.0 ; extra == 'dev'
|
|
24
|
+
Requires-Dist: mypy>=1.10.0 ; extra == 'dev'
|
|
25
|
+
Requires-Dist: pre-commit>=3.7.0 ; extra == 'dev'
|
|
26
|
+
Requires-Dist: mkdocs>=1.6.0 ; extra == 'dev'
|
|
27
|
+
Requires-Dist: mkdocs-material>=9.5.0 ; extra == 'dev'
|
|
28
|
+
Requires-Python: >=3.13
|
|
29
|
+
Project-URL: Homepage, https://aexsalomao.github.io/sabia
|
|
30
|
+
Project-URL: Documentation, https://aexsalomao.github.io/sabia
|
|
31
|
+
Project-URL: Repository, https://github.com/aexsalomao/sabia
|
|
32
|
+
Project-URL: Issues, https://github.com/aexsalomao/sabia/issues
|
|
33
|
+
Project-URL: Changelog, https://github.com/aexsalomao/sabia/blob/master/CHANGELOG.md
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# sabia
|
|
38
|
+
|
|
39
|
+
> Polars-native technical features for trading pipelines — pure, point-in-time, online-ready.
|
|
40
|
+
|
|
41
|
+
`sabia` reads price/volume into features, grounded in the trading & finance literature. It is the
|
|
42
|
+
**features** brick in a layered stack:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
marketgoblin (data in) → sabia (features) → quale (signals)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Runtime dependencies are just `polars` and `numpy`. (The stack's calendar brick
|
|
49
|
+
[`quando`](https://github.com/aexsalomao/quando) will be wired in when calendar-aware seasonality or
|
|
50
|
+
the microstructure tier needs it — v1 seasonality is pure vectorized datetime.) Risk/eval math lives
|
|
51
|
+
in `ruin`; signals live in `quale`. sabia computes features and nothing else.
|
|
52
|
+
|
|
53
|
+
## What it is
|
|
54
|
+
|
|
55
|
+
Pure functions over OHLCV bars that return Polars expressions (`pl.Expr`) — strictly trailing,
|
|
56
|
+
point-in-time correct, deterministic. **Batch-first, online-ready**: nothing streams in v1, but every
|
|
57
|
+
feature declares the history it needs and is covered by a windowed-recompute parity test, so a future
|
|
58
|
+
online engine is a thin wrapper rather than a rewrite.
|
|
59
|
+
|
|
60
|
+
## Install
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
uv sync --extra dev
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Quickstart
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import polars as pl
|
|
70
|
+
import sabia
|
|
71
|
+
|
|
72
|
+
frame = ... # OHLCV LazyFrame/DataFrame; see sabia.validate for the input contract
|
|
73
|
+
|
|
74
|
+
# Features are pl.Expr — compose them lazily, or materialize eagerly:
|
|
75
|
+
df = sabia.compute(frame, sabia.momentum.rsi(period=14), sabia.volatility.vol_yz(window=21))
|
|
76
|
+
|
|
77
|
+
# Query the registry by horizon or data tier:
|
|
78
|
+
reg = sabia.Registry.default()
|
|
79
|
+
reg.where(lambda s: sabia.Horizon.MEDIUM in s.native_band)
|
|
80
|
+
reg.available(sabia.DataTier.DAILY)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Invariants
|
|
84
|
+
|
|
85
|
+
Causality · point-in-time correctness · purity (no I/O, clocks, randomness) · Polars-native
|
|
86
|
+
(no pandas) · determinism within a declared tolerance. All enforced by tests, not convention. See
|
|
87
|
+
`FEATURES.md` for the full spec.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
sabia-0.1.0/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# sabia
|
|
2
|
+
|
|
3
|
+
> Polars-native technical features for trading pipelines — pure, point-in-time, online-ready.
|
|
4
|
+
|
|
5
|
+
`sabia` reads price/volume into features, grounded in the trading & finance literature. It is the
|
|
6
|
+
**features** brick in a layered stack:
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
marketgoblin (data in) → sabia (features) → quale (signals)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Runtime dependencies are just `polars` and `numpy`. (The stack's calendar brick
|
|
13
|
+
[`quando`](https://github.com/aexsalomao/quando) will be wired in when calendar-aware seasonality or
|
|
14
|
+
the microstructure tier needs it — v1 seasonality is pure vectorized datetime.) Risk/eval math lives
|
|
15
|
+
in `ruin`; signals live in `quale`. sabia computes features and nothing else.
|
|
16
|
+
|
|
17
|
+
## What it is
|
|
18
|
+
|
|
19
|
+
Pure functions over OHLCV bars that return Polars expressions (`pl.Expr`) — strictly trailing,
|
|
20
|
+
point-in-time correct, deterministic. **Batch-first, online-ready**: nothing streams in v1, but every
|
|
21
|
+
feature declares the history it needs and is covered by a windowed-recompute parity test, so a future
|
|
22
|
+
online engine is a thin wrapper rather than a rewrite.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
uv sync --extra dev
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quickstart
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import polars as pl
|
|
34
|
+
import sabia
|
|
35
|
+
|
|
36
|
+
frame = ... # OHLCV LazyFrame/DataFrame; see sabia.validate for the input contract
|
|
37
|
+
|
|
38
|
+
# Features are pl.Expr — compose them lazily, or materialize eagerly:
|
|
39
|
+
df = sabia.compute(frame, sabia.momentum.rsi(period=14), sabia.volatility.vol_yz(window=21))
|
|
40
|
+
|
|
41
|
+
# Query the registry by horizon or data tier:
|
|
42
|
+
reg = sabia.Registry.default()
|
|
43
|
+
reg.where(lambda s: sabia.Horizon.MEDIUM in s.native_band)
|
|
44
|
+
reg.available(sabia.DataTier.DAILY)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Invariants
|
|
48
|
+
|
|
49
|
+
Causality · point-in-time correctness · purity (no I/O, clocks, randomness) · Polars-native
|
|
50
|
+
(no pandas) · determinism within a declared tolerance. All enforced by tests, not convention. See
|
|
51
|
+
`FEATURES.md` for the full spec.
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sabia"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Polars-native technical features for trading pipelines — pure, point-in-time, online-ready"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Antônio Salomão", email = "83646729+aexsalomao@users.noreply.github.com" }
|
|
10
|
+
]
|
|
11
|
+
keywords = ["finance", "features", "trading", "technical-analysis", "polars", "backtesting"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Topic :: Office/Business :: Financial",
|
|
19
|
+
]
|
|
20
|
+
requires-python = ">=3.13"
|
|
21
|
+
dependencies = [
|
|
22
|
+
# Polars is pinned exactly: the §3.4 immutability / fingerprint guarantee depends on it.
|
|
23
|
+
"polars==1.39.3",
|
|
24
|
+
"numpy>=1.26",
|
|
25
|
+
# sabia's input contract is tz-aware UTC timestamps; tzdata provides the IANA zone database
|
|
26
|
+
# where the OS lacks one (Windows, slim containers), so zone resolution never fails at runtime.
|
|
27
|
+
"tzdata",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=8.0",
|
|
33
|
+
"pytest-cov>=5.0",
|
|
34
|
+
"pytest-benchmark>=4.0",
|
|
35
|
+
"hypothesis>=6.100",
|
|
36
|
+
"ruff>=0.8.0",
|
|
37
|
+
"mypy>=1.10.0",
|
|
38
|
+
"pre-commit>=3.7.0",
|
|
39
|
+
"mkdocs>=1.6.0",
|
|
40
|
+
"mkdocs-material>=9.5.0",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[project.urls]
|
|
44
|
+
Homepage = "https://aexsalomao.github.io/sabia"
|
|
45
|
+
Documentation = "https://aexsalomao.github.io/sabia"
|
|
46
|
+
Repository = "https://github.com/aexsalomao/sabia"
|
|
47
|
+
Issues = "https://github.com/aexsalomao/sabia/issues"
|
|
48
|
+
Changelog = "https://github.com/aexsalomao/sabia/blob/master/CHANGELOG.md"
|
|
49
|
+
|
|
50
|
+
[build-system]
|
|
51
|
+
requires = ["uv_build>=0.11.6,<0.12.0"]
|
|
52
|
+
build-backend = "uv_build"
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = ["tests"]
|
|
56
|
+
# Default run is the fast unit suite; calibration (integration) and benchmarks (slow) are separate
|
|
57
|
+
# gates, run explicitly with `pytest -m integration` / `pytest -m slow`.
|
|
58
|
+
addopts = "-m 'not slow and not integration'"
|
|
59
|
+
markers = [
|
|
60
|
+
"integration: calibration / cross-subsystem tests (local fixture, no network)",
|
|
61
|
+
"slow: performance benchmarks (pytest-benchmark)",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
line-length = 100
|
|
66
|
+
target-version = "py313"
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = [
|
|
70
|
+
"E", # pycodestyle errors
|
|
71
|
+
"W", # pycodestyle warnings
|
|
72
|
+
"F", # pyflakes
|
|
73
|
+
"I", # isort
|
|
74
|
+
"UP", # pyupgrade
|
|
75
|
+
"B", # flake8-bugbear
|
|
76
|
+
"SIM", # flake8-simplify
|
|
77
|
+
]
|
|
78
|
+
ignore = [
|
|
79
|
+
"B904", # raise-without-from-inside-except — sometimes intentional
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint.isort]
|
|
83
|
+
known-first-party = ["sabia"]
|
|
84
|
+
|
|
85
|
+
[tool.mypy]
|
|
86
|
+
python_version = "3.13"
|
|
87
|
+
strict = true
|
|
88
|
+
ignore_missing_imports = true
|
|
89
|
+
exclude = ["tests/"]
|
|
90
|
+
|
|
91
|
+
[tool.coverage.run]
|
|
92
|
+
source = ["sabia"]
|
|
93
|
+
omit = ["tests/*"]
|
|
94
|
+
|
|
95
|
+
[tool.coverage.report]
|
|
96
|
+
show_missing = true
|
|
97
|
+
skip_covered = false
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""
|
|
2
|
+
sabia -- Polars-native technical features for trading pipelines.
|
|
3
|
+
|
|
4
|
+
Pure functions over OHLCV bars that return strictly-trailing, point-in-time-correct ``pl.Expr``
|
|
5
|
+
features. Batch-first, online-ready. See ``FEATURES.md`` for the full specification.
|
|
6
|
+
|
|
7
|
+
import polars as pl
|
|
8
|
+
import sabia
|
|
9
|
+
|
|
10
|
+
sabia.validate(frame, required=[sabia.Column.CLOSE])
|
|
11
|
+
df = sabia.compute(frame, sabia.momentum.rsi(period=14), sabia.volatility.vol_yz(window=21))
|
|
12
|
+
|
|
13
|
+
reg = sabia.Registry.default()
|
|
14
|
+
reg.where(lambda s: sabia.Horizon.MEDIUM in s.native_band)
|
|
15
|
+
reg.available(sabia.DataTier.DAILY)
|
|
16
|
+
|
|
17
|
+
Time-series features compose as ``pl.Expr`` via ``compute``. Cross-sectional features are two-pass;
|
|
18
|
+
evaluate them through ``sabia.evaluate(frame, feature)`` or the registry.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import polars as pl
|
|
24
|
+
|
|
25
|
+
from sabia import (
|
|
26
|
+
cross_sectional,
|
|
27
|
+
distribution,
|
|
28
|
+
mean_reversion,
|
|
29
|
+
momentum,
|
|
30
|
+
normalize,
|
|
31
|
+
returns,
|
|
32
|
+
seasonality,
|
|
33
|
+
trend,
|
|
34
|
+
volatility,
|
|
35
|
+
volume,
|
|
36
|
+
)
|
|
37
|
+
from sabia.registry import RegisteredFeature, Registry, evaluate, make_feature
|
|
38
|
+
from sabia.spec import (
|
|
39
|
+
Column,
|
|
40
|
+
Cost,
|
|
41
|
+
DataTier,
|
|
42
|
+
Family,
|
|
43
|
+
FeatureSpec,
|
|
44
|
+
Horizon,
|
|
45
|
+
Recurrence,
|
|
46
|
+
)
|
|
47
|
+
from sabia.validate import SabiaValidationError, validate
|
|
48
|
+
|
|
49
|
+
__version__ = "0.1.0"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def compute(frame: pl.DataFrame | pl.LazyFrame, *exprs: pl.Expr) -> pl.DataFrame:
|
|
53
|
+
"""Materialize feature expressions into a DataFrame -- a ``select`` over the same expressions.
|
|
54
|
+
|
|
55
|
+
For time-series and normalization features (``pl.Expr``). Cross-sectional features are two-pass;
|
|
56
|
+
use ``evaluate`` (or the registry) for those.
|
|
57
|
+
"""
|
|
58
|
+
return frame.lazy().select(*exprs).collect()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
__all__ = [
|
|
62
|
+
"Column",
|
|
63
|
+
"Cost",
|
|
64
|
+
"DataTier",
|
|
65
|
+
"Family",
|
|
66
|
+
"FeatureSpec",
|
|
67
|
+
"Horizon",
|
|
68
|
+
"RegisteredFeature",
|
|
69
|
+
"Registry",
|
|
70
|
+
"Recurrence",
|
|
71
|
+
"SabiaValidationError",
|
|
72
|
+
"__version__",
|
|
73
|
+
"compute",
|
|
74
|
+
"cross_sectional",
|
|
75
|
+
"distribution",
|
|
76
|
+
"evaluate",
|
|
77
|
+
"make_feature",
|
|
78
|
+
"mean_reversion",
|
|
79
|
+
"momentum",
|
|
80
|
+
"normalize",
|
|
81
|
+
"returns",
|
|
82
|
+
"seasonality",
|
|
83
|
+
"trend",
|
|
84
|
+
"validate",
|
|
85
|
+
"volatility",
|
|
86
|
+
"volume",
|
|
87
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Shared expression guards enforcing the no-inf/no-NaN policy (FEATURES.md 3.5): degenerate inputs
|
|
2
|
+
# yield null, never inf or NaN. Used across feature families wherever a divide, log, or sqrt could
|
|
3
|
+
# otherwise escape into downstream math.
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import polars as pl
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def safe_div(numerator: pl.Expr, denominator: pl.Expr) -> pl.Expr:
|
|
11
|
+
"""``numerator / denominator``, or null where the denominator is zero."""
|
|
12
|
+
return pl.when(denominator == 0).then(None).otherwise(numerator / denominator)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def safe_log(expr: pl.Expr) -> pl.Expr:
|
|
16
|
+
"""Natural log, or null where the argument is non-positive (log-domain breach)."""
|
|
17
|
+
return pl.when(expr <= 0).then(None).otherwise(expr.log())
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def safe_sqrt(expr: pl.Expr) -> pl.Expr:
|
|
21
|
+
"""Square root, or null where the argument is negative."""
|
|
22
|
+
return pl.when(expr < 0).then(None).otherwise(expr.sqrt())
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = ["safe_div", "safe_log", "safe_sqrt"]
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Cross-sectional family: rank / standardize a per-symbol signal across the universe at each
|
|
2
|
+
# timestamp. Evaluated in two passes (registry.evaluate): the per-symbol SIGNAL (trailing,
|
|
3
|
+
# .over(symbol)) is materialized first, then the cross-sectional REDUCTION runs within each
|
|
4
|
+
# timestamp slice (.over(timestamp)) -- Polars cannot nest those two groupings in one expression.
|
|
5
|
+
# The frame must carry the complete cross-section at each timestamp (validate cross_sectional=True).
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from functools import partial
|
|
10
|
+
|
|
11
|
+
import polars as pl
|
|
12
|
+
|
|
13
|
+
from sabia._math import safe_log
|
|
14
|
+
from sabia.normalize import xs_rank, xs_zscore
|
|
15
|
+
from sabia.registry import XS_SIGNAL_COLUMN, RegisteredFeature, make_feature
|
|
16
|
+
from sabia.spec import Column, Cost, Family, Horizon, Recurrence
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def momentum_signal(
|
|
20
|
+
close: str = Column.CLOSE, *, window: int, symbol: str = Column.SYMBOL
|
|
21
|
+
) -> pl.Expr:
|
|
22
|
+
"""Per-symbol ``window``-bar log-return signal (the input to a cross-sectional reduction)."""
|
|
23
|
+
return safe_log(pl.col(close) / pl.col(close).shift(window)).over(symbol)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def volatility_signal(
|
|
27
|
+
close: str = Column.CLOSE, *, window: int, symbol: str = Column.SYMBOL
|
|
28
|
+
) -> pl.Expr:
|
|
29
|
+
"""Per-symbol realized-volatility signal over ``window`` bars."""
|
|
30
|
+
log_return = safe_log(pl.col(close) / pl.col(close).shift(1))
|
|
31
|
+
return log_return.rolling_std(window, min_samples=window).over(symbol)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _xs_rank(name: str, timestamp: str = Column.TIMESTAMP) -> pl.Expr:
|
|
35
|
+
return xs_rank(pl.col(XS_SIGNAL_COLUMN), over=timestamp).alias(name)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _xs_zscore(name: str, timestamp: str = Column.TIMESTAMP) -> pl.Expr:
|
|
39
|
+
return xs_zscore(pl.col(XS_SIGNAL_COLUMN), over=timestamp).alias(name)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
FEATURES: tuple[RegisteredFeature, ...] = (
|
|
43
|
+
make_feature(
|
|
44
|
+
momentum_signal,
|
|
45
|
+
build=partial(_xs_rank, "xs_rank_mom_252"),
|
|
46
|
+
signal=partial(momentum_signal, window=252),
|
|
47
|
+
name="xs_rank_mom_252",
|
|
48
|
+
family=Family.CROSS_SECTIONAL,
|
|
49
|
+
native_band=(Horizon.LONG,),
|
|
50
|
+
lookback=252,
|
|
51
|
+
min_history=253,
|
|
52
|
+
recurrence=Recurrence.FINITE,
|
|
53
|
+
effective_warmup=253,
|
|
54
|
+
cost_class=Cost.LINEAR,
|
|
55
|
+
inputs=(Column.CLOSE,),
|
|
56
|
+
citation="Jegadeesh & Titman (1993)",
|
|
57
|
+
params={"window": 252, "reduction": "rank"},
|
|
58
|
+
),
|
|
59
|
+
make_feature(
|
|
60
|
+
momentum_signal,
|
|
61
|
+
build=partial(_xs_zscore, "xs_zscore_ret_21"),
|
|
62
|
+
signal=partial(momentum_signal, window=21),
|
|
63
|
+
name="xs_zscore_ret_21",
|
|
64
|
+
family=Family.CROSS_SECTIONAL,
|
|
65
|
+
native_band=(Horizon.MEDIUM,),
|
|
66
|
+
lookback=21,
|
|
67
|
+
min_history=22,
|
|
68
|
+
recurrence=Recurrence.FINITE,
|
|
69
|
+
effective_warmup=22,
|
|
70
|
+
cost_class=Cost.LINEAR,
|
|
71
|
+
inputs=(Column.CLOSE,),
|
|
72
|
+
citation="cross-sectional relative strength",
|
|
73
|
+
params={"window": 21, "reduction": "zscore"},
|
|
74
|
+
),
|
|
75
|
+
make_feature(
|
|
76
|
+
volatility_signal,
|
|
77
|
+
build=partial(_xs_rank, "xs_rank_vol_63"),
|
|
78
|
+
signal=partial(volatility_signal, window=63),
|
|
79
|
+
name="xs_rank_vol_63",
|
|
80
|
+
family=Family.CROSS_SECTIONAL,
|
|
81
|
+
native_band=(Horizon.MEDIUM,),
|
|
82
|
+
lookback=63,
|
|
83
|
+
min_history=64,
|
|
84
|
+
recurrence=Recurrence.FINITE,
|
|
85
|
+
effective_warmup=64,
|
|
86
|
+
cost_class=Cost.LINEAR,
|
|
87
|
+
inputs=(Column.CLOSE,),
|
|
88
|
+
citation="low-volatility anomaly",
|
|
89
|
+
params={"window": 63, "reduction": "rank"},
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
__all__ = ["FEATURES", "momentum_signal", "volatility_signal"]
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Distribution family: rolling shape and downside moments of the return distribution. All FINITE,
|
|
2
|
+
# strictly trailing, panel-safe via .over(symbol). Zero-variance windows yield null, never NaN.
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from functools import partial
|
|
7
|
+
|
|
8
|
+
import polars as pl
|
|
9
|
+
|
|
10
|
+
from sabia._math import safe_log, safe_sqrt
|
|
11
|
+
from sabia.registry import RegisteredFeature, make_feature
|
|
12
|
+
from sabia.spec import Column, Cost, Family, Horizon, Recurrence
|
|
13
|
+
|
|
14
|
+
_BANDS = (Horizon.SHORT, Horizon.MEDIUM)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _log_return(close: str) -> pl.Expr:
|
|
18
|
+
return safe_log(pl.col(close) / pl.col(close).shift(1))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def skew(close: str = Column.CLOSE, *, window: int = 63, symbol: str = Column.SYMBOL) -> pl.Expr:
|
|
22
|
+
"""Rolling skewness of log returns over ``window`` bars. FINITE. A flat window yields null."""
|
|
23
|
+
value = _log_return(close).rolling_skew(window, min_samples=window).fill_nan(None)
|
|
24
|
+
return value.over(symbol).alias(f"skew_{window}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def kurtosis(
|
|
28
|
+
close: str = Column.CLOSE, *, window: int = 63, symbol: str = Column.SYMBOL
|
|
29
|
+
) -> pl.Expr:
|
|
30
|
+
"""Rolling excess kurtosis of log returns over ``window`` bars. FINITE. Flat window -> null."""
|
|
31
|
+
value = _log_return(close).rolling_kurtosis(window, min_samples=window).fill_nan(None)
|
|
32
|
+
return value.over(symbol).alias(f"kurtosis_{window}")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def downside_dev(
|
|
36
|
+
close: str = Column.CLOSE, *, window: int = 21, symbol: str = Column.SYMBOL
|
|
37
|
+
) -> pl.Expr:
|
|
38
|
+
"""Downside deviation: RMS of negative log returns over ``window`` bars. FINITE.
|
|
39
|
+
|
|
40
|
+
The semivariance behind the Sortino ratio -- only losses contribute. Citation: Sortino (1991).
|
|
41
|
+
"""
|
|
42
|
+
# clip (not min_horizontal) so a null return stays null instead of being imputed to 0.
|
|
43
|
+
downside = _log_return(close).clip(upper_bound=0.0)
|
|
44
|
+
variance = (downside**2).rolling_mean(window, min_samples=window)
|
|
45
|
+
return safe_sqrt(variance).over(symbol).alias(f"downside_dev_{window}")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
FEATURES: tuple[RegisteredFeature, ...] = (
|
|
49
|
+
make_feature(
|
|
50
|
+
skew,
|
|
51
|
+
build=partial(skew, window=63),
|
|
52
|
+
name="skew_63",
|
|
53
|
+
family=Family.DISTRIBUTION,
|
|
54
|
+
native_band=_BANDS,
|
|
55
|
+
lookback=63,
|
|
56
|
+
min_history=64,
|
|
57
|
+
recurrence=Recurrence.FINITE,
|
|
58
|
+
effective_warmup=64,
|
|
59
|
+
cost_class=Cost.LINEAR,
|
|
60
|
+
inputs=(Column.CLOSE,),
|
|
61
|
+
citation="rolling third moment",
|
|
62
|
+
params={"window": 63},
|
|
63
|
+
),
|
|
64
|
+
make_feature(
|
|
65
|
+
kurtosis,
|
|
66
|
+
build=partial(kurtosis, window=63),
|
|
67
|
+
name="kurtosis_63",
|
|
68
|
+
family=Family.DISTRIBUTION,
|
|
69
|
+
native_band=_BANDS,
|
|
70
|
+
lookback=63,
|
|
71
|
+
min_history=64,
|
|
72
|
+
recurrence=Recurrence.FINITE,
|
|
73
|
+
effective_warmup=64,
|
|
74
|
+
cost_class=Cost.LINEAR,
|
|
75
|
+
inputs=(Column.CLOSE,),
|
|
76
|
+
citation="rolling fourth moment",
|
|
77
|
+
params={"window": 63},
|
|
78
|
+
),
|
|
79
|
+
make_feature(
|
|
80
|
+
downside_dev,
|
|
81
|
+
build=partial(downside_dev, window=21),
|
|
82
|
+
name="downside_dev_21",
|
|
83
|
+
family=Family.DISTRIBUTION,
|
|
84
|
+
native_band=_BANDS,
|
|
85
|
+
lookback=21,
|
|
86
|
+
min_history=22,
|
|
87
|
+
recurrence=Recurrence.FINITE,
|
|
88
|
+
effective_warmup=22,
|
|
89
|
+
cost_class=Cost.LINEAR,
|
|
90
|
+
inputs=(Column.CLOSE,),
|
|
91
|
+
citation="Sortino (1991)",
|
|
92
|
+
params={"window": 21},
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
__all__ = ["FEATURES", "downside_dev", "kurtosis", "skew"]
|