5d ago
Five layers of defense between an algo and a margin call
Notes from hardening the OU-MRS paper-trading bot after a 20.6x leverage anomaly. Notional caps, dual-cap max lots, kill switches.
OU-MRS runs an Ornstein-Uhlenbeck mean-reversion strategy on Indian index options. After a phase eight bug let a single position approach 20x notional leverage on a small paper account, the rule became: never let one number be the only thing standing between the strategy and ruin.
The risk stack now has five independent layers. A pre-trade leverage check rejects orders that exceed a hard notional cap. A position cap rejects orders that would put more than a configured percentage of capital in one symbol. A dual-cap max-lots layer takes the minimum of two independent lot-size calculations. A real-time drawdown monitor flattens everything when daily loss crosses a threshold. A heartbeat-driven kill switch trips if the strategy stops emitting health beacons.
Each layer is dumb on purpose. Smart code is the kind that fails interestingly. Risk code should fail boringly: a noisy log, a flat book, and a person paged. The strategy itself can be as adventurous as it wants because the rails are paranoid.