Skip to main content

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

Project description

ctalearn

CI PyPI 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.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

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.2.0.tar.gz (39.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.2.0-py3-none-any.whl (44.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctalearn-0.2.0.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","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.2.0.tar.gz
Algorithm Hash digest
SHA256 b8928212f4e486cf265b0a41ea7cea54fd92e4861465f8acb1c6949deed6d377
MD5 a825d3af2444eba9566890d8832b7083
BLAKE2b-256 ed1730dbf935cf0cf843cd5d295c64278f90ab8c8340eca9b0053b66205a5c53

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ctalearn-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 44.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31d38434b5bc9d0c3f25563f057447197957517dd36c847f9ad0d6c7ee25a7ad
MD5 01c5ad419210d0edf081fa7aa0ef1371
BLAKE2b-256 73aaf1f9bb80d767aef6c21bee54ea71becd835b169f1fb72aafdaa8d67d9dd4

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