Skip to content

zidankazi/rust-options

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-options

A blazing fast equity derivatives pricing engine written in Rust with a transformer-based vol surface predictor. Sub-microsecond Black-Scholes, 1,370x-optimized parallel Monte Carlo, analytical Greeks, binomial trees for American options, SVI vol smile calibration, and a PyTorch-trained transformer loaded from ONNX that predicts SVI parameters directly from market history. All pricing math from scratch with zero external math dependencies.

Benchmarks

Benchmark Python (NumPy/SciPy) Rust Speedup
Black-Scholes + all Greeks ~10-50μs ~19ns ~2,000x
Implied vol (Newton-Raphson) ~100-500μs ~50ns ~5,000x
Monte Carlo 100K paths ~5-15s ~600μs ~16,000x
Binomial tree 200 steps ~1-5ms ~11μs ~200x
Vol surface (500 BS calls) ~5-25ms ~10μs ~1,500x

All benchmarks measured with Criterion.rs. Python times are typical for equivalent implementations using NumPy, SciPy, and scipy.stats.norm. Run them yourself:

cargo bench -p pricer

Why Rust?

Most options pricing code lives in Python with NumPy and SciPy. That works for learning, but Python pays a cost on every function call, loop iteration, and memory allocation. Rust compiles down to native machine code with no garbage collector, so tight math loops run at hardware speed.

Here's the same Black-Scholes formula in both languages. Same math, same inputs:

# Python: ~10-50μs per call
from scipy.stats import norm
import numpy as np

def black_scholes(S, K, T, r, sigma):
    d1 = (np.log(S/K) + (r + 0.5*sigma**2)*T) / (sigma*np.sqrt(T))
    d2 = d1 - sigma*np.sqrt(T)
    return S*norm.cdf(d1) - K*np.exp(-r*T)*norm.cdf(d2)
// Rust: ~19ns per call (same formula, ~2000x faster)
// Normal CDF/PDF built from scratch using Abramowitz & Stegun approximation.
// No external math libraries, just f64 arithmetic.
pub fn black_scholes(contract: &OptionContract) -> Result<PricingResult, PricerError> {
    let d1 = ((contract.s / contract.k).ln()
        + (r - q + 0.5 * sigma * sigma) * t)
        / (sigma * t.sqrt());
    let d2 = d1 - sigma * t.sqrt();
    // ... price + all five Greeks in one pass
}

At 53 million BS prices per second, the math is never the bottleneck. The network is. That's what matters when you're pricing thousands of contracts in real time for trading, risk dashboards, or backtesting.

Architecture

rust-options/
├── crates/
│   ├── pricer/        ← BS, MC, binomial, IV solver, SVI calibration, Greeks (pure math, zero deps)
│   ├── market-data/   ← Yahoo Finance client, live option chains + quotes
│   ├── risk/          ← portfolio risk analytics (planned)
│   ├── strategy/      ← trade strategies & backtesting (planned)
│   ├── vol-model/     ← transformer-based vol surface predictor via ONNX Runtime
│   └── web/
│       ├── src/       ← Axum REST API serving market data + pricer
│       └── frontend/  ← TypeScript + Vite (market overview, chain viewer, pricer)

Features

pricer

  • Project scaffolding & types
  • Normal CDF/PDF (Abramowitz & Stegun)
  • xorshift64 PRNG + Marsaglia polar method
  • Black-Scholes pricing (European, Merton extension)
  • Analytical Greeks (delta, gamma, theta, vega, rho)
  • Implied volatility solver (Newton-Raphson + bisection)
  • Monte Carlo engine (GBM, antithetic variates)
  • Binomial tree (CRR, American options)
  • SVI vol smile calibration (Nelder-Mead optimizer)
  • Benchmarks
  • Full test suite

market-data

  • Yahoo Finance integration (option chains, quotes, sparklines)
  • Automatic IV + Greeks computation on live market prices
  • Parallel quote fetching

vol-model

  • Historical option chain data pipeline (Massive.com / Polygon.io)
  • SVI calibration on historical data for training labels
  • Transformer encoder for SVI parameter prediction (PyTorch)
  • ONNX export with roundtrip verification
  • Rust inference via ort crate — end-to-end ONNX → Rust → pricer::svi
  • Web API integration

risk

  • Not started

strategy

  • Not started

web

  • Axum REST API
  • TypeScript frontend (Vite)
  • Market overview landing page with sparkline charts
  • Live option chain viewer with Greeks
  • Black-Scholes pricing calculator with payoff diagram
  • Strategy builder
  • Portfolio tracking
  • Backtesting

Getting Started

# Build everything
cargo build

# Run tests
cargo test -p pricer

# Run benchmarks
cargo bench -p pricer

Design Principles

  • Speed is a feature. Sub-microsecond pricing, zero allocations in hot paths.
  • No unnecessary dependencies. Normal CDF, PRNG, and RNG implemented from scratch for WASM portability.
  • Pure math in the pricer. No IO, no async, no side effects. Inputs in, prices out.
  • Correct first, then fast. Validated against known analytical results before optimizing.

About

A blazing fast equity derivatives pricing engine written in Rust. Sub-microsecond Black-Scholes, parallel Monte Carlo, SVI calibration, and a transformer vol surface predictor via ONNX.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors