Vahag Byurat  ·  All Projects
Rust · Python · Quantitative Tooling

Options Risk Engine

A portfolio margin and liquidity engine for bounded, same-underlying positions. It computes exact margin floors in closed form without sampling a price grid, then estimates how much additional spread width the position can absorb.

Rust · egui Python · zero dependencies Black-Scholes · Greeks 695 tests
The Problem
The calculation this project solves

For a bounded option position, the useful quantity here is the lowest possible payoff at expiration and how that floor changes when another vertical spread is added. Broker interfaces may show buying power, but they usually do not explain this structural payoff capacity directly.

A price-grid implementation samples the payoff at selected underlying prices. That is unnecessary for expiration payoff because the function is piecewise linear and its breakpoints are already known.

For a bounded expiration payoff, a finite minimum occurs at zero, at a strike, or at the boundary of the domain.

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

For calls on one underlying and expiration, the terminal payoff is V(P) = Σ qtyᵢ · max(0, P − Kᵢ). Puts use the corresponding max(0, Kᵢ − P) term. Each term changes slope at its strike, so the combined payoff can reach a finite minimum only at zero, at a strike, or at the boundary of the domain.

The maximum terminal loss, max(0, −min V), follows from an O(n log n) sort and a linear walk. This is an expiration-payoff calculation, not a broker portfolio-margin calculation. Broker requirements also use current option values, volatility scenarios, offsets, minimum charges, and house rules.

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 the maximum terminal loss is 10 points.
V = 0 +10 min V = −10 = 10-point maximum loss 100 110 130 price at expiration →
Checking the breakpoints gives the exact minimum for this terminal payoff.
Spread-capacity estimate

Within this terminal-payoff model, a positive payoff floor above spot can absorb some additional bounded vertical-spread width before the floor becomes negative. The engine reports that distance as a structural capacity measure. It does not claim that a broker will assign the same buying-power treatment.


Engineering Judgment
Handling incomplete and unsafe results

The implementation distinguishes finite results, unbounded outcomes, missing inputs, and invalid calculations so they cannot be rendered as the same kind of number.

Unbounded outcomes

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 are excluded

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.

Global and spot-relative floors

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 for implied volatility

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. With a valid bracket and a monotone pricing function, bisection converges predictably. Inputs outside no-arbitrage bounds return no result rather than a fabricated volatility.

Recorded corrections

The algorithm document records three corrections to the original reasoning. One involved a deep-in-the-money short vertical: measurement showed that the interest term was material, so the specification now reports the signed value 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)Terminal floor
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. A mismatch identifies which representation diverged.

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 result for a position set that was never supplied.


Current Shape
A specification you can execute

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 payoff-floor and capacity algorithms as an executable specification. It is written for review and eventual translation into the Rust implementation, not as a separate product.

This project remains closed-source because it is coupled to real brokerage exports. Every position, ladder, and figure on this page is invented for illustration; no account holdings are included.