Where you are. Fourteen lessons built an amplifier and the machinery that contains it. This project runs both halves together, over one seeded day, with every cash and collateral flow posted to the miniledger so that nothing can be asserted without being recorded.
The brief
Four things, one script.
Margin. Post initial margin, then run the thirty-day seeded price path against a sixty-contract book. Apply each day’s variation margin, and when the balance falls below maintenance, post a top-up back to the initial level and count the call. Every one of those movements is a real posting on the broker’s ledger, not a number in a variable.
Funding. Run sixty intervals of the perpetual’s funding loop from a mark eight points above its index, and confirm the gap closes to under a quarter of where it began.
Measure. Compute the Greeks at the money from the binomial pricer you built in lesson 6, by the same finite differences lesson 7 used.
Clear. Novate the seeded trade set into the CCP, check the nets sum to zero, and walk a 550 default loss through the stack - confirming it stops at the CCP’s own capital before reaching the surviving members.
Then the assertion that ties it together: the broker’s balance sheet still balances after every flow.
The idea in one paragraph
This module told one story twice: leverage multiplies a position, and margin, funding and clearing are the machinery that stops the multiplication cascading when somebody fails. The project runs both halves at once over a single seeded day, so the amplifier and the safety net are exercised against the same prices rather than in separate demonstrations. Margin is called and restored, a contract with no expiry is tethered by funding, an option position is measured before the market moves, and a clearing house absorbs a member’s default in the agreed order. Every one of those movements is a posting on the ledger, which is why the final check is that the balance sheet still balances rather than that any risk number came out right.
Why every flow is posted
The exercise could track margin in a floating-point variable and print the same numbers. It posts each movement to the ledger instead, and the difference is not decoration.
A margin engine that is right about the market and wrong about the ledger is the failure this whole module has been circling. Collateral that was demanded but never received, a top-up recorded twice, a payment that left one account without arriving anywhere - each of those produces a system that believes it is protected and is not. Posting every flow means the arithmetic has to close, and the miniledger’s invariants refuse anything that does not.
That is why the final assertion is about the balance sheet rather than about any of the risk numbers.
What the numbers say
Two margin calls over the month, because the path falls from 100 to about 87.46 and the cushion is repeatedly rebuilt. The funding gap closes from eight to a fraction of a point. Delta sits a little above a half at the money with positive gamma, exactly as an at-the-money call should. The three members net to zero against the hub, and the 550 loss is absorbed with the survivors untouched.
None of those numbers came from anywhere except your own code, which is the point the course has been making since module 0: every claim in this module is one you can run.
What module 6 does with this
You now have the whole legacy machine. Modules 0 to 3 moved money; module 4 built the markets on top; module 5 built the leverage those markets run on and the safety net that keeps it standing.
Module 6 turns to the claim module 0 introduced and left deliberately untested: that message, settlement and asset can be fused into one object on a shared ledger. Every mechanism you have built is about to be rebuilt on different foundations, and having built the originals is the only way to judge whether the rebuild is an improvement.
Check yourself
1. Why does the project post every margin movement to a ledger rather than tracking a balance in a variable?
Because a margin engine that is right about the market and wrong about its own records is exactly the failure the module is about: collateral demanded but never received, or recorded twice. Posting each flow forces the arithmetic to close, and the ledger’s invariants reject anything that does not.
2. Every mechanism in this module is mechanical rather than discretionary. Why is that deliberate?
Because these mechanisms have to work under stress, at speed, and often out of hours. A rule that requires a judgement call fails precisely when everybody is busy and frightened. The judgement is spent in advance, when the thresholds, ratios and loss ordering are agreed.
3. The final assertion is about the balance sheet rather than about any risk number. Why is that the right last check?
Because every risk number is computed from the recorded state, so if the records are inconsistent the risk numbers are meaningless regardless of how carefully they were derived. Checking that the sheet balances verifies the foundation the other four checks stand on.
4. Two calls landed during the month. What would it mean if the same path produced none?
That the position was small enough for the cushion to absorb the whole decline, or that the maintenance level was set too low to bite. Neither is wrong in itself, but it would mean the exercise was not exercising the mechanism it exists to demonstrate - which is why the position is sized so that the seeded path genuinely breaches it.
Do this
Run the module once, end to end.
python3 code/margin_engine.py
The starter provides the components and leaves you the driver: post initial margin and run the margin cycle, run the funding loop, compute the Greeks, novate the trades and walk the default, then report all of it. The assertions check the calls, the funding convergence, the Greeks’ signs, the netting and the waterfall’s first layer, and finally that the broker’s sheet balances. Success prints all four sections and the line:
margined, funded, measured and cleared: the safety net holds the amplifier
The completed version is in solutions/margin_engine.py.
What you can now do. You can run a margin engine that calls collateral when it should, tether a contract with no expiry, measure an option position’s behaviour before the market moves, and clear a book through a hub that survives a member failing - with every flow recorded and the books still balancing. That is the legacy machine, complete. The next module asks what happens when somebody proposes replacing it.