-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSM_streamlit.py
196 lines (159 loc) · 6.81 KB
/
BSM_streamlit.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
import streamlit as st
import seaborn as sns
import streamlit.components.v1 as components
def blackScholes(S, K, r, T, sigma, type="c"):
"Calculate Black Scholes option price for a call/put"
d1 = (np.log(S/K) + (r + sigma**2/2)* T)/(sigma*np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
try:
if type == "c":
price = S * norm.cdf(d1, 0, 1) - K * np.exp(-r * T) * norm.cdf(d2, 0, 1)
elif type == "p":
price = K * np.exp(-r * T) * norm.cdf(-d2, 0, 1) - S * norm.cdf(-d1, 0, 1)
return price
except:
st.sidebar.error("Please confirm all option parameters!")
def optionDelta (S, K, r, T, sigma, type="c"):
"Calculates option delta"
d1 = (np.log(S/K) + (r + sigma**2/2)* T)/(sigma*np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
try:
if type == "c":
delta = norm.cdf(d1, 0, 1)
elif type == "p":
delta = -norm.cdf(-d1, 0, 1)
return delta
except:
st.sidebar.error("Please confirm all option parameters!")
def optionGamma (S, K, r, T, sigma):
"Calculates option gamma"
d1 = (np.log(S/K) + (r + sigma**2/2)* T)/(sigma*np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
try:
gamma = norm.pdf(d1, 0, 1)/ (S * sigma * np.sqrt(T))
return gamma
except:
st.sidebar.error("Please confirm all option parameters!")
def optionTheta(S, K, r, T, sigma, type="c"):
"Calculates option theta"
d1 = (np.log(S/K) + (r + sigma**2/2)* T)/(sigma*np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
try:
if type == "c":
theta = - ((S * norm.pdf(d1, 0, 1) * sigma) / (2 * np.sqrt(T))) - r * K * np.exp(-r*T) * norm.cdf(d2, 0, 1)
elif type == "p":
theta = - ((S * norm.pdf(d1, 0, 1) * sigma) / (2 * np.sqrt(T))) + r * K * np.exp(-r*T) * norm.cdf(-d2, 0, 1)
return theta/year_length_T
except:
st.sidebar.error("Please confirm all option parameters!")
def optionVega (S, K, r, T, sigma):
"Calculates option vega"
d1 = (np.log(S/K) + (r + sigma**2/2)* T)/(sigma*np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
try:
vega = S * np.sqrt(T) * norm.pdf(d1, 0, 1) * 0.01
return vega
except:
st.sidebar.error("Please confirm all option parameters!")
def optionRho(S, K, r, T, sigma, type="c"):
"Calculates option rho"
d1 = (np.log(S/K) + (r + sigma**2/2)* T)/(sigma*np.sqrt(T))
d2 = d1 - sigma * np.sqrt(T)
try:
if type == "c":
rho = 0.01 * K * T * np.exp(-r*T) * norm.cdf(d2, 0, 1)
elif type == "p":
rho = 0.01 * -K * T * np.exp(-r*T) * norm.cdf(-d2, 0, 1)
return rho
except:
st.sidebar.error("Please confirm all option parameters!")
st.set_page_config(page_title="Black-Scholes Model")
sidebar_title = st.sidebar.header("Black-Scholes Parameters")
space = st.sidebar.header("")
r = st.sidebar.number_input("Risk-Free Rate", min_value=0.000, max_value=1.000, step=0.0001, value=0.0339, format="%1.4f")
S = st.sidebar.number_input("Underlying Asset Price", min_value=1.00, step=0.10, value=133.32)
K = st.sidebar.number_input("Strike Price", min_value=1.00, step=0.10, value=145.00)
days_to_expiry = st.sidebar.number_input("Time to Expiry Date (in days)", min_value=1.0, step=0.1, value=5.0)
year_length_E = st.sidebar.number_input("Year Length for Expiry (in days)", min_value=1, step=1, value=252)
year_length_T = st.sidebar.number_input("Year Length for Theta (in days)", min_value=1, step=1, value=252)
sigma = st.sidebar.number_input("Volatility", min_value=0.000, max_value=1.000, step=0.0001, value=0.85664, format="%1.5f")
type_input = st.sidebar.selectbox("Option Type",["Call", "Put"])
type=""
if type_input=="Call":
type = "c"
elif type_input=="Put":
type = "p"
T = days_to_expiry/year_length_E
spot_prices = [i for i in range(0, int(S)+50 + 1)]
prices = [blackScholes(i, K, r, T, sigma, type) for i in spot_prices]
deltas = [optionDelta(i, K, r, T, sigma, type) for i in spot_prices]
gammas = [optionGamma(i, K, r, T, sigma) for i in spot_prices]
thetas = [optionTheta(i, K, r, T, sigma, type) for i in spot_prices]
vegas = [optionVega(i, K, r, T, sigma) for i in spot_prices]
rhos = [optionRho(i, K, r, T, sigma, type) for i in spot_prices]
sns.set_style("whitegrid")
fig1, ax1 = plt.subplots()
sns.lineplot(spot_prices, prices)
ax1.set_ylabel('Option Price')
ax1.set_xlabel("Underlying Asset Price")
ax1.set_title("Option Price")
fig2, ax2 = plt.subplots()
sns.lineplot(spot_prices, deltas)
ax2.set_ylabel('Delta')
ax2.set_xlabel("Underlying Asset Price")
ax2.set_title("Delta")
fig3, ax3 = plt.subplots()
sns.lineplot(spot_prices, gammas)
ax3.set_ylabel('Gamma')
ax3.set_xlabel("Underlying Asset Price")
ax3.set_title("Gamma")
fig4, ax4 = plt.subplots()
sns.lineplot(spot_prices, thetas)
ax4.set_ylabel('Theta')
ax4.set_xlabel("Underlying Asset Price")
ax4.set_title("Theta")
fig5, ax5 = plt.subplots()
sns.lineplot(spot_prices, vegas)
ax5.set_ylabel('Vega')
ax5.set_xlabel("Underlying Asset Price")
ax5.set_title("Vega")
fig6, ax6 = plt.subplots()
sns.lineplot(spot_prices, rhos)
ax6.set_ylabel('Rho')
ax6.set_xlabel("Underlying Asset Price")
ax6.set_title("Rho")
fig1.tight_layout()
fig2.tight_layout()
fig3.tight_layout()
fig4.tight_layout()
fig5.tight_layout()
fig6.tight_layout()
st.markdown("<h2 align='center'>Black-Scholes Option Price Calculator</h2>", unsafe_allow_html=True)
st.markdown("<h5 align='center'>Made by Tiago Moreira</h5>", unsafe_allow_html=True)
st.header("")
st.markdown("<h6>See project's description and assumptions here: <a href='https://github.com/TFSM00/Black-Scholes-Calculator'>https://github.com/TFSM00/Black-Scholes-Calculator</a></h6>", unsafe_allow_html=True)
st.markdown("<h6>See all my other projects here: <a href='https://github.com/TFSM00'>https://github.com/TFSM00</a></h6>", unsafe_allow_html=True)
st.header("")
st.markdown("<h3 align='center'>Option Prices and Greeks</h3>", unsafe_allow_html=True)
st.header("")
col1, col2, col3, col4, col5 = st.columns(5)
col2.metric("Call Price", str(round(blackScholes(S, K, r, T, sigma,type="c"), 3)))
col4.metric("Put Price", str(round(blackScholes(S, K, r, T, sigma,type="p"), 3)))
bcol1, bcol2, bcol3, bcol4, bcol5 = st.columns(5)
bcol1.metric("Delta", str(round(blackScholes(S, K, r, T, sigma,type="c"), 3)))
bcol2.metric("Gamma", str(round(optionGamma(S, K, r, T, sigma), 3)))
bcol3.metric("Theta", str(round(optionTheta(S, K, r, T, sigma,type="c"), 3)))
bcol4.metric("Vega", str(round(optionVega(S, K, r, T, sigma), 3)))
bcol5.metric("Rho", str(round(optionRho(S, K, r, T, sigma,type="c"), 3)))
st.header("")
st.markdown("<h3 align='center'>Visualization of the Greeks</h3>", unsafe_allow_html=True)
st.header("")
st.pyplot(fig1)
st.pyplot(fig2)
st.pyplot(fig3)
st.pyplot(fig4)
st.pyplot(fig5)
st.pyplot(fig6)