Hi, so I am trying to make a plot that tracks the live advances and declines ratio for the day for different indices of the stock markets.
I want to plot multiple lines for different indices on the same plot so I can visualise which sectors are performing better than the benchmark index. I am also using a drop down to select which Indices I wish to track the ratio and Nifty 50 is the benchmark that I will also keep selected.
Right now, if I select multiple values from dropdown, it does not update the graph. It only updates basis a single value selected.
My code is below. You can access the data from here.
from dash import html
from dash import dcc
import dash
from dash.dependencies import Input, Output
import plotly.graph_objs as go
from nsetools import Nse
import pandas as pd
import datetime as dt
nse = Nse()
nse_adv = pd.DataFrame(nse.get_advances_declines())
indices = [{'label': name, 'value': name} for name in nse_adv['indice']]
# indices = [{'label': name, 'value': [idx, 4]} for name, idx in zip(nse_adv['indice'], nse_adv.index)]
app = dash.Dash()
app.layout = html.Div([html.H1('Index Advance Decline Dashboard'),
html.Div([dcc.Dropdown(id='indexval', options=indices, value=['NIFTY 50'], multi=True)]),
html.Div([
dcc.Graph(id='graph'),
dcc.Interval(id='interval-comp', interval=2000, n_intervals=0)
]),
])
rat_val = []
timer = []
@app.callback(Output('graph', 'figure'),
[Input('indexval', 'value'),
Input('interval-comp', 'n_intervals')])
def filter_index(ind, n):
nse_adv = pd.DataFrame(nse.get_advances_declines())
nse_adv['adr'] = round(nse_adv['advances'] / nse_adv['declines'], 2)
nse_adv['adr'][nse_adv['adr'] == 0] = nse_adv['advances']
nse_adv['date'] = dt.datetime.now()
nse_adv.set_index('date', inplace=True)
# num = nse_adv.iloc[ind]
timer.append(nse_adv.index[0].strftime('%Y-%m-%d %H:%M:%S'))
for i, j in zip(ind, range(len(nse_adv))):
rat_val.append(nse_adv[nse_adv['indice'] == i]['adr'][j])
# rat_val.append(num)
data = [go.Scatter(x=timer, y=rat_val, mode='lines+markers')]
layout = go.Layout(xaxis={'title': 'Period'}, yaxis={'title': 'Ratio'})
fig = go.Figure(data=data, layout=layout)
return fig
app.run_server()
print(rat_val)
print(timer)