2
2
import pandas as pd
3
3
import datetime
4
4
5
- from bokeh .io import curdoc
5
+ from bokeh .io import curdoc , show , output_file
6
6
from bokeh .layouts import row , column
7
7
from bokeh .plotting import figure
8
- from bokeh .models import ColumnDataSource
8
+ from bokeh .models import ColumnDataSource , FactorRange
9
9
from bokeh .models .widgets import Select , TextInput , DatePicker , Button
10
+ from bokeh .transform import factor_cmap
10
11
11
12
# Load data, probably from a database. I am loading it directly from a file
12
13
df = pd .read_hdf ('derivatives_may_2019.h5' )
@@ -17,26 +18,112 @@ def update_expiry_date(attrname, old, new):
17
18
"""
18
19
Update expiry date for this particular symbol
19
20
"""
20
- print (attrname , old , new )
21
21
q = 'symbol == "{}"' .format (new )
22
22
dates = sorted (df .query (q ).expiry_dt .unique ())
23
23
dates = [str (dt )[:10 ] for dt in dates ]
24
24
expiry_select .options = dates
25
+ expiry_select .value = dates [0 ]
26
+ print (select_symbols , select_symbols .value , date_picker .value )
27
+
28
+ def update_oi_chart ():
29
+ pass
30
+
31
+ def query (dataframe , symbol = 'NIFTY' , date = '2019-05-02' , expiry = '2019-05-30' ):
32
+ """
33
+ Given a dataframe and query string, return the queried data
34
+ """
35
+ cond = '(symbol=="{symbol}") & (timestamp == "{dt}") & (expiry_dt=="{exp}") & (option_typ != "XX")'
36
+ q = cond .format (symbol = symbol , dt = str (date )[:10 ], exp = str (expiry )[:10 ])
37
+ print ('QUERY IS' , q )
38
+ df2 = dataframe .query (q )[['strike_pr' , 'option_typ' , 'open_int' ]]
39
+ df2 .sort_values (by = ['strike_pr' ], inplace = True )
40
+ return df2
41
+
42
+ def get_data_as_source (data ):
43
+ """
44
+ Get data in source format
45
+ """
46
+ x = [(str (int (a )), str (b )) for a ,b in zip (q .strike_pr , q .option_typ )]
47
+ source = ColumnDataSource (data = dict (x = x , values = data .open_int .values ))
48
+ return source
49
+
50
+ def fig1 ():
51
+ """
52
+ Generate the first figure
53
+ """
54
+ q = query (df , symbol = select_symbols .value , date = date_picker .value ,
55
+ expiry = expiry_select .value )
56
+ #q = query(df, symbol='ACC', date='2019-05-23', expiry='2019-05-30')
57
+ if (q is None ) or (len (q ) == 0 ):
58
+ return figure ()
59
+
60
+ x = [(str (int (a )), str (b )) for a ,b in zip (q .strike_pr , q .option_typ )]
61
+ source = ColumnDataSource (data = dict (x = x , values = q .open_int .values ))
62
+
63
+ TOOLTIPS = [
64
+ ('index' , '$index' ),
65
+ ('open_interest' , '@values' ),
66
+ ('option' , '@x' )
67
+ ]
68
+ p = figure (x_range = FactorRange (* x ),
69
+ title = "Open Interest" , tooltips = TOOLTIPS ,
70
+ tools = "pan,wheel_zoom,box_zoom,hover,reset" )
71
+ p .vbar (x = 'x' , top = 'values' , width = 0.9 , source = source ,
72
+ fill_color = factor_cmap ('x' , palette = ['red' , 'green' ],
73
+ factors = ['PE' , 'CE' ], start = 1 , end = 2 ))
74
+
75
+
76
+ def update ():
77
+ """
78
+ Update chart data
79
+ """
80
+ data = query (df , symbol = select_symbols .value , date = date_picker .value ,
81
+ expiry = expiry_select .value )
82
+ x_range = [(str (int (a )), str (b )) for a ,b in
83
+ zip (data .strike_pr , data .option_typ )]
84
+ src = ColumnDataSource ({'x' : x_range , 'values' : data .open_int .values })
85
+ source .data = src .data
86
+ #p1.x_range = FactorRange(*x)
87
+
25
88
26
89
27
90
# Create widgets
28
91
select_symbols = Select (title = 'Pick a symbol' , options = symbols )
29
92
expiry_select = Select (title = 'Select an expiry date' , options = None )
30
- date_picker = DatePicker (title = 'Select a date' )
93
+ date_picker = DatePicker (title = 'Select a date' ,
94
+ value = datetime .datetime .today ().date ())
31
95
button = Button (label = "Update" , button_type = "success" )
32
96
97
+ # Create charts
98
+ q = query (df , symbol = select_symbols .value , date = date_picker .value ,
99
+ expiry = expiry_select .value )
100
+ q = query (df , symbol = 'BANKBARODA' )
101
+ x = [(str (int (a )), str (b )) for a ,b in zip (q .strike_pr , q .option_typ )]
102
+ source = ColumnDataSource ({'x' : x , 'values' : q .open_int .values })
103
+ TOOLTIPS = [
104
+ ('index' , '$index' ),
105
+ ('open_interest' , '@values' ),
106
+ ('option' , '@x' )
107
+ ]
108
+ p1 = figure (x_range = FactorRange (),
109
+ title = "Open Interest" , tooltips = TOOLTIPS ,
110
+ tools = "pan,wheel_zoom,box_zoom,hover,reset" )
111
+ p1 .x_range = FactorRange (* x )
112
+
113
+ bar = p1 .vbar (x = 'x' , top = 'values' , width = 0.9 , source = source ,
114
+ fill_color = factor_cmap ('x' , palette = ['red' , 'green' ],
115
+ factors = ['PE' , 'CE' ], start = 1 , end = 2 ))
116
+
117
+
33
118
# Event handlers
34
119
select_symbols .on_change ('value' , update_expiry_date )
35
- date_picker .on_change ('value' , lambda x ,y ,z : print (z ))
120
+ #date_picker.on_change('value', lambda x,y,z: print(z))
121
+ button .on_click (update )
36
122
37
123
layout = column (
38
124
row (select_symbols , expiry_select ),
39
- row (date_picker , button )
125
+ row (date_picker , button ),
126
+ row (p1 )
40
127
)
41
128
42
129
# add the layout to curdoc
0 commit comments