Start here

Quickstart

Build a minimal strategy, run a synced backtest, and open the result in Strategy Center.

This quickstart takes you from an empty Python file to a synced AlphaLens Strategy Center run.

You will:

  1. Install alphalens-core.
  2. Create an AlphaLens API key.
  3. Write a small Algorithm.
  4. Run a cloud-synced backtest.
  5. Open the run in Strategy Center.

Install

Create or activate your Python environment, then install the hosted workflow extras:

bash
pip install "alphalens-core[live,cloud,fast]"

For research-only use, the base package works:

bash
pip install alphalens-core

Create an API key

Open Dashboard -> Settings -> API Keys in AlphaLens and create a key for your local machine.

The key is shown once. Put it in your shell or your strategy repo's .env.local:

bash
ALPHALENS_API_KEY=alens_...

You can also cache the key locally:

bash
alphalens connect --api-key alens_...

Create a strategy file

Create my_strategy.py:

python
from alphalens_core import Algorithm


class GoldenCross(Algorithm):
    start = "2020-01-01"
    end = "2025-12-31"
    universe = ["SPY"]
    resolution = "1day"
    initial_cash = 100_000
    benchmark_symbol = "SPY"

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

    def on_data(self, slice):
        if "SPY" not in slice:
            return

        history = self.history("SPY", 50)
        if len(history) < 50:
            return

        fast = history["close"].tail(10).mean()
        slow = history["close"].mean()
        target = 1.0 if fast > slow else 0.0
        self.set_holdings("SPY", target, tag="golden-cross")

The important parts are:

ComponentPurpose
Algorithm subclassThe strategy contract
Class attributesDefault run window, universe, resolution, cash, benchmark
initializeSetup, warmup, schedules, brokerage model
on_dataCalled for each completed bar
historyReads the current and prior bars
set_holdingsConverts target weights into orders

Read Build a strategy for the full strategy contract.

Run the backtest

From the folder containing my_strategy.py, run:

bash
ALPHALENS_API_KEY=alens_... alphalens run --strategy my_strategy:GoldenCross

The run happens locally. When cloud auth is configured, alphalens-core syncs the result to AlphaLens as a backtest:<run_id> source.

You can override class defaults without editing the file:

bash
alphalens run \
  --strategy my_strategy:GoldenCross \
  --start 2021-01-01 \
  --end 2025-12-31 \
  --cash 250000 \
  --benchmark SPY

View the result

Open Dashboard -> Strategy Center.

Select:

  1. Strategy: GoldenCross
  2. Run: the latest backtest

The dashboard uses the same Standard Portfolio components as broker accounts: performance, benchmark, drawdown, rolling Sharpe, trades, positions, and allocation.

Next steps

  • Add more symbols with universe.
  • Use history_arrays for faster cross-sectional signals.
  • Add sweep_grid and test parameter variants.
  • Start a paper deployment with alphalens live --strategy my_strategy:GoldenCross.