Where you are. You have a mark price an attacker cannot easily shove. Now something has to act on it, and the interesting question is what happens when acting is not enough.
A trader is long with a thin margin. The price falls through their maintenance level. The engine closes the position, sells the inventory, and the trader’s margin covers the loss. Everything worked.
Now the price gaps. It was ninety a moment ago and it is seventy-two now, with nothing traded in between. The engine closes the position at seventy-two and the margin does not cover the loss. Somebody is owed money by somebody who does not have it.
The idea in one paragraph
A liquidation engine closes a position while its margin still covers the loss, which works whenever prices move continuously and fails whenever they jump. For the gap case a venue holds an insurance fund, paid for out of liquidation penalties in normal times, which absorbs shortfalls so that the winning side is still paid in full. When the fund is exhausted, there is nothing left except to take the money from the people who made it, by cutting the positions of profitable traders on the other side: auto-deleveraging, which is the mechanism nobody defends and every venue has, because the alternative is a venue that owes more than it holds.
The gap is the whole exercise
The exercise runs eight leveraged longs through a decline that falls gently from a hundred to ninety and then gaps straight to seventy-two.
Down to ninety, nothing happens: every position is above its maintenance level, and an engine checking every round would close each one with its margin intact. That is the case that makes engines look easy.
The gap changes everything at once. Five positions go straight through their margin without ever printing a price at which they could have been closed safely. The insurance fund absorbs their shortfalls in order until it is empty, and the last one arrives with nothing left to pay it, so a profitable position is cut instead.
| outcome | count |
|---|---|
| force-closed during the crash | 8 |
| shortfalls the insurance fund absorbed | 2,000, the whole fund |
| positions auto-deleveraged | 1 |
n = 8 trials · eight leveraged longs through a decline that gaps from 90 to 72
Why the last resort exists at all
The honest reason auto-deleveraging exists is arithmetic rather than policy.
If a losing trader owes more than their margin and the fund is empty, the money to pay the winner does not exist anywhere. A traditional exchange has other options at that point: it can pursue the member for the debt, call on a clearing house’s default fund, or draw on the mutualised resources module 5 described. An on-chain venue where the trader is an anonymous address has none of those, because there is nobody to pursue.
So the loss falls on the winners, and the design’s honesty is that it says so in advance and applies a published rule rather than improvising.
Check yourself
1. Why is closing a position at its maintenance level not always enough?
Because prices gap. The engine can only act at prices that actually print, and a position can go from healthy to owing money without any price in between at which it could have been closed safely.
2. What is the insurance fund for, and who pays for it?
It absorbs the shortfall when a liquidated position’s margin does not cover its loss, so that the winning side is still paid in full. It is funded from liquidation penalties collected in ordinary conditions, which means the traders who get liquidated in calm markets pay for the gaps.
3. What is auto-deleveraging, and why does every venue have one?
It is cutting a profitable trader’s position to cover a loss the fund cannot. Every venue has one because if the fund is empty and the loser cannot pay, the money to pay the winner does not exist; an on-chain venue has no member to pursue for the debt, so the loss falls on the winners.
4. Why is the ordering of shortfalls uncomfortable?
Because it decides who is deleveraged, and it is arbitrary. The same book in a different sequence exhausts the fund at a different point and cuts a different trader’s winning position. It is a real property of these systems and the best argument for sizing the fund so the last resort never arrives.
Do this
Run the crash and drain the fund.
python3 code/liquidation_engine.py
The starter provides the book, the gapping crash and the maintenance test, and leaves you the close: record and remove the position, take any shortfall out of the insurance fund, and when the fund cannot cover one, take what is left, record an auto-deleveraging and leave the fund at zero. The assertions check that most of the book closes, that some position goes through its margin, that the fund pays and never goes negative, and that exactly one position is deleveraged.
The completed version is in solutions/liquidation_engine.py.
What you can now do. You can run a liquidation engine through a gap, drain an insurance fund in order, and explain why the last resort exists and who it falls on. The next lesson asks how much margin the engine should have demanded in the first place.