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.1.1.tar.gz (38.2 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.1-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctalearn-0.1.1.tar.gz
  • Upload date:
  • Size: 38.2 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.1.tar.gz
Algorithm Hash digest
SHA256 3284df63e4878e196f93cd7583a354ecf7bc3875e466a7a09d88c48f15ac52f4
MD5 925f7e4d6845f48b402f0aa81f79cacf
BLAKE2b-256 6ae767a72d8a37ff71f254227bb39c9f887e08593114203faf8a72ea2e8a4b46

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ctalearn-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 edbff84ce6d9aea706d86995fb2cd3729196343cc11c7a56411e0194d9d84c3e
MD5 95257c36aafc28ae35eeefa321bfb1cd
BLAKE2b-256 636b9270045f3543da5b525e0d52354705b072803fb152f895e48831613d7a93

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