context. The file is imported, so module-level code runs — keep it to imports, constants and definitions.
File skeleton
TradingAlgorithm and BarData is optional; the type hints give your editor completion on context and data.
Hooks
The engine substitutes a no-op for any hook you leave out.
initialize(context)
Use it to:
- Look up assets.
- Store constants and mutable state on
context. - Register scheduled callbacks and configure trading controls.
- Mount point-in-time datasets with
context.huggingface_dataset(...). - Attach pipelines.
- Read configuration from
context.algorithm.config.
initialize: there is no current bar yet.
handle_data(context, data)
Use it to read current or historical values, calculate signals, check cash, positions and open orders, place or cancel orders, and record metrics.
before_trading_start(context, data)
Optional, once per session before normal bar processing. Define it async def: every data read is a coroutine, and the engine awaits the hook when it is one. A plain def still runs, but any await data... call inside it is impossible.
Use it to reset daily state, read pipeline output or prepare the day’s universe. Orders are disallowed here.
analyze(context, perf)
Optional, once after the simulation finishes. perf is a pandas DataFrame with one row per session, including the columns you recorded with context.record(...).
compute_signals(context, prices) and WARMUP
Optional. Return indicators computed over whole arrays; read them bar by bar through context.signals. Declare how many bars of history they need with a module-level WARMUP. See Vectorized Signals.
Storing state
Use attributes oncontext for anything that must persist across bars:
Algorithm configuration
SubclassBaseAlgorithmConfig at module level, and the engine loads it from the JSON file passed as run_simulation(..., config_file=...).