def __init__(self, config: configparser.ConfigParser, app: dash.Dash): self.app = app self.config = config self.db_type = self.config['App']['DbType'] self.db_path = self.config.get('App', 'DbPath', vars=os.environ) if self.db_type == 'mysql': self.engine = sqlalchemy.create_engine(self.db_path, echo=False) elif self.db_type == 'sqllite': self.engine = sqlalchemy.create_engine( self.db_path, echo=False, poolclass=StaticPool, connect_args={"check_same_thread": False}) else: raise str("No db.type specified") self.timeframe_list = utils.get_timeframe_list() self.expiry_list = utils.get_expiry_list() self.historical_expires, self.future_expires = utils.get_historical_future_expires( ) self.symbol_list = utils.get_symbol_list() self.selected_symbol = self.symbol_list[1] self.strike_list = utils.get_strike_list(self.selected_symbol) self.default_strike = {'NIFTY': 14300, 'BANKNIFTY': 32000} self.layout = None self.register_callbacks(app)
def update_strike_dropdown(symbol): print('update_strike_dropdown') global selected_symbol, strike_list selected_symbol = symbol strike_list = utils.get_strike_list(selected_symbol) options = [{'label': opt, 'value': opt} for opt in strike_list] return [options, self.default_strike[selected_symbol]]
def update_option_chain(n_clicks, n_intervals, symbol, expiry, check_list_values): self.app.title = '{} - Chain'.format(symbol) # print('Refreshing: '.format(n_intervals)) self.selected_symbol = symbol self.strike_list = utils.get_strike_list(symbol) # option_oi_data = oi.get_option_oi_buildup_range(symbol, expiry, strike, timeframe, 100) start = timeit.default_timer() df = self.get_option_chain(symbol, expiry) # df.to_csv('/mnt/hgfs/win-d/work/stock_market/data_viewer/apps/test_data/option_chain.csv') # df = pd.read_csv('/mnt/hgfs/win-d/work/stock_market/data_viewer/apps/test_data/option_chain.csv') stop = timeit.default_timer() execution_time = stop - start print( "Time taken to build option chain: {}".format(execution_time)) # return df.to_dict('records'), style_data_conditional option_chain = df[[ 'CE_OI', 'CE_OI_CHG', 'CE_LP', 'strike', 'PE_LP', 'PE_OI_CHG', 'PE_OI' ]] # option_chain = df[['CE_OI', 'CE_OI_CHG', 'CE_IV', 'CE_vol', 'CE_LP', # 'strike', # 'PE_LP', 'PE_vol', 'PE_IV', 'PE_OI_CHG', 'PE_OI']] table = dbc.Table.from_dataframe(option_chain, striped=True, bordered=True, hover=True) def color_scale(df, column_name, table, col_idx): # table.children[1].children[0].children[0].style = {'background-color': '#bde9ba'} scale = gradient_color(df, column_name) for idx, color in enumerate(scale): table.children[1].children[idx].children[col_idx].style = { 'background-color': color } color_scale(option_chain, 'CE_OI_CHG', table, 1) color_scale(option_chain, 'PE_OI_CHG', table, 5) # color_scale(option_chain, 'PE_OI_CHG', table, 1) def set_column_bars(df, table, column_idx, left_to_right=True): normalized_df = (df - df.min()) / (df.max() - df.min()) for idx, size in enumerate(normalized_df): table.children[1].children[idx].children[ column_idx].style = { 'width': 200, 'background-image': 'linear-gradient(#0091ea,#0091ea)', 'background-repeat': 'no-repeat', 'background-size': '{}%'.format(size * 100), } if left_to_right is False: table.children[1].children[idx].children[ column_idx].style[ 'background-position'] = '100% 100%' set_column_bars(option_chain['CE_OI'], table, 0) set_column_bars(option_chain['PE_OI'], table, 6, False) # add checkbox selected_strikes = get_selected_strikes(check_list_values) table.children[0].children.children.insert( 0, html.Th(style={'width': 40})) table.children[0].children.children.append( html.Th(style={'width': 40})) for idx, row in enumerate(table.children[1].children): if idx == (int(len(table.children[1].children) / 2) - 16): row.id = 'table-center-row' ce_option_value = "{}-{}".format( option_chain.iloc[idx]['strike'].astype(int), 'CE') ce_value = [ce_option_value ] if ce_option_value in selected_strikes else [] pe_option_value = "{}-{}".format( option_chain.iloc[idx]['strike'].astype(int), 'PE') pe_value = [pe_option_value ] if pe_option_value in selected_strikes else [] row.children.insert( 0, html.Td( dcc.Checklist( id={ 'type': 'strike', 'index': idx }, options=[{ "label": "", "value": ce_option_value }], value=ce_value, ))) row.children.append( html.Td( dcc.Checklist( id={ 'type': 'strike', 'index': idx }, options=[{ "label": "", "value": pe_option_value }], value=pe_value, ))) # option chain divider color for row_idx in range(len(df)): for col_idx in range(len(table.children[0].children.children)): if col_idx in [0, 1, 3, 4, 5] and df.iloc[row_idx][ 'CE_underlyingValue'] > df.iloc[row_idx]['strike']: if hasattr( table.children[1].children[row_idx]. children[col_idx], 'style'): table.children[1].children[row_idx].children[ col_idx].style['background-color'] = '#ffecb3' else: table.children[1].children[row_idx].children[ col_idx].style = { 'background-color': '#ffecb3' } elif col_idx in [7, 8, 9, 11, 12] and df.iloc[row_idx][ 'CE_underlyingValue'] < df.iloc[row_idx]['strike']: if hasattr( table.children[1].children[row_idx]. children[col_idx], 'style'): table.children[1].children[row_idx].children[ col_idx].style['background-color'] = '#ffecb3' else: table.children[1].children[row_idx].children[ col_idx].style = { 'background-color': '#ffecb3' } table_row = [table] return table_row, 'Last update time: {}'.format( time.strftime("%I:%M:%S %p"))