Skip to main content
Orders are async calls on context. Place them in handle_data or in scheduled callbacks — not in initialize or before_trading_start.

Execution styles

Fixed quantity

await context.order(asset, amount, style, account_id=None, exchange_name=None)

Positive amount buys or covers; negative sells or shorts.

Fixed money amount

await context.order_value(asset, value, limit_price=None, stop_price=None, style=None, exchange_name=None)

Buy or sell value worth of the asset. Pass either a style or the shorthand prices: limit_price alone means a limit order, stop_price alone a stop, both a stop-limit, neither a market order. Combining style with a price raises.

Targets

await context.order_target(asset, target, style, exchange_name=None)

Adjust the position to a target number of shares or contracts.

await context.order_target_value(asset, target, style, exchange_name=None)

Adjust the position to a target notional value.

await context.order_target_percent(asset, target, style, exchange_name=None)

Adjust the position to a target share of portfolio value. target must be between -1 and 1. The quantity is reduced by the projected commission and slippage, so the order fits the cash it is sized against.

await context.order_percent(asset, percent, style, exchange_name=None)

Order an incremental percent of portfolio value relative to the current position. Not a target.

Rebalance a book

await context.rebalance(weights, style=None, exchange_name=None)

Hold exactly these weights and nothing else. Every listing named is ordered to its target weight, and every position not named is closed — the half that strategies forget, so a name drops out of the universe and quietly stays in the book.
  • Weights are shares of portfolio value, as in order_target_percent. They are not checked against 1.0, because a futures book is levered by construction.
  • style defaults to MarketOrder().
  • A leg whose market is shut at that moment is left exactly as it is — neither ordered nor closed (since 2.11.0).
  • Name only listings that have a price today: ordering one with no price raises.

Open orders

context.get_open_orders(asset=None)

Working orders. Pass an asset to filter. Cancelled and rejected orders are removed from this book.

context.get_order(order_id, exchange_name)

await context.cancel_order(order_id, exchange_name, relay_status=True)

Returned order object

Order methods return an Order, or None if no order was placed.

Fill timing

With next-bar execution an order placed in handle_data fills on the following bar, and handle_data sees a portfolio that already reflects earlier fills. Same-bar execution fills at the close of the bar the decision was made on — a price the market had not printed yet. The fill price is one bar field: close by default, or open, high, low (--fill-price / price_used_in_order_execution). No fill happens on a bar with no volume, and the slippage model caps a fill at a share of the bar’s volume, so a large order can fill over several bars.

Target orders and open orders

Target helpers size against the current position and do not count orders that haven’t filled yet. Two target calls before a fill over-order:
Guard with open orders, or use context.rebalance:

What raises

  • Ordering a ContinuousFuture — order await data.current_contract(cf) instead.
  • Ordering a FuturesContract from context.future_symbol — order the listing from futures_symbol, futures_chain or front_contract.
  • Ordering a listing whose market is closed — the error names the listing’s calendar and the simulation’s.
  • An order a trading control rejects, with the default on_error="fail".