Created
August 15, 2021 21:25
-
-
Save romanbsd/d11ccde031bb3a475ba8f43c43cc1935 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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