Scheduling callbacks
context.schedule_function(func, date_rule=None, time_rule=None, half_days=True, calendar=None)
Register an async callback with the same signature as handle_data.
time_rule is ignored by the current scheduler because there is only one bar per session. In minute simulations, both date and time rules matter.
Date rules
Import fromziplime.utils.events or ziplime.api.
| Rule | Meaning |
|---|---|
date_rules.every_day() | Every trading day |
date_rules.week_start(days_offset=0) | Nth trading day of each week |
date_rules.week_end(days_offset=0) | N trading days before week end |
date_rules.month_start(days_offset=0) | Nth trading day of each month |
date_rules.month_end(days_offset=0) | N trading days before month end |
Time rules
| Rule | Meaning |
|---|---|
time_rules.market_open(minutes=30) | 30 minutes after market open |
time_rules.market_close(minutes=15) | 15 minutes before market close |
time_rules.every_minute() | Every minute in minute simulations |
Calendars
schedule_function accepts calendar=calendars.US_EQUITIES or calendar=calendars.US_FUTURES.
calendar is omitted, Ziplime uses the simulation trading calendar.
before_trading_start vs scheduled callbacks
Use before_trading_start for daily preparation that should happen before trading logic.
Use scheduled callbacks for trading actions that should occur at a specific session time.
| Hook | Can place orders? | Async? | Common use |
|---|---|---|---|
before_trading_start | No | No | Prepare universe, reset daily state, read pipeline output |
| Scheduled callback | Yes | Yes | Rebalance, exit, risk reduction |
handle_data | Yes | Yes | Main per-bar trading logic |
Trading controls
Controls are usually configured ininitialize.
Long-only
Maximum leverage
run_simulation(..., max_leverage=...) also registers a leverage control.
Maximum position size
asset to limit only that asset. Use asset=None to apply the limit globally.
Maximum order size
Maximum order count
Error behavior
Most controls accepton_error="fail" by default. Depending on the control implementation, "fail" raises when a rule is violated. Use non-failing modes only when you intentionally want violations to be logged or ignored by that control.
Asset restrictions
context.set_asset_restrictions(restrictions, on_error="fail") registers a restricted list.
Restriction classes are available from ziplime.finance.asset_restrictions and through ziplime.api:
Cancel policies
Import fromziplime.finance.cancel_policy or ziplime.api.
| Policy | Meaning |
|---|---|
EODCancel(warn_on_cancel=True) | Cancel open orders at session end. |
NeverCancel() | Leave open orders active until filled or manually canceled. |
Commission and slippage setup
Backtest fill models are most commonly configured inrun_simulation(...):
context.set_commission(...) and context.set_slippage(...) are available during initialization. Use them only when your exchange/blotter setup is designed to read those context-level models. For standard run_simulation usage, prefer the explicit runner parameters.