Skip to content

setchepa/optpricer

Repository files navigation

optpricer

Open-source options pricing & analytics — pricing, Greeks, implied volatility, surface fitting, multi-leg strategy P&L, and scenario analysis in one Python library with an optional FastAPI server.

License: MIT Python

Models

Model Use case
Black-Scholes-Merton Equities / ETFs with continuous dividend yield
Black 76 Futures and bond options (forward-price variant)
Bachelier (Normal) SOFR, interest-rate options, negative-rate regimes
Cox-Ross-Rubinstein (CRR) American options via binomial tree

Features

  • Closed-form pricing for calls and puts across all analytical models
  • Full first-order Greeks (delta, gamma, theta, vega, rho)
  • Second-order Greeks (vanna, volga, charm) for BSM and Black 76
  • Two-stage implied-volatility solver (Newton-Raphson → Brent fallback)
  • Vectorized batch IV solver for homogeneous books
  • No-arbitrage bounds checking and put-call parity validation
  • Volatility surface fitting, multi-leg strategy P&L, scenario shocks
  • FastAPI HTTP server with OpenAPI docs

Install

git clone https://github.com/<your-username>/optpricer.git
cd optpricer
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

Or, once published:

pip install optpricer

Use as a library

from engine.dispatcher import price, greeks, implied_vol

# Price a BSM call
result = price({
    "model": "bs",
    "S": 100, "K": 100, "T": 1.0,
    "r": 0.05, "sigma": 0.20,
    "option_type": "call",
})

# Full Greeks, including second-order
g = greeks({
    "model": "bs",
    "S": 100, "K": 100, "T": 1.0,
    "r": 0.05, "sigma": 0.20,
    "option_type": "call",
    "second_order": True,
})

# Solve implied volatility from a market price
iv = implied_vol({
    "model": "bs",
    "S_or_F": 100, "K": 100, "T": 1.0,
    "r": 0.05, "market_price": 10.45,
    "option_type": "call",
})

Run the HTTP API

uvicorn api.main:app --reload
# → http://127.0.0.1:8000/docs  (interactive OpenAPI UI)

Example request:

curl -s http://127.0.0.1:8000/price \
  -H 'Content-Type: application/json' \
  -d '{"model":"bs","S":100,"K":100,"T":1.0,"r":0.05,"sigma":0.2,"option_type":"call"}'

Or with Docker:

docker build -t optpricer .
docker run -p 8000:8000 optpricer

Endpoints

Method Path Purpose
POST /price Single-option price
POST /greeks Full Greeks for a single option
POST /iv Implied volatility from a market price
POST /greeks/batch Batch Greeks for a book of options
POST /iv/batch Batch implied volatility (vectorized for BSM / Black 76)
POST /surface Fit a volatility surface from quotes
POST /strategy Multi-leg strategy analytics and P&L
POST /scenario Scenario analysis under spot / vol shocks
GET /health Liveness check

Full request/response schemas at /docs (Swagger UI) or /redoc after starting the server.

Docs

Testing

pytest tests/ -v --tb=short
pytest tests/ --cov=engine --cov=api --cov-report=term-missing

The suite includes Hull-textbook benchmark cases, put-call parity checks, finite-difference Greek validation, and IV-solver convergence tests across 185+ securities and IV regimes from 6% to 200%+.

Contributing

See CONTRIBUTING.md. Issues and pull requests are welcome.

License

MIT

About

Open-source options pricing & analytics — BSM, Black 76, Bachelier, CRR, with a FastAPI server.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages