Free lesson · Putting it all together
7 / Turn targets into idempotent orders and handle partial fills
Open interactive lessonPractice calculationsExplore labs
Start with the idea
A target position says where you want to end up. An order is a request to change holdings. Include orders already working before submitting anything else.
Symbols, units & horizon
- q_target: desired final signed holdings in shares
- q_held: currently confirmed signed holdings
- q_working: net signed unfilled quantity of live orders
- Δq: additional signed quantity to submit, positive buy and negative sell
- all quantities: same instrument and lot units
When and why to use this
Use target-minus-held-minus-working arithmetic to connect portfolio construction to safe order-state handling.
Suppose target holdings are 25 shares, current holdings 10, and an earlier buy order still has 5 shares working. Only another 10 shares are uncommitted. Ignoring the working order can cause overbuying when both requests fill.
Separate desired, submitted, acknowledged, partially filled, filled, cancel-requested, canceled and rejected states. A cancel request does not erase exposure before acknowledgment; a late fill must still update the ledger. Reconcile with the venue after interruptions.
Use a stable client order identifier and query order status before retrying an uncertain submission. Idempotence means a repeated request does not create duplicate economic action. Keep data validation, position limits and cash checks outside the model. A stale quote or unresolved order state can justify pausing new submissions.
The capstone does not connect to a broker. Begin with deterministic replay, then a paper environment whose fill assumptions are understood. Distinguish order acceptance from an actual fill and keep timestamps for both.
7 / Turn targets into idempotent orders and handle partial fills
- Subtract confirmed holdings from the target to find the remaining desired change.
- Subtract net unfilled working quantity already committed toward that change.
- Check the result against cash, lot and position limits before submitting. Recalculate after every order-state or fill event.
Target 25, held 10 and working buy 5 imply Δq=10. If that working order fills, held becomes 15 and working becomes 0, leaving the same additional need of 10.
Apply it in a strategy
- Maintain a deterministic order-state record with unique client IDs.
- Run independent pre-trade checks and model pending exposure.
- Reconcile fills and unresolved orders using the execution lifecycle.
Research deliverable
Replay a partial fill, a submission timeout and a cancel/fill race without creating duplicate exposure.
Python implementation
Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.
def additional_order(target,held,working):
if not all(isinstance(q,int) for q in [target,held,working]): raise ValueError("Whole-share quantities required")
return target-held-working
print(additional_order(25,10,5),additional_order(25,15,0))Continue learning
Putting It All Together: Build a Complete Trading Research System — all lessons- 1 / Define the job and a small research contract
- 2 / Make a point-in-time data contract
- 3 / Turn an idea into a causal feature and a baseline
- 4 / Replay decisions into fills and net returns
- 5 / Add ML, regime models or RL only at a defined interface
- 6 / Convert forecasts into constrained portfolio positions
- 7 / Turn targets into idempotent orders and handle partial fills
- 8 / Reconcile fills, costs, cash and performance
- 9 / Run the miniature system and define the promotion decision
Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations