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.2.tar.gz (38.4 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.2-py3-none-any.whl (42.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctalearn-0.1.2.tar.gz
  • Upload date:
  • Size: 38.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","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.2.tar.gz
Algorithm Hash digest
SHA256 1ac03dc0aa75b8ccf0452d3f52c06933dedc23b0d538c51e9e26abadbd99f07c
MD5 99ac53e0d740b258e1cf709c837173af
BLAKE2b-256 36d37138874b5e8d685fad74ad565fc92114debb7f12ea209508f5f439904184

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ctalearn-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 42.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 83ba1c971300a8c42dc70154a442b555bdaa4ac38e6de2182cc263b3fb1a771a
MD5 a9a8daa90b5149f97266f8865a2ab5ee
BLAKE2b-256 83dbea34e5c8fd319d7114d71c8524369e85391fb80e7649fe3782946bacff3a

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