-
Notifications
You must be signed in to change notification settings - Fork 0
/
black_scholes.py
86 lines (76 loc) · 3.22 KB
/
black_scholes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Black-Scholes Option Pricing with Greeks
# Ramy Alsammarae
import numpy as np
from scipy.stats import norm
# Input values
r = 0.035 # Risk-free rate
S = 25 # Price of the underlying asset
K = 30 # Strike price
T = 240/365 # Time to expiration (years)
sigma = 0.25 # Volatility (stdev of log returns)
option_type = 'c' # 'c' for Call or 'p' for Put
def black_scholes(r, S, K, T, sigma, option_type):
"""Calculates the Black-Scholes price of an option"""
d1 = (np.log(S/K) + (r + sigma**2/2)*T)/(sigma*np.sqrt(T))
d2 = d1 - sigma*np.sqrt(T)
if option_type == "c":
price = S*norm.cdf(d1) - K*np.exp(-r*T)*norm.cdf(d2)
elif option_type == "p":
price = K*np.exp(-r*T)*norm.cdf(-d2) - S*norm.cdf(-d1)
else:
raise ValueError("Invalid option type. Please use 'c' for Call or 'p' for Put.")
return price
def option_delta(r, S, K, T, sigma, option_type):
"""Calculates the delta of an option"""
d1 = (np.log(S/K) + (r + sigma**2/2)*T)/(sigma*np.sqrt(T))
if option_type == "c":
delta_calc = norm.cdf(d1)
elif option_type == "p":
delta_calc = -norm.cdf(-d1)
else:
raise ValueError("Invalid option type. Please use 'c' for Call or 'p' for Put.")
return delta_calc
def option_gamma(r, S, K, T, sigma):
"""Calculates the gamma of an option"""
d1 = (np.log(S/K) + (r + sigma**2/2)*T)/(sigma*np.sqrt(T))
gamma_calc = norm.pdf(d1)/(S*sigma*np.sqrt(T))
return gamma_calc
def option_vega(r, S, K, T, sigma):
"""Calculates the vega of an option"""
d1 = (np.log(S/K) + (r + sigma**2/2)*T)/(sigma*np.sqrt(T))
vega_calc = S*norm.pdf(d1)*np.sqrt(T)
return vega_calc*0.01
def option_theta(r, S, K, T, sigma, option_type):
"""Calculates the theta of an option"""
d1 = (np.log(S/K) + (r + sigma**2/2)*T)/(sigma*np.sqrt(T))
d2 = d1 - sigma*np.sqrt(T)
if option_type == "c":
theta_calc = -S*norm.pdf(d1)*sigma/(2*np.sqrt(T)) - r*K*np.exp(-r*T)*norm.cdf(d2)
elif option_type == "p":
theta_calc = -S*norm.pdf(d1)*sigma/(2*np.sqrt(T)) + r*K*np.exp(-r*T)*norm.cdf(-d2)
else:
raise ValueError("Invalid option type. Please use 'c' for Call or 'p' for Put.")
return theta_calc/365
def option_rho(r, S, K, T, sigma, option_type):
"""Calculates the rho of an option"""
d1 = (np.log(S/K) + (r + sigma**2/2)*T)/(sigma*np.sqrt(T))
d2 = d1 - sigma*np.sqrt(T)
if option_type == "c":
rho_calc = K*T*np.exp(-r*T)*norm.cdf(d2)
elif option_type == "p":
rho_calc = -K*T*np.exp(-r*T)*norm.cdf(-d2)
else:
raise ValueError("Invalid option type. Please use 'c' for Call or 'p' for Put.")
return rho_calc*0.01
option_price = black_scholes(r, S, K, T, sigma, option_type)
delta = option_delta(r, S, K, T, sigma, option_type)
gamma = option_gamma(r, S, K, T, sigma)
vega = option_vega(r, S, K, T, sigma)
theta = option_theta(r, S, K, T, sigma, option_type)
rho = option_rho(r, S, K, T, sigma, option_type)
print("Option Price: ", round(option_price, 3))
print(" Delta: ", round(delta, 3))
print(" Gamma: ", round(gamma, 3))
print(" Vega : ", round(vega, 3))
print(" Theta: ", round(theta, 3))
print(" Rho : ", round(rho, 3))