Black-Scholes Part 1
We are going to build interactive dashboards based off of the options part 1 course in this course. To start with we will try to work with the black-scholes model to develop interesting option valuation dashboards.
First, we need to start with the basics. Let’s create the widgets and then create a dummy function that simply prints the parameters. We will eventually change this function into its final form. Start with the following code.
from IPython.display import display
from ipywidgets import widgets, interact
#Build the widgets
X = widgets.IntSlider(
value=50,
min=1,
max=100,
step=1,
description='Strike Price:',
readout_format='.1f',
)
t = widgets.FloatSlider(
value=1,
min=0.01,
max=10,
step=.01,
description='Time to Maturity:',
readout_format='.2f',
)
rf = widgets.FloatSlider(
value=.05,
min=0,
max=.20,
step=.0001,
description='Risk-free Rate:',
readout_format='.2%',
)
sigma = widgets.FloatSlider(
value=.08,
min=0,
max=.80,
step=.0001,
description='Implied Volatility:',
readout_format='.2%',
)
#Create a dummy function
def compute_BS_calls(X, sigma, rf, t):
print(X, sigma, rf, t)
interact(compute_BS_calls,X=X, sigma=sigma, rf=rf, t=t);
After playing with the widgets, close them all.
X.close()
sigma.close()
rf.close()
t.close()
To make our lives easier, we can wrap the widget creation into a function.
#Build a formula to automatically create the widgets
def build_widgets():
X = widgets.IntSlider(
value=50,
min=1,
max=100,
step=1,
description='Strike Price:',
readout_format='.1f',
)
t = widgets.FloatSlider(
value=1,
min=0.01,
max=10,
step=.01,
description='Time to Maturity:',
readout_format='.2f',
)
rf = widgets.FloatSlider(
value=.05,
min=0,
max=.20,
step=.0001,
description='Risk-free Rate:',
readout_format='.2%',
)
sigma = widgets.FloatSlider(
value=.08,
min=0,
max=.80,
step=.0001,
description='Implied Volatility:',
readout_format='.2%',
)
return X, t, rf, sigma
X, t, rf, sigma = build_widgets()
interact(compute_BS_calls,X=X, sigma=sigma, rf=rf, t=t);
As well, we could add a widget closing function.
#Create a function to close the widgets
def close_widgets(widgets):
for widget in widgets:
widget.close()
close_widgets([X, t, rf, sigma])
Challenge
Find the black-scholes call options formula from the prior lessons, then create a function which returns a pandas series of the black-scholes prices given parameters as fixed and the prices between 0 and 100 as the independent variable/index for the pandas series. Use this as a function for the ipywidgets.