Skip to main content
This guide documents the language surface you use inside a Ziplime algorithm file. An algorithm file is ordinary Python, but Ziplime gives it a small event-driven API:
  • context stores your strategy state and exposes trading functions.
  • data gives access to current and historical market data.
  • Lifecycle functions tell Ziplime when to set up, trade, prepare a session, and analyze results.
Most strategy work happens in two functions:
async def initialize(context):
    ...

async def handle_data(context, data):
    ...

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

from ziplime.finance.execution import MarketOrder

SYMBOLS = ["AAPL", "MSFT", "NVDA"]


async def initialize(context):
    context.assets = [await context.symbol(symbol) for symbol in SYMBOLS]
    context.has_rebalanced = False


async def handle_data(context, data):
    if context.has_rebalanced:
        return

    weight = 1.0 / len(context.assets)
    for asset in context.assets:
        await context.order_target_percent(
            asset=asset,
            target=weight,
            style=MarketOrder(),
        )

    context.has_rebalanced = True
    context.record(target_weight=weight, cash=context.portfolio.cash)

Main objects

ObjectWhere you receive itWhat it is for
contextEvery lifecycle functionPersistent strategy state and trading API
datahandle_data, scheduled callbacks, before_trading_startCurrent and historical data
context.portfolioAny time after initializationCash, portfolio value, positions, returns
context.accountAny time after initializationAccount and leverage metrics
perfanalyze(context, perf)Final performance table

Async rule of thumb

Use await when calling functions that load data, look up assets, place orders, or cancel orders:
from ziplime.finance.execution import MarketOrder

asset = await context.symbol("AAPL")
prices = await data.history(assets=[asset], fields=["close"], bar_count=20)
order = await context.order_target_percent(asset, 0.25, style=MarketOrder())
In the current runtime:
FunctionShould be async?Notes
initialize(context)YesZiplime awaits it.
handle_data(context, data)YesZiplime awaits it on every bar.
Scheduled callbacksYesZiplime awaits callbacks registered by schedule_function.
before_trading_start(context, data)NoCalled synchronously.
analyze(context, perf)NoCalled synchronously after the run.

API map

TaskPrimary functions
Look up assetscontext.symbol, context.sid, context.future_symbol, context.symbols_universe
Read current dataawait data.current(...)
Read trailing windowsawait data.history(...)
Place orderscontext.order, context.order_target, context.order_target_percent, context.order_percent, context.order_target_value
Inspect orderscontext.get_open_orders, context.get_order, context.cancel_order
Inspect portfoliocontext.portfolio, context.account, portfolio helper methods
Record metricscontext.record(...)
Schedule callbackscontext.schedule_function(...), date_rules, time_rules
Configure controlscontext.set_long_only, context.set_max_leverage, context.set_max_position_size, context.set_max_order_size, context.set_max_order_count
Use pipelinescontext.attach_pipeline, context.pipeline_output
Migrate old Zipline codeMigrating from Classic Zipline
  1. Algorithm file
  2. Context API
  3. Market data
  4. Orders
  5. Portfolio and recording
  6. Scheduling and controls
  7. Pipelines
  8. Migrating from Classic Zipline