Skip to main content

AlphaLens — a LEAN-lite event-driven backtesting & walk-forward engine

Project description

alphalens-core

A LEAN-lite, event-driven backtesting & walk-forward engine for systematic trading strategies.

Write a strategy as a Python class, run a single forward pass over an in-memory OHLCV feed, and get back an equity curve, a trade log, and a rich set of performance statistics — plus walk-forward analysis, parameter sweeps, and an HTML dashboard. alphalens-core is the open engine behind AlphaLens.

Install

pip install alphalens-core              # core engine (pandas + numpy only)
pip install "alphalens-core[viz]"       # + matplotlib / quantstats dashboard & reports
pip install "alphalens-core[polygon]"   # + Polygon market-data fetching
pip install "alphalens-core[fast]"      # + pyarrow parquet cache

Quickstart

from alphalens_core import Algorithm, Backtester

class GoldenCross(Algorithm):
    start = "2020-01-01"
    end = "2024-12-31"
    universe = ["SPY"]

    def initialize(self):
        self.set_warmup(50)

    def on_data(self, slice):
        hist = self.history("SPY", 50)["close"]
        if len(hist) < 50:
            return
        self.set_holdings("SPY", 1.0 if hist.tail(10).mean() > hist.mean() else 0.0)

result = Backtester().run(GoldenCross)
print(result.stats)     # Sharpe, Sortino, CAGR, max drawdown, win rate, …
result.dashboard        # inline HTML report in Jupyter, or opens in your browser

Live data needs a POLYGON_API_KEY (a .env / .env.local file is loaded automatically). To run fully offline, inject your own DataFeed.

Features

  • LEAN-style lifecycleinitialize, on_data, on_order_event, on_securities_changed, on_end_of_day, on_warmup_finished.
  • Realistic fills — next-bar execution with pluggable slippage / fee / fill models, and a one-bar look-ahead guard so decisions on bar t can't peek at bar t's close.
  • Walk-forward analysis with per-fold parameter search.
  • Parameter sweeps + PBO (probability of backtest overfitting).
  • Simulated brokerage that mirrors a live-broker API, so a live adapter drops in without strategy changes.
  • Reportingresult.stats, an HTML result.dashboard, and quantstats reports.

CLI

alphalens run --strategy mymod:MyStrategy
alphalens wfa --strategy mymod:MyStrategy --train 504 --test 63 --step 63
alphalens cache --info

License

Apache-2.0 © AlphaLens LLC

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

alphalens_core-0.1.0.tar.gz (148.9 kB view details)

Uploaded Source

Built Distribution

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

alphalens_core-0.1.0-py3-none-any.whl (133.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alphalens_core-0.1.0.tar.gz
  • Upload date:
  • Size: 148.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for alphalens_core-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf2f6d7f134cc58d7b0f7bb5e7331f0110bab12d2a25dd4e44941ee241eb8aa2
MD5 9963a4172e3470619bfbb0bb1601d5c5
BLAKE2b-256 ace38975a6e1b3a34a8bb8a493397bfa7a3923201df436572f86334f44187e15

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alphalens_core-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 133.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for alphalens_core-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 510991ec660398e099891b6013b562d2d5c298f1f83d0489661a5e5018950c55
MD5 e63dcdc0be3fc38ff2f73f1c32951ae4
BLAKE2b-256 672e1a655ec64f8f185ebf99ab6c4547108d4f9cad2b49e689bd57b2e001ef87

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