Skip to main content

A cross-sectional / time-series alpha research toolkit for CTA strategies

Project description

ctalearn

CI Python License: MIT

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 — wraps pl.DataFrame, auto-detects bar frequency, fills gaps to a dense grid, and aligns operands automatically on arithmetic (+ - * /).
  • Operator librarycs_* (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.13+. Add it to your project straight from GitHub (managed with uv):

uv add git+https://github.com/fffelix-huang/ctalearn.git

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

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ctalearn-0.1.0.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ctalearn-0.1.0-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

Details for the file ctalearn-0.1.0.tar.gz.

File metadata

  • Download URL: ctalearn-0.1.0.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","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}

File hashes

Hashes for ctalearn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4935651e4b8b6e9b785fe517772fbe3e283a31cd8fafe2a853b03b105bcc581
MD5 fc1ff3cf05270c8cd40c383842f30126
BLAKE2b-256 dd9841a5377e52fbe555d347132128622de5cc109b0fa0b0d0bf451917d4502e

See more details on using hashes here.

File details

Details for the file ctalearn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ctalearn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","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}

File hashes

Hashes for ctalearn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5cfbc80dc4b65c85ffe3123c7736b02e6f71b08ee92bbc2855fac1c2c1530c3f
MD5 76c98600081dd966958471f288b2c166
BLAKE2b-256 6d79601cfe2740a44995610ea94c44ccbac70f015312d7a2bb7c083c45f59938

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page