contextstores your strategy state and exposes trading functions.datagives access to current and historical market data.- Lifecycle functions tell Ziplime when to set up, trade, prepare a session, and analyze results.
Mental model
initialize is your setup block. Look up assets, store parameters, register scheduled callbacks, and configure controls there.
handle_data is your trading block. It runs on every emitted bar, reads data, checks portfolio state, places orders, and records metrics.
before_trading_start is an optional daily preparation hook. Use it for daily universe selection or pipeline output. Do not place orders there.
analyze is an optional reporting hook after the backtest finishes.
Minimal algorithm
Main objects
| Object | Where you receive it | What it is for |
|---|---|---|
context | Every lifecycle function | Persistent strategy state and trading API |
data | handle_data, scheduled callbacks, before_trading_start | Current and historical data |
context.portfolio | Any time after initialization | Cash, portfolio value, positions, returns |
context.account | Any time after initialization | Account and leverage metrics |
perf | analyze(context, perf) | Final performance table |
Async rule of thumb
Useawait when calling functions that load data, look up assets, place orders, or cancel orders:
| Function | Should be async? | Notes |
|---|---|---|
initialize(context) | Yes | Ziplime awaits it. |
handle_data(context, data) | Yes | Ziplime awaits it on every bar. |
| Scheduled callbacks | Yes | Ziplime awaits callbacks registered by schedule_function. |
before_trading_start(context, data) | No | Called synchronously. |
analyze(context, perf) | No | Called synchronously after the run. |
API map
| Task | Primary functions |
|---|---|
| Look up assets | context.symbol, context.sid, context.future_symbol, context.symbols_universe |
| Read current data | await data.current(...) |
| Read trailing windows | await data.history(...) |
| Place orders | context.order, context.order_target, context.order_target_percent, context.order_percent, context.order_target_value |
| Inspect orders | context.get_open_orders, context.get_order, context.cancel_order |
| Inspect portfolio | context.portfolio, context.account, portfolio helper methods |
| Record metrics | context.record(...) |
| Schedule callbacks | context.schedule_function(...), date_rules, time_rules |
| Configure controls | context.set_long_only, context.set_max_leverage, context.set_max_position_size, context.set_max_order_size, context.set_max_order_count |
| Use pipelines | context.attach_pipeline, context.pipeline_output |
| Migrate old Zipline code | Migrating from Classic Zipline |