Skip to content

Commit f56cb39

Browse files
committed
interactivity added
1 parent 396bdfb commit f56cb39

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

oi_graph/futures_oi.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from bokeh.io import curdoc
55
from bokeh.layouts import row, column, gridplot, layout
66
from bokeh.palettes import Spectral6
7-
from bokeh.models import ColumnDataSource
7+
from bokeh.models import ColumnDataSource, Range1d
88
from bokeh.models.widgets import Select, Button
99
from bokeh.plotting import figure, output_file, show
1010

@@ -33,32 +33,48 @@ def get_open_interest(data, symbol):
3333
pivot = temp.pivot(index='timestamp', columns='expiry_dt', values='open_int')
3434
pivot.rename(lambda x: x.strftime('%Y-%m-%d'),
3535
axis='columns', inplace=True)
36+
pct_pivot = pivot.pct_change()*100
3637
return (list(pivot.columns), pivot.reset_index())
3738

38-
def main():
39-
output_file('futures_oi.html')
40-
df = load_data()
41-
symbols = list(df.symbol.unique())
42-
source = ColumnDataSource()
43-
44-
# Create widgets
45-
select_symbol = Select(options=symbols, title='Select a symbol')
39+
output_file('futures_oi.html')
40+
df = load_data()
41+
symbols = list(df.symbol.unique())
42+
source = ColumnDataSource()
4643

44+
# Create widgets
45+
select_symbol = Select(options=symbols, title='Select a symbol',
46+
value='NIFTY')
47+
button = Button(label='Refresh', button_type="success")
4748

48-
# Create plots
49-
p = figure(title='Open interest chart for futures')
50-
cols, data = get_open_interest(df, 'SBIN')
51-
colors = Spectral6[:len(cols)]
49+
# Create plots
50+
p = figure(title='Open interest chart for NIFTY futures')
51+
cols, data = get_open_interest(df, 'NIFTY')
52+
colors = Spectral6[:len(cols)]
53+
source.data = source.from_df(data)
54+
print(cols, colors)
55+
p.vbar_stack(cols, width=0.6, x='index', color=colors, source=source)
56+
57+
# setup callbacks
58+
def update():
59+
symbol = select_symbol.value
60+
cols, data = get_open_interest(df, symbol)
5261
source.data = source.from_df(data)
53-
print(cols, colors)
54-
p.vbar_stack(cols, width=0.6, x='index', color=colors, source=source)
62+
max_val = data.sum(axis=1).max()
63+
print(cols, symbol, max_val)
64+
p.y_range = Range1d(100, max_val)
65+
p.title.text = 'Open interest chart for {} futures'.format(symbol)
66+
67+
68+
# set up event triggers
69+
button.on_click(update)
5570

56-
# Display the dashboard
57-
l = layout(
58-
row([select_symbol, p])
59-
)
60-
show(l)
6171

72+
# Display the dashboard
73+
l = layout(
74+
column(
75+
row(select_symbol,button),
76+
p)
77+
)
6278

63-
if __name__ == "__main__":
64-
main()
79+
curdoc().add_root(l)
80+
show(l)

0 commit comments

Comments
 (0)