Skip to content
All articles
Derivatives

The Black-Scholes formula in a few lines of Python

· 1 min read

Black-Scholes prices a European option on a stock that pays no dividends, assuming constant volatility and a constant interest rate. The inputs are the spot S, strike K, time to expiry T in years, the continuously compounded rate r and the volatility sigma.

python
from math import log, sqrt, exp
from scipy.stats import norm

def bs_call(S, K, T, r, sigma):
    d1 = (log(S / K) + (r + 0.5 * sigma**2) * T) / (sigma * sqrt(T))
    d2 = d1 - sigma * sqrt(T)
    return S * norm.cdf(d1) - K * exp(-r * T) * norm.cdf(d2)

def bs_put(S, K, T, r, sigma):
    # put-call parity: C - P = S - K * exp(-rT)
    return bs_call(S, K, T, r, sigma) - S + K * exp(-r * T)
01

Reading the formula

The first term, S times N(d1), is the value of receiving the stock if the option finishes in the money. The second, K times exp(-rT) times N(d2), is the discounted strike you expect to pay. N(d2) is the risk-neutral probability of finishing in the money.

02

A quick sanity check

Put-call parity must hold exactly: C - P = S - K·exp(-rT). If your call and put prices do not satisfy it, there is a bug. Also check the limits: as sigma goes to zero the call tends to max(S - K·exp(-rT), 0), and a deep in-the-money call has a delta close to 1.

From here the Greeks are small derivatives of the same expression. Delta of a call is N(d1), and vega is S times the normal density at d1 times sqrt(T).

Black-Scholes calculator

Educational content only, not investment advice.

More articles