-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathImplied_VOT.py
More file actions
95 lines (71 loc) · 2.55 KB
/
Implied_VOT.py
File metadata and controls
95 lines (71 loc) · 2.55 KB
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
# Only using toolbox functions for normal CDF calculation and mathematical tools and excel operations
from math import *
import random
from scipy.stats import norm
import openpyxl
# Opening the data file
wb = openpyxl.load_workbook('Data1.xlsx')
sheet1 = wb.get_sheet_by_name('SPY1118')
# Initializing constants:
T = 52/365 # Time to maturity = Recorded on 9/26
S = 214.95 # Underlying asset price
r = 0.4 # interest rate
tol = 10**-6 # tolerance
A = 2 # starting row in excel
# Initializing arrays for variables
K = [0]*101 # Array of strike prices
Vol = [0]*101 # Array of implied volatility
Bid = [0]*101
Ask = [0]*101
C = [0]*101
option = 'Call'
def impliedvol(S, K, T, r, option,C,tol):
def CBS(S, K, T, r, sigma, option):
# Calculations for the solution to BSM equation
dplus = (1 / (sigma * sqrt(T))) * ((log(S / K)) + (r + (sigma ** 2) / 2) * T)
dminus = (1 / (sigma * sqrt(T))) * ((log(S / K)) + (r - (sigma ** 2) / 2) * T)
# Calculating price of Call and Put
if option == 'Call':
return S * norm.cdf(dplus) - K * exp(-r * T) * norm.cdf(dminus)
elif option == 'Put':
return K * exp(-r * T) * norm.cdf(-dminus) - S * norm.cdf(-dplus)
def f(x, option):
return CBS(S, K, T, r, x, option) - C
# Calculating implied votality by bisection method
t = 0
while t < 1000:
t = t + 1
a = random.uniform(0, 100)
b = random.uniform(0, 100)
if (f(a, option)) * (f(b, option)) < 0:
break
else:
continue
def bsecvot(S, K, T, r, option, a, b):
c = (a + b) / 2
if f(a,option) == 0:
return a
elif f(b,option) == 0:
return b
elif f(a,option) * f(b,option) < 0:
while abs(b - a) > tol:
if f(a,option) * f(c,option) < 0:
b = c
c = (a + b) / 2
# print('C1')
continue
elif f(b,option)*f(c,option) < 0:
a = c
c = (a + b) / 2
#print('C2')
continue
return c
return bsecvot(S, K, T, r, option, a, b)
while A < 124:
A=A+1
Bid[A] = sheet1.cell(row=A, column=4).value
Ask[A] = sheet1.cell(row=A, column=5).value
K[A] = sheet1.cell(row=A, column=1).value
C[A] = (Bid[A] + Ask[A]) / 2
Vol[A] = impliedvol(S,K[A],T,r,option,C[A],tol)
print(Vol)