-
Notifications
You must be signed in to change notification settings - Fork 8
/
option_calculations.py
111 lines (89 loc) · 3.79 KB
/
option_calculations.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import numpy as np
from scipy.stats import norm
N = norm.cdf
Np = norm.pdf
def option_profit_expiry(premium, strike, size, chart_low, chart_high, option_type, step_number):
# calculates the profit at expiry of a linear option
profit_list = []
step = (chart_high - chart_low) / step_number
count = 0
if option_type == "C":
while count < step_number + 1:
if chart_low + (count * step) < strike:
profit_list.append((0 - premium) * size)
else:
profit_list.append(((chart_low + (count * step)) - strike - premium) * size)
count = count + 1
else:
while count < step_number + 1:
if chart_low + (count * step) < strike:
profit_list.append((strike - (chart_low + (count * step)) - premium) * size)
else:
profit_list.append((0 - premium) * size)
count = count + 1
return profit_list
def inverse_option_profit_expiry(premium, strike, size, chart_low, chart_high, option_type, step_number):
# calculates the profit at expiry of an inverse option
profit_list = []
step = (chart_high - chart_low) / step_number
count = 0
if option_type == "C":
while count < step_number + 1:
if chart_low + (count * step) < strike:
profit_list.append((0 - premium) * size)
else:
profit_list.append((((chart_low + (count * step)) - strike)/(chart_low + (count * step)) - premium) * size)
count = count + 1
else:
while count < step_number + 1:
if chart_low + (count * step) < strike:
profit_list.append(((strike - (chart_low + (count * step)))/(chart_low + (count * step)) - premium) * size)
else:
profit_list.append((0 - premium) * size)
count = count + 1
return profit_list
def bs_price(S, K, T, R, 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 * 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):
# calculates the black scholes delta of an option
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):
# calculates the black scholes gamma of an option
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):
# calculates the black scholes vega of an option
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):
# calculates the black scholes 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 = -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):
# calculates the black scholes 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 = 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