# Execution Simulation and Backtest Engines

Model orders and fills without using unavailable future information.

Use this workbook alongside the course. Write your answers before opening the solutions. Practical work is self-reviewed; scored knowledge checks are in the Academy.

## 1. Event model

### Market and order events

An event-driven simulator represents market observations, order requests, acknowledgements and executions as distinct events. Specify which event can change which state. A price update should not silently imply an order fill without applying the fill rules.

### Event ordering

Ordering matters when events share a timestamp. Use a deterministic tie-break policy consistent with the available data and state its limitations. Choosing favorable order sequences retrospectively can create artificial performance.

### Clock and latency

Decision, submission and arrival times differ. Model latency explicitly where it matters, including uncertainty. A strategy cannot execute before its input is available or before the modeled order could reach the venue.

### State transitions

State transitions should be explicit: created, submitted, accepted, partially filled, filled, cancel pending, cancelled or rejected as appropriate. Invalid transitions should fail visibly rather than be repaired by silently inventing an event.

### Worked example

A signal uses the closing value at 10:00, but the simulator fills at 09:59:59. That execution precedes the information required to create the signal and violates causality.

### Independent exercise

Write an invariant connecting feature availability, decision time, submission and earliest fill time.

My inputs and assumptions:

My calculation or decision:

Evidence that would change my conclusion:


## 2. Fill model

### Bid and ask

A buy market order interacts with ask-side liquidity and a sell with bid-side liquidity under the modeled venue. Midpoint execution is a separate assumption that may be unrealistic. Include quantity and price-level depth where the data supports it.

### Limit-order uncertainty

Limit fills require assumptions about queue position and available opposing flow. A touched price alone is insufficient evidence. Report conservative and alternative models when queue information is missing rather than claiming exact fills.

### Partial fills

Partial fills change both position and remaining order quantity. Each execution must update accounting once. A model that treats every partial fill as the full requested size can create impossible exposure.

### Stops and gaps

Stops and gaps require explicit trigger and execution conventions. A stop activation does not guarantee the stop price. Ambiguous OHLC paths should be flagged or handled under a predefined rule, not resolved favorably after seeing P&L.

### Worked example

A limit order requests five units but available modeled quantity is two. Filling all five at the best price assumes liquidity beyond the modeled evidence. The remaining three need an explicit subsequent path.

### Independent exercise

Define the resulting order and position state after the two-unit fill, then describe two possible later events.

My inputs and assumptions:

My calculation or decision:

Evidence that would change my conclusion:


## 3. Accounting

### Position lots

Lot accounting determines how entries and exits are matched. FIFO, average-cost or another convention can change intermediate realized P&L while total economics still need reconciliation. State the chosen method and preserve raw executions.

### Fees and financing

Fees and financing should be applied at the events or intervals defined by the model. Distinguish per-side charges from costs already represented in fill prices. Include currency conversion under an explicit convention.

### Realized and unrealized P&L

Realized and unrealized P&L contribute differently to balance and equity. A simulator must mark open positions consistently and avoid recognizing the same price movement twice when a position closes.

### Cash and margin state

Cash and margin state can constrain which orders are allowed or force liquidation under modeled rules. A strategy that ignores funding obligations can appear feasible even when its path could not be maintained.

### Worked example

A position has an unrealized gain of 20 immediately before closing at the same mark. After close, realized P&L rises by 20 and unrealized P&L falls by 20; equity should not gain another 20 solely from the reclassification.

### Independent exercise

Write an accounting invariant for closing a position at the current mark with no additional costs.

My inputs and assumptions:

My calculation or decision:

Evidence that would change my conclusion:


## 4. Verification

### No-lookahead cases

Use no-lookahead cases in which a future price would tempt an incorrect early action. The expected result should come from an independently specified event trace, not from the same simulator logic.

### Cancel-fill races

Test cancel-fill races and duplicate events. A repeated execution message should not double exposure, and a cancel acknowledgement should not erase an earlier valid fill. Stable identities and transition rules are essential.

### Missing data behavior

Missing data should invoke a documented policy. A simulator must not silently fill an order through an interval with no supporting observations while claiming exact realism. Mark affected outcomes and assess sensitivity.

### Compare simulation against recorded executions

Compare simulated behavior with recorded executions only after matching instrument, inputs and timing. Differences reveal model limitations but may also reflect incomplete observation of the live environment. Report the limits rather than forcing exact agreement by hindsight tuning.

### Worked example

A fill event is replayed twice after a restart. The correct accounting should recognize its stable identity and leave exposure unchanged on the second delivery.

### Independent exercise

Design a regression test for this duplicate and a separate test for a fill arriving before a cancel acknowledgement.

My inputs and assumptions:

My calculation or decision:

Evidence that would change my conclusion:


## Course project

Implement or specify a simulator and demonstrate its behavior on adversarial price paths.

### Self-review rubric

- Concepts and reasoning: 25%
- Calculations, data and evidence: 30%
- Process and risk controls: 25%
- Limitations and communication: 20%

Record one correction and one next practice task. This rubric is not automatically graded.

## Worked solutions

### Exercise 1

Require availability no later than decision, decision no later than submission and fill no earlier than the assumed executable arrival. Document any timestamp uncertainty instead of using it to justify a favorable impossible sequence.

### Exercise 2

The position increases by two and remaining order quantity becomes three. Later opposing flow may fill more, or a confirmed cancellation may remove the remainder. Neither event should be assumed without applying its rules.

### Exercise 3

Equity should remain unchanged by the realized/unrealized transfer alone. Quantity becomes zero and the gain moves into realized accounting. Any equity change requires a new price, cost or cash-flow event.

### Exercise 4

Replay the duplicate and assert unchanged position, cash and fill count. For the race, retain the valid fill and cancel only remaining quantity. Expected states must be specified independently of the implementation.

## Further reading

- https://www.cmegroup.com/education/courses/introduction-to-futures
- https://docs.python.org/3/tutorial/
