4
4
from bokeh .io import curdoc
5
5
from bokeh .layouts import row , column , gridplot , layout
6
6
from bokeh .palettes import Spectral6
7
- from bokeh .models import ColumnDataSource
7
+ from bokeh .models import ColumnDataSource , Range1d
8
8
from bokeh .models .widgets import Select , Button
9
9
from bokeh .plotting import figure , output_file , show
10
10
@@ -33,32 +33,48 @@ def get_open_interest(data, symbol):
33
33
pivot = temp .pivot (index = 'timestamp' , columns = 'expiry_dt' , values = 'open_int' )
34
34
pivot .rename (lambda x : x .strftime ('%Y-%m-%d' ),
35
35
axis = 'columns' , inplace = True )
36
+ pct_pivot = pivot .pct_change ()* 100
36
37
return (list (pivot .columns ), pivot .reset_index ())
37
38
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 ()
46
43
44
+ # Create widgets
45
+ select_symbol = Select (options = symbols , title = 'Select a symbol' ,
46
+ value = 'NIFTY' )
47
+ button = Button (label = 'Refresh' , button_type = "success" )
47
48
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 )
52
61
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 )
55
70
56
- # Display the dashboard
57
- l = layout (
58
- row ([select_symbol , p ])
59
- )
60
- show (l )
61
71
72
+ # Display the dashboard
73
+ l = layout (
74
+ column (
75
+ row (select_symbol ,button ),
76
+ p )
77
+ )
62
78
63
- if __name__ == "__main__" :
64
- main ( )
79
+ curdoc (). add_root ( l )
80
+ show ( l )
0 commit comments