Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created August 15, 2021 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/d11ccde031bb3a475ba8f43c43cc1935 to your computer and use it in GitHub Desktop.
Save romanbsd/d11ccde031bb3a475ba8f43c43cc1935 to your computer and use it in GitHub Desktop.
from scipy.stats import norm
import numpy as np
N = norm.cdf
def d1_d2(S: float, K: float, T: float, r: float, sigma: float):
d1 = (np.log(S / K) + (r + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
return d1, d2
def BS_CALL(S: float, K: float, T: float, r: float, sigma: float) -> float:
"""Black-Scholes model for a CALL option
Args:
S (float): Current price
K (float): Strike price
T (float): Time (% p.a.)
r (float): Risk-free rate (% p.a.)
sigma (float): Volatility
Returns:
float: price
"""
d1, d2 = d1_d2(S, K, T, r, sigma)
return S * N(d1) - K * np.exp(-r * T) * N(d2)
def BS_PUT(S, K, T, r, sigma) -> float:
"""Black-Scholes model for a PUT option
Args:
S (float): Current price
K (float): Strike price
T (float): Time (% p.a.)
r (float): Risk-free rate (% p.a.)
sigma (float): Volatility
Returns:
float: price
"""
d1, d2 = d1_d2(S, K, T, r, sigma)
return K * np.exp(-r * T) * N(-d2) - S * N(-d1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment