Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Martingale

A high-performance C++ framework for derivative valuation and quantitative risk analytics.

Disclaimer: This was not vibe coded. Every line and comment was written with intent and validated against closed-form analytical solutions, because a Monte Carlo engine that hasn't been sanity-checked against Black-Scholes is just an expensive random number generator.


What is Martingale?

Martingale is a C++ derivatives pricing engine built from first principles. It simulates the evolution of asset prices under the risk-neutral measure $\mathbb{Q}$ using Geometric Brownian Motion (GBM) and discounts the expected payoff to obtain the present value of an option.

Under $\mathbb{Q}$, the asset price $S_t$ follows the stochastic differential equation:

$$dS_t = r,S_t,dt + \sigma,S_t,dW_t$$

where $r$ is the risk-free rate, $\sigma$ is the volatility, and $W_t$ is a standard Brownian motion.

The name comes from the mathematical concept of a martingale process, which is the foundation of risk-neutral pricing in quantitative finance. When the asset price is discounted by the risk-free money-market account, the resulting process is a martingale under $\mathbb{Q}$:

$$\mathbb{E}^{\mathbb{Q}}!\left[,e^{-r,T},S_T \mid \mathcal{F}_t,\right] = e^{-r,t},S_t$$

This is precisely what guarantees an arbitrage-free price.

Integrating the GBM SDE exactly gives the terminal stock price under the risk-neutral measure:

$$S_T = S_0 \cdot \exp!\left(\left(r - \tfrac{\sigma^2}{2}\right)T + \sigma\sqrt{T},Z\right), \qquad Z \sim \mathcal{N}(0,1)$$

The present value of any European-style instrument is then:

$$V \approx e^{-rT} \cdot \frac{1}{N} \sum_{i=1}^{N} \phi!\left(S_T^{(i)}\right)$$

where $\phi$ is the payoff function of the contract. For a call: $\phi(S_T) = \max(S_T - K,, 0)$; for a put: $\phi(S_T) = \max(K - S_T,, 0)$.

For path-dependent instruments the payoff $\phi$ requires the entire simulated trajectory ${S_{t_1}, \ldots, S_{t_M}}$ rather than $S_T$ alone. Each path is generated by applying M independent GBM increments:

$$S_{t_{j+1}} = S_{t_j} \cdot \exp!\left(\left(r - \tfrac{\sigma^2}{2}\right)\Delta t + \sigma\sqrt{\Delta t},Z_j\right), \qquad \Delta t = \frac{T}{M}$$

Asian options replace $S_T$ in the payoff with a price average:

$$\phi_{\text{Asian call}} = \max!\left(\frac{1}{M}\sum_{i=1}^{M} S_{t_i} - K,;0\right)$$

The geometric average variant $G = \left(\prod S_{t_i}\right)^{1/M}$ is lognormally distributed and admits an exact closed-form price with adjusted parameters:

$$\sigma_A^2 = \sigma^2 \cdot \frac{(M+1)(2M+1)}{6M^2}, \qquad \mu_A = \left(r - \tfrac{\sigma^2}{2}\right)\frac{M+1}{2M}$$

Barrier options add a knock-out condition: a down-and-out call pays the vanilla call payoff only if the asset never touches the barrier $B$ at any monitoring date:

$$\phi_{\text{DOC}} = \max(S_T - K,;0)\cdot \mathbf{1}!\left[\min_{i} S_{t_i} > B\right]$$

The continuous-monitoring closed-form (Rubinstein & Reiner, 1991) for $B < K$ is:

$$\text{DOC} = C_{\text{BS}}(S_0, K) ;-; \left(\frac{B}{S_0}\right)^{2\lambda} C_{\text{BS}}!\left(\frac{B^2}{S_0}, K\right) \quad \text{where } \lambda = \frac{r + \sigma^2/2}{\sigma^2}$$


Build & Run

Prerequisites

  • clang++ or g++ with C++20 support

CLI

c++ -std=c++20 -O2 -Iinclude \
    src/Option.cpp src/Payoff.cpp src/PathPayoff.cpp src/PathOption.cpp \
    src/MonteCarloPricer.cpp src/main.cpp \
    -o option_pricer && ./option_pricer

Validation Test

c++ -std=c++20 -O2 -Iinclude \
    src/Option.cpp src/Payoff.cpp src/PathPayoff.cpp src/PathOption.cpp \
    src/MonteCarloPricer.cpp tests/test_pricer.cpp \
    -o test_pricer && ./test_pricer

License

MIT License © 2026 Gunj Joshi

You are free to use, modify, and distribute this software, provided that the original copyright notice and this permission notice are included in all copies or substantial portions of the software.

About

High-performance C++ framework for derivative valuation and quantitative risk analytics.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages