-
Notifications
You must be signed in to change notification settings - Fork 0
/
Greeks calcumation
90 lines (70 loc) · 3.5 KB
/
Greeks calcumation
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
87
88
89
#This code was created to be combined with the BS options pricing model
import numpy as np
from scipy import stats
from py_vollib.black_scholes import black_scholes as bs
from py_vollib.black_scholes.greeks.analytical import delta, gamma, vega, theta, rho ;
#Delta measures the rate of change of the theoratical option value with respect to the underlying price
def delta_calc(r,S,K,T,sigma, type="c"):
"calculate delta of a european option"
d1= (np.log(S/K) + (r + sigma**2/2)*T) / (sigma*np.sqrt(T))
try:
if type=="c":
delta_calc = stats.norm.cdf(d1, 0, 1)
elif type =="p":
delta_calc = -stats.norm.cdf(-d1, 0, 1)
return delta_calc, delta(type, S, K, T, r, sigma)
except:
print('Please confirm all option parameters above !')
#Gamma measures the rate of change in the Delta with respect to changes in the underlying price
def gamma_calc(r,S,K,T,sigma, type="c"):
"calculate gamma of a european option"
d1= (np.log(S/K) + (r + sigma**2/2)*T) / (sigma*np.sqrt(T))
d2= d1 - sigma*np.sqrt(T)
try:
gamma_calc = stats.norm.pdf(d1, 0, 1) / (S*sigma*np.sqrt(T))
return gamma_calc, gamma(type, S, K, T, r, sigma)
except:
print('Please confirm all option parameters above !')
#Vega measures sensitivity to volatility. Vega is the derivative of the option value with respect to the volatility of the underlying asset
def vega_calc(r,S,K,T,sigma, type="c"):
"calculate vega of a european option"
d1= (np.log(S/K) + (r + sigma**2/2)*T) / (sigma*np.sqrt(T))
try:
vega_calc = S * stats.norm.pdf(d1, 0 ,1) * np.sqrt(T)
return vega_calc*0.01, vega(type, S, K, T, r, sigma)
except:
print('Please confirm all option parameters above !')
#Theta measures the sensitivity of the value of the derivative to the passage of time - time decay
def theta_calc(r,S,K,T,sigma, type="c"):
"calculate theta of a european option"
d1= (np.log(S/K) + (r + sigma**2/2)*T) / (sigma*np.sqrt(T))
d2= d1 - sigma*np.sqrt(T)
try:
if type=="c":
theta_calc= -S* stats.norm.pdf(d1, 0, 1)* sigma/(2*np.sqrt(T)) - r*K*np.exp(-r*T)*stats.norm.pdf(d2, 0, 1)
elif type =="p":
theta_calc= -S* stats.norm.pdf(d1, 0, 1)* sigma/(2*np.sqrt(T)) + r*K*np.exp(-r*T)*stats.norm.pdf(-d2, 0, 1)
return theta_calc/365, theta(type, S, K, T, r, sigma)
except:
print('Please confirm all option parameters above !')
#Rho measure the sensitivity to the interest rate
def rho_calc(r,S,K,T,sigma, type="c"):
"calculate rho of a european option"
d1= (np.log(S/K) + (r + sigma**2/2)*T) / (sigma*np.sqrt(T))
d2= d1 - sigma*np.sqrt(T)
try:
if type=="c":
rho_calc= K*T*np.exp(-r*T)*stats.norm.cdf(d2, 0, 1)
elif type =="p":
rho_calc= (-K)*T*np.exp(-r*T)*stats.norm.cdf(-d2, 0, 1)
return rho_calc*0.01, rho(type, S, K, T, r, sigma)
except:
print('Please confirm all option parameters above !')
# All together
option_type="p"
print("Option Price:", [round(x,3) for x in blackScholes(r,S,K,T,sigma, option_type)])
print("Option Delta:", [round(x,3) for x in delta_calc(r,S,K,T,sigma, option_type)])
print("Option Gamma:", [round(x,3) for x in gamma_calc(r,S,K,T,sigma, option_type)])
print("Option Vega:", [round(x,3) for x in vega_calc(r,S,K,T,sigma, option_type)])
print("Option Theta:", [round(x,3) for x in theta_calc(r,S,K,T,sigma, option_type)])
print("Option Rho:", [round(x,3) for x in rho_calc(r,S,K,T,sigma, option_type)])