-
Notifications
You must be signed in to change notification settings - Fork 11
/
myfinutils.py
141 lines (105 loc) · 3.53 KB
/
myfinutils.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
def interestRate(Call, Put, Spot, Strk, TtM):
# INPUT
# Call : call price
# Put : put price
# Spot : spot price
# Strk : strike price
# TtM : time to maturity
# OUTPUT
# out : interest rate
import numpy as np
return np.log(Strk / (Spot + Put - Call)) / TtM
def impliedVolatility(target, flag, spot, strk, ttM, inR):
# INPUT:
# target : target price (Call or Put)
# flag : 'C' if call option 'P' if put option
# spot : spot price
# strk : strike price
# ttM : time to maturity
# inR : interest rate
# OUTPUT:
# sigma : implied volatility
import myblackscholes as bs
import numpy as np
import pandas as pd
MAX_ITER = 100
MAX_ERROR = 1.0e-5
sigma = 0.5
i = 0
while (i < MAX_ITER):
if flag == 'C': # call
price = bs.callPrice(spot, strk, ttM, inR, sigma)
elif flag == 'P': # put
price = bs.putPrice(spot, strk, ttM, inR, sigma)
else: # error
price = target * 0
vega = bs.vega(spot, strk, ttM, inR, sigma)
diff = target - price
if (abs(diff) < MAX_ERROR):
return sigma # vol
sigma = sigma + diff / vega
i += 1
# return best value so far
return sigma
def impliedVolatilitySurface(Target, flag, Spot, Strk, TtM, InR):
# INPUT:
# target : target price (Call or Put)
# opt : 'C' if call option 'P' if put option
# Spot : spot price
# Strk : strike price
# TtM : time to maturity
# InR : interest rate
# OUTPUT:
# Sigma : implied volatility surface
import myblackscholes as bs
import numpy as np
import pandas as pd
# initializes dataframe at 0.5
Sigma_col = Target.columns
Sigma = pd.DataFrame(0.5 * np.ones(Target.shape), columns=Sigma_col)
for i in range(0, Sigma.shape[0]):
for t in Sigma_col:
Sigma[t][i] = impliedVolatility(Target[t][i], flag, Spot[t][i], Strk[t][i], TtM[t][i], InR[t][i])
return Sigma
'''
def parallelImpliedVolatilitySurface(Target, opt_flag, Spot, Strk, TtM, InR):
# INPUT:
# target : target price (Call or Put)
# opt_flag : 'C' if call option 'P' if put option
# Spot : spot price
# Strk : strike price
# TtM : time to maturity
# InR : interest rate
# OUTPUT:
# Sigma : implied volatility
import myblackscholes as bs
import numpy as np
import pandas as pd
MAX_ITER = 100
MAX_ERROR = 1.0e-5
Sigma = pd.DataFrame(0.5 * np.ones(Target.shape), columns=Target.columns)
FLAG = (Sigma == 0.5)
print(FLAG)
Price = Target * 0
Vega = Target * 0
i = 0
while (i < MAX_ITER):
if opt_flag == 'C': # call
Price[FLAG] = bs.callPrice(Spot[FLAG], Strk[FLAG], TtM[FLAG], InR[FLAG], Sigma[FLAG])
elif opt_flag == 'P': # put
Price = bs.putPrice(Spot, Strk, TtM, InR, Sigma)
else: # error
return Target * 0
Vega[FLAG] = bs.vega(Spot[FLAG], Strk[FLAG], TtM[FLAG], InR[FLAG], Sigma[FLAG])
Diff = Target - Price
FLAG = Diff.abs() > MAX_ERROR
# debug
print('i =', i)
print(Diff)
if (FLAG.any(axis=None) == False):
return Sigma # vol
Sigma[FLAG] = Sigma[FLAG] + Diff[FLAG] / Vega[FLAG]
i += 1
# return best value so far
return Sigma
'''