ctalearn
A cross-sectional / time-series alpha research toolkit for CTA strategies.
ctalearn is built around a thin DataFrame wrapper over Polars
that guarantees a dense, regularly-sampled time grid. On top of it sits a library
of vectorized operators (cross-sectional, time-series, arithmetic, strategy),
a Numba-accelerated backtester, Glassnode data ingestion, and a small embedded
DSL for expressing alphas as plain text.
Key features
- Aligned
DataFrame— wrapspl.DataFrame, auto-detects bar frequency, fills gaps to a dense grid, and aligns operands automatically on arithmetic (+ - * /). - Operator library —
cs_*(across assets),ts_*(through time), element-wise transforms, and signal-to-weight strategies. Native Polars where possible; Numba only where Polars has no equivalent. - Alpha DSL — a Lark grammar for expressions like
cs_rank(ts_mean(close, 20) - open), with static type checking before execution. Ships its built-in function registry so consumers only provide their own data loaders. - Backtesting — Numba-accelerated
simulate_trade(weights, prices, fee). - Data — cached Glassnode REST ingestion, single-asset and parallel cross-sectional fetch.
Installation
Requires Python 3.11+.
pip install ctalearn
uv add ctalearn # with uv
Quick start
import polars as pl
from ctalearn import DataFrame
from ctalearn.operator import ts_mean, cs_rank
prices = DataFrame(
pl.DataFrame({
"ts": pl.datetime_range(..., interval="1d", eager=True),
"BTC": [...],
"ETH": [...],
}),
time_col="ts",
)
# 20-bar moving average, then rank across assets each bar.
signal = cs_rank(prices - ts_mean(prices, 20))
Alpha DSL
Express the same idea as text and run it through the interpreter:
from ctalearn.dsl import (
parser, TypeCheckTransformer, ExecutionTransformer,
BUILTIN_FUNCTIONS, BUILTIN_FUNCTION_SCHEMA, DslType,
)
code = "return cs_rank(close - ts_mean(close, 20));"
# Caller supplies only the factor side; functions come from the library.
factor_schema = {"close": DslType.DATAFRAME}
data_loaders = {"close": lambda: prices}
tree = parser.parse(code)
TypeCheckTransformer(factor_schema, BUILTIN_FUNCTION_SCHEMA).transform(tree)
result = ExecutionTransformer(BUILTIN_FUNCTIONS, data_loaders).transform(tree)
See examples/dsl_interpreter.py for a complete,
runnable integration.
Development
git clone https://github.com/fffelix-huang/ctalearn.git
cd ctalearn
uv sync # install / sync dependencies
uv run pytest # run tests (doctests + coverage; benchmarks skipped)
uv run pytest --benchmark-only # run only the benchmarks
uv run ruff check --fix # lint
uv run ruff format # format
uv run pre-commit run --all-files # all hooks
License
Release files for ctalearn 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ctalearn-0.3.0.tar.gz | 39.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ctalearn-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 84.4 kB
Release files / ctalearn-0.3.0.tar.gz
| Download URL | ctalearn-0.3.0.tar.gz |
|---|---|
| Size | 39.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5158600f358d74a35279dbb39ad07161b01652bd831ee29b72374ad969981b57
|
|
BLAKE2b-256 checksum How to use checksums |
d406acfcb7265bc9157a1b1ae8cd2ed781ab7c7ffeea38393ecb292af2b32e88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ctalearn-0.3.0-py3-none-any.whl
| Download URL | ctalearn-0.3.0-py3-none-any.whl |
|---|---|
| Size | 44.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a71cf1fd86a168cf89baecfe7506aabc3ecaa478e40c4c73fd4b1fc5c578ac9
|
|
BLAKE2b-256 checksum How to use checksums |
8ebfcbe0c1b3bd38d1dfe3e8a080c2585208427e1cdde14134fc0e14ccaee490
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|