Skip to content

ryanneefeng/options-pricing-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Black-Scholes Options Pricing Engine

License: MIT C++ Status

A high-performance C++ implementation of the Black-Scholes-Merton model for pricing European options with complete Greeks calculations made by a student who realized they wanted to get into quant and CS way too late.

Features

  • European Options Pricing
  • Complete Greeks Suite (Delta, Gamma, Theta, Vega, Rho)
  • Input Validation
  • Clean Architecture
  • Put-call party verification
  • Multi-option batch processing
  • Automated Greek analysis and interpretation
  • Professional user interface

Documentation:

  • Comprehensive README with mathematical background
  • Inline code documentation
  • technical writeup (in progress - 60% complete)

Planned Enhancements:

  • Implied volatility calculator (Newton-Raphson method)
  • American options pricing (binomial tree)
  • Dividend-paying stocks support
  • Historical volatility calculator
  • Interactive mode with saved sessions

Mathematical Background

The Black-Scholes model provides analytical solutions for European option pricing under the following assumptions:

  • Log-normal distribution of asset prices
  • Constant volatility and risk-free rate
  • No dividends during option lifetime
  • No arbitrage opportunities
  • Continuous trading

Formula

Call Option:

C = S₀N(d₁) - Ke^(-rT)N(d₂)

Put Option:

P = Ke^(-rT)N(-d₂) - S₀N(-d₁)

Where:

d₁ = [ln(S₀/K) + (r + σ²/2)T] / (σ√T)
d₂ = d₁ - σ√T
N(x) = Standard normal cumulative distribution function

Parameters:

  • S₀ = Current stock price
  • K = Strike price
  • T = Time to expiration (years)
  • r = Risk-free interest rate (annualized)
  • σ = Volatility (annualized standard deviation)

Greeks

"Greeks" measure the sensitivity of option prices to various parameters. Basically they tell us and other people whether an option is worth buying, selling, or keeping:

Greek Measures Call Formula Put Formula
Delta (Δ) Rate of change w.r.t. underlying price N(d₁) N(d₁) - 1
Gamma (Γ) Rate of change of Delta N'(d₁)/(S₀σ√T) N'(d₁)/(S₀σ√T)
Theta (Θ) Rate of change w.r.t. time Complex (see code) Complex (see code)
Vega (ν) Rate of change w.r.t. volatility S₀√T N'(d₁) S₀√T N'(d₁)
Rho (ρ) Rate of change w.r.t. interest rate KTe^(-rT)N(d₂) -KTe^(-rT)N(-d₂)

Note: N'(x) = (1/√2π)e^(-x²/2) is the standard normal probability density function

Installation

# Clone the repository
git clone https://github.com/ryaneefeng/options-pricing-engine.git
cd options-pricing-engine

# Compile the program
g++ -std=c++17 src/main.cpp src/Option.cpp -o bin/pricer -I include

# Run the program
./pricer

Example Session:

======================================================
    Black-Scholes Options Pricing Engine v1.0
======================================================

How many options would you like to run?
2
Enter Stock Price (S): $100
Enter Strike Price (K): $105
Enter Time to Maturity (T) in years: 1.0
Enter Risk-free Rate (r) as decimal (e.g., 0.05 for 5%): 0.05
Enter Volatility (sigma) as decimal (e.g., 0.20 for 20%): 0.20

Calculating...

======================================================
                 CALL OPTION
======================================================
Price:  $8.0214
Delta:   0.5422
Gamma:   0.0198
Theta:   -6.2771
Vega:    39.6705
Rho:     46.2015

======================================================
                 PUT OPTION
======================================================
Price:  $7.9004
Delta:   -0.4578
Gamma:   0.0198
Theta:   -1.2832
Vega:    39.6705
Rho:     -53.6776

======================================================
                     VALIDATION
======================================================
Put-Call Parity Error: 7.11e-15
Calculations verified!
======================================================
                    CALL ANALYSIS
======================================================
Delta: Good value. Reacts strongly to price changes and good for directional trades.
Gamma: Healthy value as the Delta will move noticeably. Good for trading and Gamma scalping.
Theta: Heavy time decay: Good for shorting, risky for long-term.
Vega: Medium sensitivity. Good if you expect rising uncertainty.
Rho: Big rate sensitivity. Long-term options/high strike.
======================================================
                    PUT ANALYSIS
======================================================
Delta: Moderately bearish. Balanced downside protection.
Gamma: Healthy value as the Delta will move noticeably. Good for trading and Gamma scalping.
Theta: Moderate time decay. Should only hold if you expect a move soon.
Vega: Medium sensitivity. Good if you expect rising uncertainty.
Rho: Big rate sensitivity. Long-term options/high strike.

Enter Stock Price (S): $50
Enter Strike Price (K): $60
Enter Time to Maturity (T) in years: 4
Enter Risk-free Rate (r) as decimal (e.g., 0.05 for 5%): 0.03
Enter Volatility (sigma) as decimal (e.g., 0.20 for 20%): 0.10

Calculating...

======================================================
                 CALL OPTION
======================================================
Price:  $2.7005
Delta:   0.4162
Gamma:   0.0390
Theta:   -1.0309
Vega:    39.0110
Rho:     72.4394

======================================================
                 PUT OPTION
======================================================
Price:  $5.9157
Delta:   -0.5838
Gamma:   0.0390
Theta:   0.5655
Vega:    39.0110
Rho:     -140.4215

======================================================
                     VALIDATION
======================================================
Put-Call Parity Error: -3.55e-15
Calculations verified!
======================================================
                    CALL ANALYSIS
======================================================
Delta: This option will move a bit with the stock, but still not something you would hedge with (more speculative than strategic).
Gamma: High Gamma. Dangerous if hedging, but great for long options and are aiming for volatility pops.
Theta: Mild time decay, good for holding long-term.
Vega: Medium sensitivity. Good if you expect rising uncertainty.
Rho: Big rate sensitivity. Long-term options/high strike.
======================================================
                    PUT ANALYSIS
======================================================
Delta: Moderately bearish. Balanced downside protection.
Gamma: High Gamma. Dangerous if hedging, but great for long options and are aiming for volatility pops.
Theta: Very slow decay. Cheap to hold.
Vega: Medium sensitivity. Good if you expect rising uncertainty.
Rho: Big rate sensitivity. Long-term options/high strike.

Programmatic Usage

#include "include/Option.h"

int main() {
    // Create an option object
    Option opt(100.0,  // Stock price
               105.0,  // Strike price
               1.0,    // Time to maturity (years)
               0.05,   // Risk-free rate
               0.20);  // Volatility
    
    // Calculate prices
    double call_price = opt.calculate_call_price();
    double put_price = opt.calculate_put_price();
    
    // Calculate Greeks
    double delta = opt.calculate_delta_call();
    double gamma = opt.calculate_gamma();
    double vega = opt.calculate_vega();
    
    return 0;
}

Project Structure

options-pricing-engine/
├── src/
│   ├── main.cpp          # User interface and program entry point
│   └── Option.cpp        # Option class implementation
├── include/
│   └── Option.h          # Option class header and declarations
├── docs/
│   ├── technical_writeup.md    # Detailed mathematical documentation
│   └── examples.md             # Usage examples and test cases
├── bin/                  # Compiled executables (generated)
├── .gitignore
├── LICENSE
├── Makefile
└── README.md

Educational Value

This project demonstrates:

Financial Mathematics:

  • Options pricing theory
  • Risk-neutral valuation
  • Stochastic calculus concepts
  • Probability distributions

Software Engineering:

  • Object-oriented design in C++
  • Header/implementation separation
  • Clean code principles
  • Documentation best practices

Numerical Computing:

  • Mathematical function approximation
  • Floating-point precision handling
  • Performance optimization

Technical Details

Normal CDF Approximation

The standard normal cumulative distribution function N(x) is approximated using the error function:

double normal_cdf(double x) {
    return 0.5 * erfc(-x * M_SQRT1_2);
}

This provides accuracy to 6+ decimal places, sufficient for financial calculations.

Performance Characteristics

  • Time Complexity: O(1) for all calculations
  • Space Complexity: O(1) (only stores 5 parameters)
  • Typical Runtime: < 1ms per calculation on modern hardware

Precision & Accuracy

  • All calculations use double precision (IEEE 754 64-bit)
  • Greeks accurate to 4+ decimal places
  • Prices accurate to $0.01 or better

Future Enhancements

Planned features for future versions:

  • American Options: Early exercise using binomial trees or Monte Carlo
  • Implied Volatility: Newton-Raphson solver for implied vol
  • Volatility Smile: Support for non-constant volatility
  • Dividend Yield: Extend model for dividend-paying stocks
  • Exotic Options: Barrier options, Asian options
  • Portfolio Analysis: Multi-option portfolio Greeks
  • GUI Interface: Qt-based graphical interface
  • Python Bindings: PyBind11 wrapper for Python integration
  • Unit Tests: Comprehensive test suite with Google Test
  • Benchmarking: Performance comparison suite

Resources & References

Academic Papers:

  • Black, F., & Scholes, M. (1973). "The Pricing of Options and Corporate Liabilities." Journal of Political Economy, 81(3), 637-654.
  • Merton, R. C. (1973). "Theory of Rational Option Pricing." Bell Journal of Economics and Management Science, 4(1), 141-183.

Online Resources:

Author

Ryan Feng
Fordham University | Class of 2028
Mathematics & Computer Science

Aspiring Quantitative Researcher with interests in derivatives pricing, algorithmic trading, and financial engineering.

Connect:

About

C++ implementation of Black-Scholes options pricing model with Monte Carlo simulation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages