Vahag Byurat  ·  All Projects
Rust · Python · Quantitative Tooling

Options Risk Engine

A portfolio margin and liquidity engine built around a single question: where can I sell option premium without consuming buying power, and which of those trades are actually worth placing?

Rust · egui Python · zero dependencies Black–Scholes · Greeks 695 tests
The Problem
Most tools guess at margin. They don't have to.

When you hold a book of options across dozens of underlyings, two questions matter more than any Greek: how much buying power is this costing me, and how much more can I sell before it costs more. Broker platforms answer the first as an opaque number and the second not at all.

The conventional way to compute the answer is to simulate: pick a grid of prices, value the book at each one, take the worst. That's approximate by construction (the true worst case can fall between your grid points), and it's slow enough that nobody runs it across a whole portfolio interactively.

The worst case is always at a strike. So you can stop searching for it.

The Insight
Terminal payoff is piecewise-linear, so the minimum is exact

For one underlying, one expiration, one right, the terminal book value is V(P) = Σ qtyᵢ · max(0, P − Kᵢ). Every term is a hinge, and every hinge bends at a strike. A sum of hinges is piecewise-linear whose only breakpoints are those same strikes, so its minimum is attained at a strike, at zero, or in the limit. Nowhere else.

That makes margin = max(0, −min V) a closed-form answer from an O(n log n) sort and a single linear walk. No grid. No simulation. No pricing model at all: margin at expiration doesn't need one.

Illustrative ladder: one expiration, calls only +1  100 C
−2  110 C
+1  130 C
Walk the strikes: V(100) = 0  ·  V(110) = +10  ·  V(130) = −10.
Slope past 130 is 1−2+1 = 0, so it stays flat. Nothing lower exists.
Minimum −10 at K=130, so margin is 10 points.
V = 0 +10 min V = −10 = 10 points of margin 100 110 130 price at expiration →
Only the corners can be lowest, so checking the corners is not an approximation, it is the answer.
The corollary that makes it useful

Read the same walk from the other side and it stops being a risk number and becomes a capacity number. If the payoff floor above spot sits F points above water, a short vertical up to F wide adds exactly zero margin. It is already paid for by structure you own. Free width and distance-to-margin turn out to be the same quantity, measured in opposite directions.


Engineering Judgment
The decisions that mattered more than the math

A risk tool that is confidently wrong is worse than no tool. Most of the design effort went into refusing to produce numbers that look trustworthy and aren't.

Unbounded is a type, not a big number

A naked short call has no worst case: V → −∞. That is categorically different from "we couldn't compute it," and both are different from a large finite number. Each is its own value in the type system, so unbounded risk can never be rendered as a reassuring figure.

Missing signals renormalise out

Composite scores divide by the weight of the signals that actually scored, not the total weight. A missing input drops out instead of silently counting as zero, and coverage is reported alongside the score. There's a named regression test for the case where this flips a ranking.

Two floors, because one is blind

The global minimum is provably blind to anything on one side of spot: a pure call book has V(0)=0, so closing the very vertical that creates 90 points of width moves it by nothing. Every result therefore carries both the global floor and the floor around spot, and never collapses them into one.

Bisection over Newton, deliberately

Implied vol solves by bisection because Newton's method diverges where vega collapses toward zero: deep in or out of the money, exactly where a book accumulates. On a monotone function bisection cannot. Outside no-arbitrage bounds it returns nothing, which is a finding rather than a failure.

A spec that argues with itself

The algorithm document carries its own corrections in the open: three places where the original reasoning was wrong, what the symptom was, and what replaced it. The deep-ITM short vertical was first described as an interest-free loan against your own width; measuring it showed the interest term is real, so the spec now reports the signed number and explains it instead of clamping it to zero.


Proving It
Where the confidence comes from
695Python tests
3×Golden case asserted
independently
0Runtime dependencies
O(n log n)Exact margin
per group

The worked example in the specification is asserted to exact equality (not within a tolerance) three independent ways: against a hand-built ladder, through the CSV parser, and through the rendered explanation text. If the three ever disagree, one of them is lying, and the test says which.

The engine deliberately refuses to merge positions across underlyings: legs aggregate on (expiration, right, strike) with no symbol, and multi-symbol input raises rather than netting. A real book holds the same contract long in one name and short in another, and a silent merge would produce a confident number for a portfolio nobody owns.


Current Shape
A specification you can execute

Two codebases, on purpose. A Rust workspace (shared options-core library, an egui desktop front-end drawing payoff curves, and a thin CLI) carries the classifier that decomposes a raw position list into the structures a trader actually thinks in: calendars, diagonals, butterflies, condors, verticals, straddles. It explodes positions into unit lots, converts strikes to integer micro-dollars to sidestep float comparison, and greedily allocates them in priority order, asserting as an invariant that every contract is fully accounted for.

Alongside it, a pure-standard-library Python prototype implements the newer margin and capacity algorithms as an executable specification: readable, exhaustively tested, and explicitly written to be translated rather than shipped. The arithmetic is meant to be ported; the printing is meant to be thrown away. Calling it a finished product would be a nicer story and a less accurate one.

This project stays closed-source: it is coupled to real brokerage exports, and the interesting parts of a risk engine are inseparable from the book they were built against. Every position, ladder, and figure on this page is invented for illustration. The mathematics is the part worth sharing, and it needs no holdings to demonstrate.