Algorithmic Trading & Indicators Advanced

Calculate Option Greeks (Delta, Gamma, Theta, Vega) via Black-Scholes in Python

Vectorized Python script using SciPy to compute European Option Greeks (Delta, Gamma, Theta, Vega, Rho) for Call and Put options without external wrapper libraries.

#numpy#scipy

Problem Statement

Third-party option pricing packages often break on newer Python versions or run slowly on full option chains. This pure NumPy/SciPy implementation computes Call/Put prices and all 5 Greeks instantaneously.

Formula / Core Logic

d1 = [ln(S/K) + (r + 0.5 * sigma^2) * T] / (sigma * sqrt(T)) d2 = d1 - sigma * sqrt(T) Call Delta = N(d1), Gamma = N'(d1) / (S * sigma * sqrt(T))

Installation

pip install numpy scipy
calculate-option-greeks-black-scholes-python.py
import numpy as np
from scipy.stats import norm

def black_scholes_greeks(S, K, T, r, sigma, option_type='call'):
    d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
    d2 = d1 - sigma * np.sqrt(T)
    
    if option_type == 'call':
        price = S * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2)
        delta = norm.cdf(d1)
        theta = (- (S * norm.pdf(d1) * sigma) / (2 * np.sqrt(T)) - r * K * np.exp(-r * T) * norm.cdf(d2)) / 365.0
    else:
        price = K * np.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)
        delta = norm.cdf(d1) - 1
        theta = (- (S * norm.pdf(d1) * sigma) / (2 * np.sqrt(T)) + r * K * np.exp(-r * T) * norm.cdf(-d2)) / 365.0
        
    gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T))
    vega = (S * norm.pdf(d1) * np.sqrt(T)) / 100.0
    return {'Price': round(price, 2), 'Delta': round(delta, 4), 'Gamma': round(gamma, 6), 'Theta_Daily': round(theta, 4), 'Vega': round(vega, 4)}

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: How is Time to Expiry (T) passed into this function?

Pass T in annualized years—for example, 7 days to expiry is T = 7 / 365.0.