-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode1
101 lines (84 loc) · 2.62 KB
/
Code1
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
from math import log, sqrt, pi, exp
from scipy.stats import norm
from datetime import datetime, date
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive, fixed, interact_manual
import plotly.graph_objects as go
from ipywidgets import widgets
def d1(S,K,T,r,sigma):
return(log(S/K)+(r+sigma**2/2.)*T)/(sigma*sqrt(T))
def d2(S,K,T,r,sigma):
return d1(S,K,T,r,sigma)-sigma*sqrt(T)
def bs_call(S,K,T,r,sigma):
return S*norm.cdf(d1(S,K,T,r,sigma))-K*exp(-r*T)*norm.cdf(d2(S,K,T,r,sigma))
def bs_put(S,K,T,r,sigma):
return K*exp(-r*T)*(1-norm.cdf(d2(S,K,T,r,sigma)))-S*(1-norm.cdf(d1(S,K,T,r,sigma)))
def call_delta(S,K,T,r,sigma):
return norm.cdf(d1(S,K,T,r,sigma))
def call_gamma(S,K,T,r,sigma):
return norm.pdf(d1(S,K,T,r,sigma))/(S*sigma*sqrt(T))
def call_vega(S,K,T,r,sigma):
return 0.01*(S*norm.pdf(d1(S,K,T,r,sigma))*sqrt(T))
def call_theta(S,K,T,r,sigma):
return 0.01*(-(S*norm.pdf(d1(S,K,T,r,sigma))*sigma)/(2*sqrt(T)) - r*K*exp(-r*T)*norm.cdf(d2(S,K,T,r,sigma)))
def call_rho(S,K,T,r,sigma):
return 0.01*(K*T*exp(-r*T)*norm.cdf(d2(S,K,T,r,sigma)))
def put_delta(S,K,T,r,sigma):
return -norm.cdf(-d1(S,K,T,r,sigma))
def put_gamma(S,K,T,r,sigma):
return norm.pdf(d1(S,K,T,r,sigma))/(S*sigma*sqrt(T))
def put_vega(S,K,T,r,sigma):
return 0.01*(S*norm.pdf(d1(S,K,T,r,sigma))*sqrt(T))
def put_theta(S,K,T,r,sigma):
return 0.01*(-(S*norm.pdf(d1(S,K,T,r,sigma))*sigma)/(2*sqrt(T)) + r*K*exp(-r*T)*norm.cdf(-d2(S,K,T,r,sigma)))
def put_rho(S,K,T,r,sigma):
return 0.01*(-K*T*exp(-r*T)*norm.cdf(-d2(S,K,T,r,sigma)))
x=5
y=15
Step_Spot=0.1
Spot = np.arange(x, y, Step_Spot)
Stime=0.01
Etime=1
Step_Time=0.01
Time = np.arange(Stime, Etime, Increment)
K=10
r=0.04
sigma=0.3
df=pd.DataFrame(Spot,columns=['Spot'])
df['Strike']=K
Delta_Call=[]
for t in Time:
for s in Spot:
S=s
T=t
key_Name = "Delta t=" + str(round(t,2))
Delta_Call.append(call_delta(S,K,T,r,sigma))
df[key_Name]=Delta_Call
Delta_Call=[]
#df
def plot_func(Maturity):
x = df['Spot']
y1=df['Delta t=0.1']
Delta="Delta t="+str(round(Maturity,2))
y2 = df[Delta]
plt.plot(x, y1, x, y2)
plt.grid(True)
plt.ylabel("Delta Call",fontsize=10)
plt.xlabel("Spot")
plt.suptitle("Delta vs Spot")
plt.show
Expy=widgets.FloatSlider(
value=Step_Time,
min=Stime,
max=Etime-Stime,
step=Step_Time,
description='Maturity:',
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='.2f'
)
interact(plot_func, Maturity=Expy)