-
Notifications
You must be signed in to change notification settings - Fork 0
/
pricing.py
155 lines (124 loc) · 3.94 KB
/
pricing.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
153
154
import os
import pandas as pd
import datetime
import numpy as np
import math as m
import scipy.stats as ss
def call_bsm(S0, K, r, T, sqrt_T, Otype, sig):
d1 = ((m.log(S0/K)) + (r+ (sig*sig)/2)*T)/(sig*sqrt_T)
d2 = d1 - sig*sqrt_T
if (Otype == "C"):
price = S0*(ss.norm.cdf(d1)) \
- K*(m.exp(-r*T))*(ss.norm.cdf(d2))
return (price)
elif (Otype == "P"):
price = -S0*(ss.norm.cdf(-d1))\
+ K*(m.exp(-r*T))*(ss.norm.cdf(-d2))
return price
def call_black76(S0, K, r, T, sqrt_T, Otype, sig):
d1 = ((m.log(S0/K)) + ((sig*sig)/2)*T)/(sig*sqrt_T)
d2 = d1 - sig*sqrt_T
if (Otype == "C"):
price = S0*m.exp(-r*T)*(ss.norm.cdf(d1)) \
- K*(m.exp(-r*T))*(ss.norm.cdf(d2))
return (price)
elif (Otype == "P"):
price = -S0*m.exp(-r*T)*(ss.norm.cdf(-d1))\
+ K*(m.exp(-r*T))*(ss.norm.cdf(-d2))
return price
def vega(S0, K, r, T, sqrt_T, sig):
d1 = ((m.log(S0/K)) + (r+ (sig*sig)/2)*T)/(sig*sqrt_T)
vega = S0*(ss.norm.pdf(d1))*sqrt_T
return vega
def vega76(S0, K, r, T, sqrt_T, sig):
d1 = ((m.log(S0/K)) + ((sig*sig)/2)*T)/(sig*sqrt_T)
vega = S0*(ss.norm.pdf(d1))*sqrt_T
return vega
def implied_volatility(S0, K, T, r, price, Otype):
e = 1e-3
x0 = 1
sqrt_T = m.sqrt(T)
def newtons_method(S0, K, T, sqrt_T, r, price, Otype, x0, e):
k=0
delta = call_bsm(S0, K, r, T, sqrt_T, Otype, x0) - price
while delta > e:
k=k+1
if (k > 30):
return np.nan
_vega = vega(S0, K, r, T, sqrt_T, x0)
if (_vega == 0.0):
return np.nan
x0 = (x0 - (call_bsm(S0, K, r, T, sqrt_T, Otype, x0) - price)/_vega)
delta = abs(call_bsm(S0, K, r, T, sqrt_T, Otype, x0) - price)
return x0
iv = newtons_method(S0, K, T, sqrt_T, r, price, Otype, x0, e)
return iv
def implied_volatility_black76(S0, K, T, r, price, Otype):
e = 1e-3
x0 = 1
sqrt_T = m.sqrt(T)
def newtons_method(S0, K, T, sqrt_T, r, price, Otype, x0, e):
k=0
delta = call_black76(S0, K, r, T, sqrt_T, Otype, x0) - price
while delta > e:
k=k+1
if (k > 30):
return np.nan
_vega = vega76(S0, K, r, T, sqrt_T, x0)
if (_vega == 0.0):
return np.nan
x0 = (x0 - (call_black76(S0, K, r, T, sqrt_T, Otype, x0) - price)/_vega)
delta = abs(call_black76(S0, K, r, T, sqrt_T, Otype, x0) - price)
return x0
iv = newtons_method(S0, K, T, sqrt_T, r, price, Otype, x0, e)
return iv
def calculate_greeks(S0, K, T, r, price, Otype):
if (np.isnan(S0) or np.isnan(K) or np.isnan(price) or S0==0.0 or price==0.0):
return [np.nan, np.nan]
if (Otype == 'C' and S0/K > 1.25):
return [0, 0]
if (Otype == 'P' and K/S0 > 1.25):
return [0, 0]
if (T < 0.25/365):
return [np.nan, np.nan]
sqrt_T = m.sqrt(T)
# print(S0, K, T, r, price, Otype)
# imp_vol
iv = implied_volatility(S0, K, T, r, price, Otype)
d1 = ((m.log(S0/K)) + (r + (iv*iv)/2)*T)/(iv*sqrt_T)
# iv = implied_volatility_black76(S0, K, T, r, price, Otype)
# d1 = ((m.log(S0/K)) + ((iv*iv)/2)*T)/(iv*sqrt_T)
# delta
if Otype == 'C':
delta = ss.norm.cdf(d1)
else:
delta = ss.norm.cdf(d1) - 1
return [round(iv,5), round(delta,4)]
if __name__=="__main__":
# FUT PRICE
S0 = 550.4
# STRIKE
K = 600
# TIME TO EXPIRY
T = 13 / 360
# INTEREST RATE
r = 0.02
# OPTION PRICE
price = 2.5
# OPTION TYPE
Otype = 'C'
# # FUT PRICE
# S0 = 19320
# # STRIKE
# K = 19400
# # TIME TO EXPIRY
# T = 17 / 360
# # INTEREST RATE
# r = 0.02
# # OPTION PRICE
# price = 305
# # OPTION TYPE
# Otype = 'P'
iv, delta = calculate_greeks(S0, K, T, r, price, Otype)
print(iv, delta)
pass