Lesson 5 · 14 min
Order types & execution
Market, limit, and stop orders — and how the PortfolioEngine simulates fills.
OrdersSlippageRebalance
An order is an instruction to your broker. Beginners hear jargon — market, limit, stop — but in systematic trading you usually encode intent in `set_target_weights` and let infrastructure translate to orders.
Definition: Execute now at the best available price.
Pros
Certainty of fill (if liquidity exists).
Cons
Price uncertainty — you pay the spread + slippage.
Quant Buffet: PortfolioEngine rebalances at the daily close with slippage applied — similar to a market-on-close intent.
How Quant Buffet executes in code
def on_day(engine, dt):
# Target 60% SPY, 40% TLT — engine sells/buys to match
engine.set_target_weights(dt, {"SPY": 0.6, "TLT": 0.4})- Rebalance uses the daily close as the reference price.
- Slippage (2 bps) makes buys slightly more expensive, sells slightly cheaper.
- Commission (5 bps) charged on notional each fill.
- Sells happen before buys so cash is available.
- Identical weights on consecutive days are skipped to reduce churn.
Long-only weight math
| Weights | Interpretation |
|---|---|
{"SPY": 1.0} | 100% in SPY, 0% cash |
{"SPY": 0.5, "TLT": 0.5} | Fully invested, equal split |
{"SPY": 0.6, "TLT": 0.3} | 90% invested, 10% cash |
{} or all zeros | Move to cash (if engine had positions) |