forked from cryptarbitrage-code/deribit-position-greeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
black_scholes.py
61 lines (43 loc) · 1.69 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
import numpy as np
from scipy.stats import norm
N = norm.cdf
Np = norm.pdf
def bs_price(S, K, T, R, sigma, option_type):
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 * N(d1) - K * np.exp(-R*T)* N(d2)
elif option_type == "P":
price = K*np.exp(-R*T)*N(-d2) - S*N(-d1)
return price
def bs_delta(S, K, T, R, sigma, option_type):
d1 = (np.log(S / K) + (R + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))
if option_type == "C":
delta = N(d1)
elif option_type == "P":
delta = N(d1) - 1
return delta
def bs_gamma(S, K, T, R, sigma):
d1 = (np.log(S / K) + (R + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))
gamma = Np(d1) / (S * sigma * np.sqrt(T))
return gamma
def bs_vega(S, K, T, R, sigma):
d1 = (np.log(S / K) + (R + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))
vega = S * Np(d1) * np.sqrt(T)
return vega * 0.01
def bs_theta(S, K, T, R, sigma, option_type):
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 = -S * Np(d1) * sigma / (2 * np.sqrt(T)) - R * K * np.exp(-R * T) * N(d2)
elif option_type == "P":
theta = -S * Np(d1) * sigma / (2 * np.sqrt(T)) + R * K * np.exp(-R * T) * N(-d2)
return theta / 365
def bs_rho(S, K, T, R, sigma, option_type):
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 = K * T * np.exp(-R * T) * N(d2)
elif option_type == "P":
rho = -K * T * np.exp(-R * T) * N(-d2)
return rho * 0.01