← dufanyin.dev

Quant options system

Architecture

One repository, Optra: an options trading engine that runs the same strategies in a backtest and live on Interactive Brokers, one server with one API in front of it for the page and for agents, and one numerics library under both. It is cut in two at gRPC: above, the control plane asks; below, the core owns the trading state and reaches the world through adapters.

Adds, starts and stops strategies, watches the account and the logs, runs backtests, prices and stresses options. Operator a browser, signed in systemd: the engine restarted before each day's open, a check every five minutes while the market is open, the day's report after the close; a Telegram message when something changes. Scheduler check, daily report An AI agent prices, calibrates, stresses, screens and backtests with the same tools the page uses: plain HTTP on the server (GET /api/tools lists them). No tool places an order. Agents tools over HTTP, never orders ●  CONTROL PLANE One Preact app, served by the server: Trading (Home, the account, orders, data) and Research (backtests, the screener, pricing, risk, numerics, validation). The signed-in owner reaches it at dufanyin.dev/optra/. Page Trading and Research One FastAPI app with one API: the trading routes (through gRPC only), backtests (entry_backtest, a process per run), the 36 research tools (through the C ABI), the run history, the day's check and report. It holds no trading state, so it can restart at any time. Server one API: trading, backtests, research tools, run history, the day THE CUT control plane above, core below The control plane's only way into the live engine: each RPC calls one of the engine's operations and translates its result into the API's messages. gRPC (live) entry_backtest: a backtest from the command line, one day or many, its result as JSON. CLI (backtest) libanalytics_c.so, loaded with ctypes: a JSON request in, a JSON response out, per workflow. The engine’s Black (prices and implied vols in a batch) is one of them: the server’s chains of the engine’s days and the holdings’ stress use it. C ABI (ctypes) ●  CORE The engines inside it around five ports: the event loop, the clock, the venue, the storage and the market data. Backtest and live are two assemblies of it; what each event does is written once. Engine one code path, two assemblies; adapters plug into its ports The one writer of the trading state. Live, one thread: fills and commands in one ordered queue, never dropped; the newest snapshot of each portfolio; its own clock for the timer. Logs and socket writes run off it. Event loop one writer; fills never dropped quotes timer timer Option chains keyed by strike and expiry, with their quotes; each snapshot's implied vol and Greeks, Black on each expiry's put-call-parity forward (from quant). Portfolios chains, IV, Greeks One code for backtest and live: on_timer once a bar, on_order and on_trade; declared parameters; they see the engine through StrategyContext. Strategies one code, both modes Each strategy's delta back into its range, in whole shares of the underlying or of what trades in its place (SPY for SPX). Hedging delta per strategy read orders orders Every order passes it first, the same in a backtest: size, position, premium, price band, the spread for a market order, no naked shorts, closing 0DTE options settled in shares before the close; the operator's halt lets through only what reduces a holding. Refusals are counted and kept. Risk limits, the halt An allowed order goes to the venue and is tracked as its strategy’s until it is done. Each update is merged into the order: a repeat or an older one changes nothing, an end is final, and an end waits until the order’s fills are booked (10 s at most). A fill IB repeats is dropped, so each is booked once, whatever order they arrive in. Execution send, merge, dedupe fills marks fills holding, fills Holdings, P&L and Greeks per strategy, a combo held as its legs (its quantity from theirs), the P&L split by source (delta, gamma, theta, vega, other, fills against the mid), checked against the broker's positions before a strategy may start; options held past their expiry settled at the close, in cash or in shares. Positions per strategy, vs broker A timer tick: settle expired options → strategies → position metrics → hedges. market data venue storage ●  ADAPTERS Live: IB quotes into a quote book, a snapshot of each changed portfolio, each minute recorded for a backtest of the day and the screener’s chains. Backtest: a day from Parquet, its snapshots computed before the run. Market data IB live, Parquet backtest Live: Interactive Brokers through the TWS API, one named paper account. Backtest: orders fill at the next bar’s quotes. Either way a fill is one contract’s: a combo fills as its legs. Broker gateway IB live, simulated backtest Contracts, strategies, orders and fills, written by a thread of its own; a restart replays the orders and fills into the holdings. In memory for a backtest. SQLite orders and fills, replayed quotes orders, fills fills saved ●  QUANT The numerics, one library for the trading engine and the research tools: Black and Black-Scholes, Greeks, implied vol by LetsBeRational, Monte Carlo, binomial and trinomial trees, finite differences, Longstaff-Schwartz, Heston, the option screener. Parallel with OpenMP, the same result at any thread count. quant pricing, IV, Greeks, MC, PDE, Heston The research workflows on quant (pricing every way at once, scenarios, hedging, validation, calibration, the screener) as JSON requests and responses, behind the C ABI. quant/analytics research workflows, C ABI IV, Greeks

Scroll sideways for the whole diagram. Click a box (or tab to it and press Enter) to see what it does and what it touches.

How it holds together