Skip to main content
A Ziplime strategy is a Python file with lifecycle functions. You do not subclass anything. Define the functions Ziplime should call and keep your strategy state on context.

File skeleton

initialize(context)

Required for most strategies. Ziplime calls it once before the first bar. Use it to:
  • Look up assets.
  • Store constants and mutable state on context.
  • Register scheduled callbacks.
  • Configure trading controls.
  • Attach pipelines.
  • Read algorithm configuration from context.algorithm.config.
Do not place orders in initialize. Order functions are only valid after initialization.

handle_data(context, data)

Required for trading logic. Ziplime calls it on every bar according to the simulation emission_rate. Use it to:
  • Read current or historical values from data.
  • Calculate signals.
  • Check cash, positions, and open orders.
  • Place, target, or cancel orders.
  • Record metrics for the result table.

before_trading_start(context, data)

Optional. Ziplime calls it once per session before normal bar processing. In the current runtime this function is called synchronously, so define it with def, not async def. Use it to:
  • Reset daily state.
  • Read pipeline output.
  • Prepare a universe for the day.
Do not place orders here. Order functions are explicitly disallowed during before_trading_start.

analyze(context, perf)

Optional. Ziplime calls it once after the simulation finishes. In the current runtime this function is called synchronously, so define it with def, not async def. perf is the final performance table created by the executor. It includes the recorded variables you created with context.record(...).

Storing state

Use attributes on context for anything that must persist across bars:
Avoid relying on mutable module-level globals for strategy state. They are harder to reset between runs and harder to reason about in tests.

Algorithm configuration

If your algorithm file defines a subclass of BaseAlgorithmConfig, Ziplime can load it from the JSON config passed to run_simulation(..., config_file=...).
Example JSON:

Common mistakes