Hi Dash community,
I have been working on some for a week and I have encountered a blocker.
My problem is the following: given two values (first level RadioItems and second level Dropdown), create fields of Inputs (therefore the set of inputs asked varies depending on the combination of RadioItems and Dropdown values selected).
I have managed to create the inputs fields however I cannot process the inputs once they have been provided (depending on the combination a graph is meant to be plotted).
Has anyone faced the same issue?
Thank you
#############################################################################
app layout
app.layout = html.Div([html.H1('Simulation of stock paths', style={'text-align': 'center', 'font-family':'verdana'}),\
html.H5('Generate stock paths according to different stochastic processes',\
style={'text-align': 'center','font-family':'verdana'}),\
dcc.Markdown('Select a process: ', style={'text-align': 'left', 'font-family': 'verdana'}),\
html.Div(\
dcc.RadioItems(id='process',options=[{'label': x, 'value': x} for x in\
['black-scholes', 'heston']],\
value='black-scholes',style={'text-align': 'left', 'font-family': 'verdana',\
'display': 'inline-block'},\
inputStyle={'margin': '10px'}),style={'display': 'inline-block'}),\
html.Br(),\
dcc.Dropdown(multi=False, style={'text-align': 'left', 'font-family': 'verdana'},\
id='model_specification'), \
html.Br(),\
html.Div(id='model_parameters'),\
html.Br(), \
html.Div(id='my-output'),\
dcc.Graph(id='stock_path')])
code for my second selection level
@app.callback(
[Output('model_specification', 'options'),\
Output('model_specification', 'value')],\
Input('process', 'value'))
def dropdown_options(process):
if process == 'black-scholes':
options_dd = {model_specification: model_specification for model_specification in ['no-jump','merton']}
elif process == 'heston':
options_dd = {model_specification: model_specification for model_specification in ['no-jump','bates']}
value = options_dd[list(options_dd.keys())[0]]
return options_dd, value
Inputs depending on the selection above
@app.callback(
Output('model_parameters','children'),\
[Input('process','value'),\
Input('model_specification','value')]
)
def parameters_input(process,model_specification):
params_ls = [dcc.Input(id='S0', placeholder='Insert S0', min=0.00, step=1, size='md',\
type='number',value=100,debounce=True),\
dcc.Input(id='r', min=0.00, step=.01, size='md', placeholder='Insert r',\
type='number',value=.05,debounce=True)]
if process == 'black-scholes':
process_param_ls = [dcc.Input(id='vol',min=0.00,step=.01,size='md',placeholder='Insert vol',type='number',\
debounce=True,value=.2)]
if model_specification == 'no-jump':
add_param_ls = []
elif model_specification == 'merton':
add_param_ls = [dcc.Input(id='jump_intensity',placeholder='Insert jump_intensity',min=0.00,step=1,\
size='md',type='number',value=1,debounce=True),\
dcc.Input(id='mu_j', min=0.00, step=.1, size='md',\
placeholder='Insert mu_j',value=.4,type='number',debounce=True), \
dcc.Input(id='vol_j',min=0.00,step=.1,size='md',placeholder='Insert vol_j',\
type='number',debounce=True,value=.2)]
elif process == 'heston':
process_param_ls = [dcc.Input(id='speed', placeholder='Insert k', min=0.00, step=.1, \
size='md', type='number', debounce=True,value=2), \
dcc.Input(id='mean', min=0.00, step=.1, size='md', \
placeholder='Insert mean of vol', type='number',debounce=True,value=.1), \
dcc.Input(id='vol_of_vol', min=0.00, step=.1, size='md',\
placeholder='Insert vol of vol', \
type='number', debounce=True,value=.5), \
dcc.Input(id='corr', min=0.00, step=.1, size='md', placeholder='Insert corr', \
type='number', debounce=True,value=.7)]
if model_specification == 'no-jump':
add_param_ls = []
elif model_specification == 'bates':
add_param_ls = [dcc.Input(id='jump_intensity', placeholder='Insert jump_intensity', min=0.00, step=1, \
size='40', type='number', debounce=True,value=1), \
dcc.Input(id='mu_j', min=0.00, step=.1, size='md', \
placeholder='Insert mu_j', type='number', debounce=True,value=.5), \
dcc.Input(id='vol_j', min=0.00, step=.1, size='md', placeholder='Insert vol_j', \
type='number', debounce=True,value=.2)]
else:
pass
return params_ls+process_param_ls+add_param_ls
Final plot using inputs entered by the user
@app.callback(
Output('stock_path', 'figure'),
[Input('process', 'value'),
Input('model_specification', 'value')]
)
def simulation(process,model_specification,*args):
if (args[0] == None)|(args[1] == None):
args[0] = 100
args[1] = 0.05
stock_sim = model(S0=args[0],r=args[1])
df = stock_sim.create_path(model_specification)
if process == 'black-scholes':
if model_specification == 'no-jump':
df = stock_sim.create_path(model_specification,args[2])
elif model_specification == 'merton':
df = stock_sim.create_path(model_specification,args[2],args[3],args[4],args[5])
elif process == 'heston':
stock_sim = model(process=process)
if model_specification == 'no-jump':
df = stock_sim.create_path(model_specification,args[2],args[3],args[4],args[5])
elif model_specification == 'bates':
df = stock_sim.create_path(model_specification,args[2],args[3],args[4],args[5],args[6],args[7])
fig = px.line(df, title='Monte Carlo simulation', labels={'value': 'Prices', 'index': 'Time'})
return fig