Skip to main content
Use scheduled functions or calendar gates when your strategy should act at specific times instead of on every bar. Use controls to enforce risk rules before an order is accepted.

Calendar gates

The simplest way to rebalance weekly or monthly is a gate at the top of handle_data:
Both read the simulation’s trading calendar, so holidays are handled without date arithmetic. They are plain calls, not awaited.

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.
  • date_rule defaults to every trading day.
  • time_rule works at any intraday emission rate — 1 minute, 5 minutes, an hour. On a daily run there is one bar per session, so the time rule is ignored.
  • Pass the time rule by keyword. A time rule in the date_rule position is accepted with a warning.

Date rules

Time rules

Offsets must be between 1 minute and 12 hours.

Calendars

schedule_function uses the simulation’s trading calendar unless you pass calendar=calendars.US_EQUITIES or calendar=calendars.US_FUTURES:
Any other value raises. For runs that mix venues, see Venues & Calendars.

Which hook for what

Trading controls

Controls are configured in initialize and checked against every order before it is placed. Since 2.10.3 they are enforced — earlier versions accepted the limits and enforced none.

Error behavior

With on_error="fail" (the default) a violating order raises TradingControlViolation. With on_error="log" the violation is logged as an error and the order goes through.

Asset restrictions

Restricted assets also report False from data.can_trade.

Cancel policies

Commission and slippage

Costs belong to the run, not to the strategy: pass them to run_simulation(...) or the CLI. See Costs & Commissions.