Python Code

Open-source Python models for quantitative finance, each with the theory, the code and the results.

Newsletter

Get exclusive models, prompts and code

One short email when there's something new. finance models, code walkthroughs, agentic prompts. No spam.

Built with
Anthropic Claude Python R ChatGPT GitHub Jupyter Excel PowerPoint VS Code SQL MATLAB

Read the theory, explore the code, and copy what you need.

5 Free Tools to Test Your Trading Strategies

Alpaca and Interactive Brokers paper APIs, TradingAgents, Glassbench, a TradingView MCP server and QuantConnect Lean: what each one is free for, the interface, and the code that connects to it.

Paper Trading Alpaca IBKR

Delta Hedging Error, a Monte Carlo on Gamma

Sell an at the money call, hedge it daily with Black-Scholes delta across 150,000 simulated paths, and watch the hedging error explode at the strike as expiry closes in.

Monte Carlo Hedging Gamma

Gamma Exposure (GEX) Step by Step

Net dealer gamma from the full SPX chain, the gamma flip, the call and put walls, and how a trading bot on the Interactive Brokers API uses them.

Options Gamma Dealers

Machine Learning for Equity and Fixed Income

Trees, random forests and XGBoost on a survivorship free S&P 500 panel, then PCA on the Treasury curve and Lasso on a macro panel, with honest results.

Machine Learning XGBoost PCA

Deep Learning for Finance, Five Architectures

A feed forward net fits the SPY vol surface, an LSTM and a transformer forecast volatility, a CNN reads charts, and an autoencoder compresses 45 large caps.

Deep Learning LSTM Transformers

Time Series Momentum, a 990 Setting Sweep

Moskowitz, Ooi and Pedersen reproduced on 45 free ETFs from 2005 to 2026, lookback against holding period, scored in sample and out of sample.

Momentum Backtest ETFs

Yield Curves, US and Brazil

The US curve from FRED and the Brazilian curve from Tesouro Direto with a Nelson Siegel Svensson fit, plus how to read inversion, steepening and flattening.

Rates Yield Curve Brazil

Six Free Volatility and Option Models

arch, QuantLib, FinancePy, tf-quant-finance, gs-quant and the SVI paper: what each one is for, the data it needs, and a runnable script for every one.

Volatility QuantLib SVI

Black-Scholes Option Pricing Model

Complete implementation of the Black-Scholes framework covering European option pricing, arbitrage bounds, implied volatility via Brent's method, and put-call parity.

Options Pricing Implied Volatility

Option Greeks in Python

Step-by-step Delta, Gamma, Theta and Vega in Python. Includes formulas, plots, and an interactive chart that shows how gamma sharpens as time to expiry approaches zero.

Greeks Delta Gamma

Heston Vol Surface in Python

Stochastic volatility step by step: each Heston parameter as a 1D slice (rho, xi, kappa, v_0), then stacked into the full 3D implied vol surface with a low-vs-high regime comparison.

Heston Stochastic Vol Skew

Monte Carlo Option Pricing with GBM

Monte Carlo simulation under Geometric Brownian Motion with Itô correction. Terminal distributions, path simulation, and comparison against Black-Scholes analytical prices.

Monte Carlo Simulation GBM

CRR Binomial Tree for American Options

Cox-Ross-Rubinstein binomial tree for American and European option pricing, early exercise detection, and convergence to Black-Scholes.

Binomial Tree American Options Early Exercise

MVO Portfolio Optimization

Mean-variance optimization using the Markowitz framework, covering efficient frontier, Sharpe ratio maximization, constrained allocation, and portfolio backtesting.

Portfolio Markowitz Backtesting

Advanced Option Greeks

The second and third order Greeks a vol desk actually watches: vanna, volga, charm, speed, zomma, colour and ultima, each derived and plotted against the variable that moves it.

Greeks Volatility Second Order

Trading Volatility Convexity

A long strangle against a short straddle, sized so vega is exactly zero. What is left is volga, and the page finds where that convexity peaks in every vol regime.

Volga Convexity Vega Neutral

Gamma Surface

Gamma sliced one dimension at a time against spot, vol, time and strike, then stacked into the full 3D Black-Scholes gamma surface as expiry approaches.

Gamma Surface 3D

Neural Network Vol Surface

Two numpy MLPs learn the implied volatility surface from real option chains, trained and scored on a strict out-of-time split.

Neural Network Volatility Machine Learning

SPY Vol Surface with Deep Learning

Deep learning applied to the SPY implied volatility surface, with the data pipeline, the architecture choices, and an honest out-of-sample read.

Deep Learning SPY Volatility

Deep Hedging

A recurrent network learns a hedging policy directly from simulated paths instead of reading delta off a closed form, and is tested against the Black-Scholes hedge.

Deep Hedging RNN Derivatives

Hierarchical Risk Parity

Lopez de Prado's HRP and HCAA built from scratch, clustering the correlation matrix before allocating, and benchmarked against Markowitz.

HRP Allocation Clustering

Risk Based Allocation

Five risk based allocation methods backtested side by side with GARCH volatility forecasts, including the turnover and cost drag each one carries.

Allocation GARCH Backtest

Free Options Data

Where to pull real option chains without paying for a data vendor, how to clean the quotes, and how to re-imply the vols so the surface is usable.

Data Options Pipeline

AI Models

Specialised AI agents for quantitative finance, from equity valuation to fund analytics.