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:
- Install
alphalens-core. - Create an AlphaLens API key.
- Write a small
Algorithm. - Run a cloud-synced backtest.
- Open the run in Strategy Center.
Install
Create or activate your Python environment, then install the hosted workflow extras:
pip install "alphalens-core[live,cloud,fast]"For research-only use, the base package works:
pip install alphalens-coreCreate 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:
ALPHALENS_API_KEY=alens_...You can also cache the key locally:
alphalens connect --api-key alens_...Create a strategy file
Create my_strategy.py:
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:
| Component | Purpose |
|---|---|
Algorithm subclass | The strategy contract |
| Class attributes | Default run window, universe, resolution, cash, benchmark |
initialize | Setup, warmup, schedules, brokerage model |
on_data | Called for each completed bar |
history | Reads the current and prior bars |
set_holdings | Converts target weights into orders |
Read Build a strategy for the full strategy contract.
Run the backtest
From the folder containing my_strategy.py, run:
ALPHALENS_API_KEY=alens_... alphalens run --strategy my_strategy:GoldenCrossThe 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:
alphalens run \
--strategy my_strategy:GoldenCross \
--start 2021-01-01 \
--end 2025-12-31 \
--cash 250000 \
--benchmark SPYView the result
Open Dashboard -> Strategy Center.
Select:
- Strategy:
GoldenCross - 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_arraysfor faster cross-sectional signals. - Add
sweep_gridand test parameter variants. - Start a paper deployment with
alphalens live --strategy my_strategy:GoldenCross.