Skip to content

Commit 69cd7b7

Browse files
committed
chart with factor axis added
1 parent 8de7ad3 commit 69cd7b7

File tree

1 file changed

+93
-6
lines changed

1 file changed

+93
-6
lines changed

oi_graph/oi.py

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import pandas as pd
33
import datetime
44

5-
from bokeh.io import curdoc
5+
from bokeh.io import curdoc, show, output_file
66
from bokeh.layouts import row, column
77
from bokeh.plotting import figure
8-
from bokeh.models import ColumnDataSource
8+
from bokeh.models import ColumnDataSource, FactorRange
99
from bokeh.models.widgets import Select, TextInput, DatePicker, Button
10+
from bokeh.transform import factor_cmap
1011

1112
# Load data, probably from a database. I am loading it directly from a file
1213
df = pd.read_hdf('derivatives_may_2019.h5')
@@ -17,26 +18,112 @@ def update_expiry_date(attrname, old, new):
1718
"""
1819
Update expiry date for this particular symbol
1920
"""
20-
print(attrname, old, new)
2121
q = 'symbol == "{}"'.format(new)
2222
dates = sorted(df.query(q).expiry_dt.unique())
2323
dates = [str(dt)[:10] for dt in dates]
2424
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+
2588

2689

2790
# Create widgets
2891
select_symbols = Select(title='Pick a symbol', options=symbols)
2992
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())
3195
button = Button(label="Update", button_type="success")
3296

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+
33118
# Event handlers
34119
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)
36122

37123
layout = column(
38124
row(select_symbols, expiry_select),
39-
row(date_picker, button)
125+
row(date_picker, button),
126+
row(p1)
40127
)
41128

42129
# add the layout to curdoc

0 commit comments

Comments
 (0)