ctalearn 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.
- ctalearn-0.1.0/LICENSE +21 -0
- ctalearn-0.1.0/PKG-INFO +133 -0
- ctalearn-0.1.0/README.md +99 -0
- ctalearn-0.1.0/ctalearn/__init__.py +5 -0
- ctalearn-0.1.0/ctalearn/core/__init__.py +0 -0
- ctalearn-0.1.0/ctalearn/core/dataframe.py +578 -0
- ctalearn-0.1.0/ctalearn/data/__init__.py +1 -0
- ctalearn-0.1.0/ctalearn/data/_fetch.py +218 -0
- ctalearn-0.1.0/ctalearn/dsl/__init__.py +25 -0
- ctalearn-0.1.0/ctalearn/dsl/analyzer.py +165 -0
- ctalearn-0.1.0/ctalearn/dsl/exceptions.py +22 -0
- ctalearn-0.1.0/ctalearn/dsl/grammar.lark +29 -0
- ctalearn-0.1.0/ctalearn/dsl/interpreter.py +121 -0
- ctalearn-0.1.0/ctalearn/dsl/library.py +126 -0
- ctalearn-0.1.0/ctalearn/dsl/parser.py +8 -0
- ctalearn-0.1.0/ctalearn/dsl/schema.py +55 -0
- ctalearn-0.1.0/ctalearn/metrics/__init__.py +21 -0
- ctalearn-0.1.0/ctalearn/metrics/_performance_metrics.py +668 -0
- ctalearn-0.1.0/ctalearn/operator/__init__.py +87 -0
- ctalearn-0.1.0/ctalearn/operator/_arithmetic.py +138 -0
- ctalearn-0.1.0/ctalearn/operator/_cross_sectional.py +680 -0
- ctalearn-0.1.0/ctalearn/operator/_strategy.py +234 -0
- ctalearn-0.1.0/ctalearn/operator/_time_series.py +792 -0
- ctalearn-0.1.0/ctalearn/operator/_utils.py +57 -0
- ctalearn-0.1.0/ctalearn/preprocess/__init__.py +3 -0
- ctalearn-0.1.0/ctalearn/preprocess/_split.py +45 -0
- ctalearn-0.1.0/ctalearn/py.typed +0 -0
- ctalearn-0.1.0/ctalearn/testing/__init__.py +3 -0
- ctalearn-0.1.0/ctalearn/testing/dataframe.py +26 -0
- ctalearn-0.1.0/ctalearn.egg-info/PKG-INFO +133 -0
- ctalearn-0.1.0/ctalearn.egg-info/SOURCES.txt +34 -0
- ctalearn-0.1.0/ctalearn.egg-info/dependency_links.txt +1 -0
- ctalearn-0.1.0/ctalearn.egg-info/requires.txt +6 -0
- ctalearn-0.1.0/ctalearn.egg-info/top_level.txt +1 -0
- ctalearn-0.1.0/pyproject.toml +124 -0
- ctalearn-0.1.0/setup.cfg +4 -0
ctalearn-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ting-Hsuan Huang
|
|
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.
|
ctalearn-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ctalearn
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A cross-sectional / time-series alpha research toolkit for CTA strategies
|
|
5
|
+
Author-email: Ting-Hsuan Huang <felixhuang07@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/fffelix-huang/ctalearn
|
|
8
|
+
Project-URL: Documentation, https://fffelix-huang.github.io/ctalearn/
|
|
9
|
+
Project-URL: Repository, https://github.com/fffelix-huang/ctalearn
|
|
10
|
+
Project-URL: Issues, https://github.com/fffelix-huang/ctalearn/issues
|
|
11
|
+
Keywords: alpha,quant,trading,backtesting,cross-sectional,time-series,factor,polars,cta
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: numpy>=1.26.4
|
|
28
|
+
Requires-Dist: scipy>=1.16.2
|
|
29
|
+
Requires-Dist: httpx>=0.28.1
|
|
30
|
+
Requires-Dist: polars>=1.34.0
|
|
31
|
+
Requires-Dist: numba>=0.62.1
|
|
32
|
+
Requires-Dist: lark>=1.3.1
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# ctalearn
|
|
36
|
+
|
|
37
|
+
[](https://github.com/fffelix-huang/ctalearn/actions/workflows/ci.yml)
|
|
38
|
+
[](https://www.python.org/)
|
|
39
|
+
[](LICENSE)
|
|
40
|
+
|
|
41
|
+
A cross-sectional / time-series **alpha research toolkit** for CTA strategies.
|
|
42
|
+
|
|
43
|
+
**ctalearn** is built around a thin `DataFrame` wrapper over [Polars](https://pola.rs)
|
|
44
|
+
that guarantees a dense, regularly-sampled time grid. On top of it sits a library
|
|
45
|
+
of vectorized operators (cross-sectional, time-series, arithmetic, strategy),
|
|
46
|
+
a Numba-accelerated backtester, Glassnode data ingestion, and a small embedded
|
|
47
|
+
DSL for expressing alphas as plain text.
|
|
48
|
+
|
|
49
|
+
## Key features
|
|
50
|
+
|
|
51
|
+
- **Aligned `DataFrame`** — wraps `pl.DataFrame`, auto-detects bar frequency,
|
|
52
|
+
fills gaps to a dense grid, and aligns operands automatically on arithmetic
|
|
53
|
+
(`+ - * /`).
|
|
54
|
+
- **Operator library** — `cs_*` (across assets), `ts_*` (through time),
|
|
55
|
+
element-wise transforms, and signal-to-weight strategies. Native
|
|
56
|
+
Polars where possible; Numba only where Polars has no equivalent.
|
|
57
|
+
- **Alpha DSL** — a Lark grammar for expressions like
|
|
58
|
+
`cs_rank(ts_mean(close, 20) - open)`, with static type checking before
|
|
59
|
+
execution. Ships its built-in function registry so consumers only provide
|
|
60
|
+
their own data loaders.
|
|
61
|
+
- **Backtesting** — Numba-accelerated `simulate_trade(weights, prices, fee)`.
|
|
62
|
+
- **Data** — cached Glassnode REST ingestion, single-asset and parallel
|
|
63
|
+
cross-sectional fetch.
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
|
|
67
|
+
Requires Python 3.13+. Add it to your project straight from GitHub
|
|
68
|
+
(managed with [uv](https://docs.astral.sh/uv/)):
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
uv add git+https://github.com/fffelix-huang/ctalearn.git
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Quick start
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
import polars as pl
|
|
78
|
+
from ctalearn import DataFrame
|
|
79
|
+
from ctalearn.operator import ts_mean, cs_rank
|
|
80
|
+
|
|
81
|
+
prices = DataFrame(
|
|
82
|
+
pl.DataFrame({
|
|
83
|
+
"ts": pl.datetime_range(..., interval="1d", eager=True),
|
|
84
|
+
"BTC": [...],
|
|
85
|
+
"ETH": [...],
|
|
86
|
+
}),
|
|
87
|
+
time_col="ts",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# 20-bar moving average, then rank across assets each bar.
|
|
91
|
+
signal = cs_rank(prices - ts_mean(prices, 20))
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Alpha DSL
|
|
95
|
+
|
|
96
|
+
Express the same idea as text and run it through the interpreter:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from ctalearn.dsl import (
|
|
100
|
+
parser, TypeCheckTransformer, ExecutionTransformer,
|
|
101
|
+
BUILTIN_FUNCTIONS, BUILTIN_FUNCTION_SCHEMA, DslType,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
code = "return cs_rank(close - ts_mean(close, 20));"
|
|
105
|
+
|
|
106
|
+
# Caller supplies only the factor side; functions come from the library.
|
|
107
|
+
factor_schema = {"close": DslType.DATAFRAME}
|
|
108
|
+
data_loaders = {"close": lambda: prices}
|
|
109
|
+
|
|
110
|
+
tree = parser.parse(code)
|
|
111
|
+
TypeCheckTransformer(factor_schema, BUILTIN_FUNCTION_SCHEMA).transform(tree)
|
|
112
|
+
result = ExecutionTransformer(BUILTIN_FUNCTIONS, data_loaders).transform(tree)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
See [`examples/dsl_interpreter.py`](examples/dsl_interpreter.py) for a complete,
|
|
116
|
+
runnable integration.
|
|
117
|
+
|
|
118
|
+
## Development
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
git clone https://github.com/fffelix-huang/ctalearn.git
|
|
122
|
+
cd ctalearn
|
|
123
|
+
uv sync # install / sync dependencies
|
|
124
|
+
uv run pytest # run tests (doctests + coverage; benchmarks skipped)
|
|
125
|
+
uv run pytest --benchmark-only # run only the benchmarks
|
|
126
|
+
uv run ruff check --fix # lint
|
|
127
|
+
uv run ruff format # format
|
|
128
|
+
uv run pre-commit run --all-files # all hooks
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
[MIT](LICENSE)
|
ctalearn-0.1.0/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# ctalearn
|
|
2
|
+
|
|
3
|
+
[](https://github.com/fffelix-huang/ctalearn/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.python.org/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
A cross-sectional / time-series **alpha research toolkit** for CTA strategies.
|
|
8
|
+
|
|
9
|
+
**ctalearn** is built around a thin `DataFrame` wrapper over [Polars](https://pola.rs)
|
|
10
|
+
that guarantees a dense, regularly-sampled time grid. On top of it sits a library
|
|
11
|
+
of vectorized operators (cross-sectional, time-series, arithmetic, strategy),
|
|
12
|
+
a Numba-accelerated backtester, Glassnode data ingestion, and a small embedded
|
|
13
|
+
DSL for expressing alphas as plain text.
|
|
14
|
+
|
|
15
|
+
## Key features
|
|
16
|
+
|
|
17
|
+
- **Aligned `DataFrame`** — wraps `pl.DataFrame`, auto-detects bar frequency,
|
|
18
|
+
fills gaps to a dense grid, and aligns operands automatically on arithmetic
|
|
19
|
+
(`+ - * /`).
|
|
20
|
+
- **Operator library** — `cs_*` (across assets), `ts_*` (through time),
|
|
21
|
+
element-wise transforms, and signal-to-weight strategies. Native
|
|
22
|
+
Polars where possible; Numba only where Polars has no equivalent.
|
|
23
|
+
- **Alpha DSL** — a Lark grammar for expressions like
|
|
24
|
+
`cs_rank(ts_mean(close, 20) - open)`, with static type checking before
|
|
25
|
+
execution. Ships its built-in function registry so consumers only provide
|
|
26
|
+
their own data loaders.
|
|
27
|
+
- **Backtesting** — Numba-accelerated `simulate_trade(weights, prices, fee)`.
|
|
28
|
+
- **Data** — cached Glassnode REST ingestion, single-asset and parallel
|
|
29
|
+
cross-sectional fetch.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Requires Python 3.13+. Add it to your project straight from GitHub
|
|
34
|
+
(managed with [uv](https://docs.astral.sh/uv/)):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
uv add git+https://github.com/fffelix-huang/ctalearn.git
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quick start
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import polars as pl
|
|
44
|
+
from ctalearn import DataFrame
|
|
45
|
+
from ctalearn.operator import ts_mean, cs_rank
|
|
46
|
+
|
|
47
|
+
prices = DataFrame(
|
|
48
|
+
pl.DataFrame({
|
|
49
|
+
"ts": pl.datetime_range(..., interval="1d", eager=True),
|
|
50
|
+
"BTC": [...],
|
|
51
|
+
"ETH": [...],
|
|
52
|
+
}),
|
|
53
|
+
time_col="ts",
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# 20-bar moving average, then rank across assets each bar.
|
|
57
|
+
signal = cs_rank(prices - ts_mean(prices, 20))
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Alpha DSL
|
|
61
|
+
|
|
62
|
+
Express the same idea as text and run it through the interpreter:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from ctalearn.dsl import (
|
|
66
|
+
parser, TypeCheckTransformer, ExecutionTransformer,
|
|
67
|
+
BUILTIN_FUNCTIONS, BUILTIN_FUNCTION_SCHEMA, DslType,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
code = "return cs_rank(close - ts_mean(close, 20));"
|
|
71
|
+
|
|
72
|
+
# Caller supplies only the factor side; functions come from the library.
|
|
73
|
+
factor_schema = {"close": DslType.DATAFRAME}
|
|
74
|
+
data_loaders = {"close": lambda: prices}
|
|
75
|
+
|
|
76
|
+
tree = parser.parse(code)
|
|
77
|
+
TypeCheckTransformer(factor_schema, BUILTIN_FUNCTION_SCHEMA).transform(tree)
|
|
78
|
+
result = ExecutionTransformer(BUILTIN_FUNCTIONS, data_loaders).transform(tree)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
See [`examples/dsl_interpreter.py`](examples/dsl_interpreter.py) for a complete,
|
|
82
|
+
runnable integration.
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
git clone https://github.com/fffelix-huang/ctalearn.git
|
|
88
|
+
cd ctalearn
|
|
89
|
+
uv sync # install / sync dependencies
|
|
90
|
+
uv run pytest # run tests (doctests + coverage; benchmarks skipped)
|
|
91
|
+
uv run pytest --benchmark-only # run only the benchmarks
|
|
92
|
+
uv run ruff check --fix # lint
|
|
93
|
+
uv run ruff format # format
|
|
94
|
+
uv run pre-commit run --all-files # all hooks
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
[MIT](LICENSE)
|
|
File without changes
|