-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy patheuropean_option.py
153 lines (136 loc) · 4.68 KB
/
european_option.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Author: shifulin
Email: [email protected]
"""
from math import log, sqrt, exp
import numpy as np
from scipy.stats import norm
# def bs_call(s, k, sigma, r, t):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# d2 = d1 - sigma * np.sqrt(t)
# return s * norm.cdf(d1) - k * np.exp(-r * t) * norm.cdf(d2)
#
#
# def bs_put(s, k, sigma, r, t):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# d2 = d1 - sigma * np.sqrt(t)
# return k * np.exp(-r * t) * norm.cdf(-d2) - s * norm.cdf(-d1)
def greeks(s, k, sigma, r, t, option_type):
sqrt_t = sqrt(t)
d1 = (log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * sqrt_t)
d2 = d1 - sigma * sqrt_t
tmp = exp(-pow(d1, 2) / 2.0)
tmp2 = sqrt(2.0 * np.pi * t)
tmp3 = r * k * exp(-r * t)
gamma = tmp / (s * sigma * tmp2)
theta_call = -(s * sigma * tmp) / (2.0 * tmp2) - tmp3 * norm.cdf(d2)
vega = s * sqrt_t * tmp / sqrt(2.0 * np.pi)
if option_type == 'Call':
delta = norm.cdf(d1)
theta = theta_call
else:
delta = norm.cdf(d1) - 1.0
theta = theta_call + tmp3
return delta, gamma, theta, vega
#
# def delta(s, k, sigma, r, t, option_type):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# if option_type == 'Call':
# return norm.cdf(d1)
# else:
# return norm.cdf(d1) - 1.0
#
#
# def gamma(s, k, sigma, r, t):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# return np.exp(-pow(d1, 2) / 2.0) / (s * sigma * np.sqrt(2.0 * np.pi * t))
#
#
# def theta(s, k, sigma, r, t, option_type):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# d2 = d1 - sigma * np.sqrt(t)
# theta_call = -(s * sigma * np.exp(-pow(d1, 2) / 2.0)) / (2.0 * np.sqrt(2.0 * np.pi * t)) - \
# r * k * np.exp(-r * t) * norm.cdf(d2)
# if option_type == 'Call':
# return theta_call
# else:
# return theta_call + r * k * np.exp(-r * t)
#
#
# def vega(s, k, sigma, r, t):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# return s * np.sqrt(t) * np.exp(-pow(d1, 2) / 2.0) / np.sqrt(2.0 * np.pi)
#
#
# def rho(s, k, sigma, r, t, option_type):
# d1 = (np.log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * np.sqrt(t))
# d2 = d1 - sigma * np.sqrt(t)
# if option_type == 'Call':
# return k * t * np.exp(-r * t) * norm.cdf(d2)
# else:
# return -k * t * np.exp(-r * t) * norm.cdf(-d2)
def bs_call(s, k, sigma, r, t):
tmp = sqrt(t)
d1 = (log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * tmp)
d2 = d1 - sigma * tmp
return s * norm.cdf(d1) - k * exp(-r * t) * norm.cdf(d2)
def bs_put(s, k, sigma, r, t):
tmp = sqrt(t)
d1 = (log(s / k) + (r + pow(sigma, 2) / 2.0) * t) / (sigma * tmp)
d2 = d1 - sigma * tmp
return k * exp(-r * t) * norm.cdf(-d2) - s * norm.cdf(-d1)
def call_iv(c, s, k, t, r=0.03, sigma_min=0.01, sigma_max=1.0, e=0.00001):
sigma_mid = (sigma_min + sigma_max) / 2.0
call_min = bs_call(s, k, sigma_min, r, t)
call_max = bs_call(s, k, sigma_max, r, t)
call_mid = bs_call(s, k, sigma_mid, r, t)
diff = c - call_mid
if c <= call_min:
return sigma_min
elif c >= call_max:
return sigma_max
while abs(diff) > e:
if c > call_mid:
sigma_min = sigma_mid
else:
sigma_max = sigma_mid
sigma_mid = (sigma_min + sigma_max) / 2.0
call_mid = bs_call(s, k, sigma_mid, r, t)
diff = c - call_mid
# print(sigma_mid)
return sigma_mid
def put_iv(c, s, k, t, r=0.03, sigma_min=0.01, sigma_max=1.0, e=0.00001):
sigma_mid = (sigma_min + sigma_max) / 2.0
put_min = bs_put(s, k, sigma_min, r, t)
put_max = bs_put(s, k, sigma_max, r, t)
put_mid = bs_put(s, k, sigma_mid, r, t)
diff = c - put_mid
if c <= put_min:
return sigma_min
elif c >= put_max:
return sigma_max
while abs(diff) > e:
if c > put_mid:
sigma_min = sigma_mid
else:
sigma_max = sigma_mid
sigma_mid = (sigma_min + sigma_max) / 2.0
put_mid = bs_put(s, k, sigma_mid, r, t)
diff = c - put_mid
return sigma_mid
# def my_test():
# call_iv(0.138, 3.046, 3.1, 0.5, r=0.03, sigma_min=0.01, sigma_max=1.0, e=0.000001)
#
#
# def my_test2():
# import matplotlib.pyplot as plt
# a = np.linspace(0, 0.8, 100)
# yc, yp = [], []
# for i in a:
# yc.append(vega(6.0, 5.0, i, 0.03, 0.5))
# yp.append(vega(6.0, 5.0, i, 0.03, 0.5))
# plt.plot(yc)
# plt.plot(yp)
# plt.show()
# if __name__ == '__main__':
# my_test2()