Anatomy of a Perp · Ch. 08
Liquidations
You go 20× long and feel like a genius. The market slips 5%, barely a candle, and the whole position is gone, with a fee taken on top. You weren’t wrong about the direction. You were wrong about the distance.
The Idea
Intuition
Chapter 4 warned you: pile on leverage and you leave almost no room to be wrong. This is the moment that room runs out.
Your margin is the only money standing between you and a loss bigger than the one you signed up for. Let the price move far enough against you and it’s gone, and whoever took the other side would be left holding a position worth less than nothing. Liquidation is the system closing you out before that can happen, and it charges a small liquidation fee for doing it.
It doesn’t wait for your margin to hit zero. It steps in a little early, at a safety buffer called the maintenance margin. And the price it watches is the mark from last chapter, not the last trade, so a passing wick can’t trigger you. Watch the cushion drain as the mark falls, then push the price yourself.
The Math
How It’s Calculated
In plain terms: your cushion starts at one divided by your leverage and shrinks one-for-one as the mark moves against you. When it reaches the maintenance margin, you’re liquidated.
Your margin fraction, your equity as a share of the position, starts at one over your leverage and moves with the price:
You’re liquidated when that cushion falls to the maintenance margin . Solve for the price and you get the liquidation price:
For a short, the leverage and margin terms simply flip sign.
Note
This is the isolated-margin model: each position stands or falls on its own collateral. Cross-margin (shared collateral across positions) and partial liquidations (closing just enough to restore the buffer) change the mechanics. Those are later chapters.
War Story
My first liquidation engine did the obvious thing: on every price tick, check every open position to see if it had crossed its line. Correct, and hopelessly slow. With enough positions open, you simply can’t re-scan all of them on every tick, and a liquidation engine that falls behind the market is worse than no engine at all.
So we rearchitected it around one observation: not every position is equally close to the edge. We sorted them into three buckets by how much cushion they had left. Green for the comfortable ones, blue for the ones getting close, red for those right up against their liquidation price.
Then each bucket got its own heartbeat. Red checked on every price tick, blue every couple of seconds, green every five or six. The bet was about time: a position doesn’t fall from a fat green cushion to liquidated in one tick. That slide takes seconds, and it has to pass through blue and red first, where we’d catch it. So we spent the tick budget on the handful of red positions instead of the thousands of green ones, and lost nothing for it. The engine got dramatically faster and steadier.
The lesson stuck. Liquidation is a timing problem more than a math problem. The formula is the easy part. Watching thousands of cushions closely enough to act in time, without melting your infrastructure, is the real engineering. (That whole problem gets its own chapter, later.)
Field note: building a perp DEX