def add_rsi_series(iw, ws, dates, ta, parent): values = ta.rsi(iw, ws) dpg.add_line_series(dates, values, label='RSI', parent=parent) dpg.add_button(label="Delete Series", user_data=dpg.last_item(), parent=dpg.last_item(), callback=lambda s, a, u: dpg.delete_item(u))
def submit(self, parent): dpg.add_button(label=self.label, parent=parent, width=-1) dpg.set_item_theme(dpg.last_item(), _source_theme) with dpg.drag_payload(parent=dpg.last_item(), drag_data=(self, self._generator, self._data)): dpg.add_text(f"Name: {self.label}")
def add_ema_series(n, dates, parent, ta): ema_dates = dates[n - 1:] ema_values = ta.ema(n).to_list()[n - 1:] dpg.add_line_series(ema_dates, ema_values, label='EMA-' + str(n), parent=parent) dpg.add_button(label="Delete Series", user_data=dpg.last_item(), parent=dpg.last_item(), callback=lambda s, a, u: dpg.delete_item(u))
def sma(): with dpg.child(width=200, height=70): item = dpg.add_button(label='SMA') dpg.set_item_theme(dpg.last_item(), button_themes[5]) dpg.add_same_line() i = dpg.add_input_int(default_value=3, label='', width=70) dpg.add_button(label='SMA200') dpg.set_item_theme(dpg.last_item(), button_themes[4]) with dpg.tooltip(dpg.last_item()): dpg.add_text('Media Movel de 200 dias') with dpg.drag_payload(parent=dpg.last_item(), drag_data=('SMA', 200), payload_type='candle_plot'): dpg.add_text('SMA200') dpg.add_same_line() dpg.add_button(label='SMA50') dpg.set_item_theme(dpg.last_item(), button_themes[3]) with dpg.drag_payload(parent=dpg.last_item(), drag_data=('SMA', 50), payload_type='candle_plot'): dpg.add_text('SMA50') dpg.add_same_line() dpg.add_button(label='SMA7') dpg.set_item_theme(dpg.last_item(), button_themes[2]) with dpg.drag_payload(parent=dpg.last_item(), drag_data=('SMA', 7), payload_type='candle_plot'): dpg.add_text('SMA7') with dpg.drag_payload(parent=item, drag_data=('SMA', dpg.get_value(i)), payload_type='candle_plot'): dpg.add_text('SMA')
def _candle_plot_drop(sender, app_data, user_data): global prices global window_data if window_data.selected_ticker == '': return ta = prices[window_data.selected_ticker]['ta'] dates = (ta.data['date'].astype('int64') // 1e9).to_list() # indicator comes in app_data logger.log('adding indicator ' + app_data[0]) if app_data[0] == 'RLINES': current_price = float(exchange.symbol_ticker(window_data.selected_ticker)[window_data.selected_ticker].current) threshold = dpg.get_value(app_data[1]) / 100 min_strength = dpg.get_value(app_data[2]) resistance_lines = ta.resistance_lines_for_price(current_price, min_strength, threshold) resistance_levels = [v[0] for v in resistance_lines] resistance_strengths = [v[1] for v in resistance_lines] resistance_lines = ta.support_lines_for_price(current_price, min_strength, threshold) resistance_levels += [v[0] for v in resistance_lines] resistance_strengths += [v[1] for v in resistance_lines] logger.log(str(resistance_levels)) hlid = dpg.add_hline_series(resistance_levels, label='RLINES', parent=window_data.candle_yaxis) def delete_hl_series(s, a, u): dpg.delete_item(u) for an in window_data.rlines_annotation_ids[u]: dpg.delete_item(an) window_data.rlines_annotation_ids[u] = [] dpg.add_button(label="Delete", user_data=dpg.last_item(), parent=dpg.last_item(), callback=delete_hl_series) window_data.rlines_annotation_ids[hlid] = [] # add annotations for ri in range(len(resistance_levels)): dpg.add_plot_annotation(label=str(resistance_strengths[ri]), default_value=(dates[-1], resistance_levels[ri]), offset=(10, 0), clamped=False, parent=window_data.candle_plot) window_data.rlines_annotation_ids[hlid].append(dpg.last_item()) elif app_data[0] == 'SMA': indicators.add_sma_series(app_data[1], dates, window_data.candle_yaxis, ta) elif app_data[0] == 'EMA': indicators.add_ema_series(app_data[1], dates, window_data.candle_yaxis, ta) elif app_data[0] == 'BOLLINGER_BANDS': indicators.add_bbands_series(app_data[1], app_data[2], dates, ta, window_data.candle_yaxis) dpg.add_button(label="Delete", user_data=dpg.last_item(), parent=dpg.last_item(), callback=lambda s, a, u: dpg.delete_item(u))
def rlines(): with dpg.child(width=200, height=90): item = dpg.add_button(label='RLINES') dpg.set_item_theme(dpg.last_item(), button_themes[5]) with dpg.tooltip(dpg.last_item()): dpg.add_text('Linhas de Resistencia') s = dpg.add_slider_float(label='%', default_value=2, min_value=0.1, max_value=5) i = dpg.add_input_int(default_value=0, label='') with dpg.drag_payload(parent=item, drag_data=('RLINES', s, i), payload_type='candle_plot'): dpg.add_text('RLINES')
def _ticker_list(parent_id): # dpg.add_text("TICKER LIST") with dpg.filter_set(parent=parent_id) as filter_id: filter_container = dpg.last_container() for ticker in tickers: with dpg.group(filter_key=ticker, horizontal=True): item = dpg.last_item() dpg.add_button(label=ticker, user_data=ticker, callback=lambda s, a, u: logger.log_info(u)) dpg.set_item_theme(dpg.last_item(), button_themes[2]) tick = exchange.symbol_ticker(ticker)[ticker] dpg.add_text("%.2f %s" % (float(tick.current), tick.currency)) with dpg.drag_payload(parent=item, drag_data=ticker, payload_type='main_window'): dpg.add_text('asdfasf') dpg.add_input_text(parent=parent_id, label="", before=filter_container, user_data=filter_container, callback=lambda s, a, u: dpg.set_value(u, a))
def add_bbands_series(w, s, dates, ta, parent): values = ta.bollinger_bands(w, s) bbdates = dates[w - 1:] upper_band = values[1].to_list()[w - 1:] lower_band = values[2].to_list()[w - 1:] with dpg.theme() as b_theme: dpg.add_theme_color(dpg.mvPlotCol_Fill, (100, 100, 150, 64), category=dpg.mvThemeCat_Plots) dpg.add_shade_series(bbdates, upper_band, y2=lower_band, label="BBANDS", parent=parent) dpg.set_item_theme(dpg.last_item(), b_theme) dpg.add_button(label="Delete Series", user_data=dpg.last_item(), parent=dpg.last_item(), callback=lambda s, a, u: dpg.delete_item(u))
def _add_inputs_and_outputs(obj, layout: {}): for attr in layout['inputs']: with dpg.node_attribute(label=attr[0], attribute_type=dpg.mvNode_Attr_Input): setattr(obj, attr[0] + '_id', dpg.last_item()) dpg.add_text(attr[1]) for attr in layout['outputs']: with dpg.node_attribute(label=attr[0], attribute_type=dpg.mvNode_Attr_Output): dpg.add_text(attr[1])
def rsi(): with dpg.child(width=200, height=90): item = dpg.add_button(label='RSI') dpg.set_item_theme(dpg.last_item(), button_themes[5]) i = dpg.add_slider_int(default_value=14, label='', min_value=3, max_value=200) j = dpg.add_slider_float(default_value=24, label='', min_value=3, max_value=200) with dpg.drag_payload(parent=item, drag_data=('RSI', i, j), payload_type='volume_plot'): dpg.add_text('RSI')
def bbands(): with dpg.child(width=200, height=95): item = dpg.add_button(label='BOLLINGER BANDS') dpg.set_item_theme(dpg.last_item(), button_themes[5]) i = dpg.add_slider_int(default_value=10, label='', min_value=3, max_value=200) j = dpg.add_slider_float(default_value=5, label='', min_value=1, max_value=5) with dpg.drag_payload(parent=item, drag_data=('BOLLINGER_BANDS', dpg.get_value(i), dpg.get_value(j)), payload_type='candle_plot'): dpg.add_text('BOLLINGER BANDS')
def update_internal(self) -> bool: # remove plot if self.plot_id != 0: dpg.delete_item(self.plot_id) self.plot_id = 0 if self.dealer is None or self.symbols is None: return True start_date = datetime(year=2021, month=6, day=29) end_date = datetime(year=2021, month=6, day=30) if self.period == '1y': start_date = end_date - timedelta(days=365) elif self.period == '6m': start_date = end_date - timedelta(days=182) elif self.period == '1m': start_date = end_date - timedelta(days=30) elif self.period == '1w': start_date = end_date - timedelta(days=7) elif self.period == '1d': start_date = end_date - timedelta(days=1) if self.period == '1y' or self.period == '6m' or self.period == '1m': if self.interval == '15m' or self.interval == '30m': print('changing interval to 1h') self.interval = '1h' self.prices = self.dealer.binance.historical_candle_series( self.symbols.symbols, start_date, end_date, self.interval) # add plot if needed if self.plot_id == 0: data = self.prices[ self.symbols.symbols[0]]['ta'].data['close'].to_list() self.plot_id = dpg.add_node_attribute( parent=self.node_id, attribute_type=dpg.mvNode_Attr_Static) dpg.add_simple_plot(label='', default_value=data, parent=dpg.last_item(), width=110, height=70) return True
def update_internal(self) -> bool: # remove plot if self.plot_id != 0: dpg.delete_item(self.plot_id) self.plot_id = 0 if self.series is None or not self.series.good(): return True if len(self.series.prices) == 0: return True plot_symbol = '' w = 0 for s in self.series.symbols.symbols: plot_symbol = s w = min([len(self.series.prices[s]['ta'].data), self.window_size]) if self.mean_type == 'SMA': self.data[s] = self.series.prices[s]['ta'].sma(w) elif self.mean_type == 'EMA': self.data[s] = self.series.prices[s]['ta'].ema(w) if w == 0 or len(plot_symbol) == 0: return True # add plot if needed if self.plot_id == 0: self.plot_id = dpg.add_node_attribute( parent=self.node_id, attribute_type=dpg.mvNode_Attr_Static) dpg.add_simple_plot( label='', default_value=self.data[plot_symbol].to_list()[w - 1:], parent=dpg.last_item(), width=110, height=70) return True
dpg.add_menu_item(label='dealer', callback=lambda: nodes.add_dealer(node_editor_id, exchange)) dpg.add_menu_item(label='current price', callback=lambda: nodes.add_current_price(node_editor_id)) dpg.add_menu_item(label='symbol', callback=lambda: nodes.add_symbol(node_editor_id)) dpg.add_menu_item(label='historical period', callback=lambda: nodes.add_history(node_editor_id)) dpg.add_menu_item(label='rolling mean', callback=lambda: nodes.add_rolling_mean(node_editor_id)) dpg.add_menu_item(label='distance', callback=lambda: nodes.add_distance(node_editor_id)) dpg.add_menu_item(label='rlines', callback=lambda: nodes.add_rlines(node_editor_id)) nodes.add_symbol(node_editor_id) nodes.add_current_price(node_editor_id) nodes.add_history(node_editor_id) nodes.add_rlines(node_editor_id) with dpg.window(label='Main', no_title_bar=True): core.set_primary_window(dpg.last_item(), True) with dpg.menu_bar(): with dpg.menu(label="File"): dpg.add_menu_item(label="show dpg demo", callback=lambda: show_demo()) dpg.add_menu_item(label="show dpg doc", callback=lambda: dpg.show_documentation()) with dpg.group(): main_parent = dpg.last_item() # menu bar # main with dpg.group(horizontal=True): with dpg.child(label='column_0', width=150, height=500): _candle_indicators() with dpg.child(label='column_1', width=700, height=500, payload_type='main_window', drop_callback=_ticker_drop): window_data.main_window_title = dpg.add_text('Ticker: ') # dpg.add_button(label='Period')
def setUp(self): dpg.create_context() with dpg.window() as self.window_id: def testy(sender, app, user): print(f"Sender: {dpg.get_item_type(sender)} {sender}, App Data: {app}, User Data:{user}") # Menus with dpg.menu_bar() as menu_bar: dpg.add_menu_item(label="menu item", payload_type="str", drop_callback=testy) with dpg.menu(label="menu", payload_type="str", drop_callback=testy): dpg.add_menu_item(label="menu item") # basic with dpg.collapsing_header(label="basic") as basic: dpg.add_image(dpg.mvFontAtlas) dpg.add_image_button(dpg.mvFontAtlas) dpg.add_text("this is a text widget") dpg.add_checkbox(label="checkbox") dpg.add_button(label="button") dpg.add_input_float(label="input float") dpg.add_input_floatx(label="input floatx") dpg.add_drag_int(label="drag int") dpg.add_drag_intx(label="drag intx") dpg.add_input_text(label="input text") dpg.add_slider_float(label="slider float") dpg.add_slider_floatx(label="slider floatx") dpg.add_listbox(label="listbox") dpg.add_selectable(label="selectable") dpg.add_radio_button(["item 1", "item 2"],label="radio button") # color with dpg.collapsing_header(label="color") as color: with dpg.group() as color: dpg.add_color_button([255,0,0,255]) dpg.add_color_edit([255,0,0,255]) dpg.add_colormap_button(label="Colormap Button 1") dpg.add_color_picker((255, 0, 255, 255), label="Color Picker", width=200) dpg.add_colormap_slider(label="Colormap Slider 1", default_value=0.5, payload_type="str", drop_callback=testy) dpg.add_colormap_scale(label="Colormap Spectral", min_scale=-100, max_scale=150, payload_type="str", drop_callback=testy) # containers with dpg.collapsing_header(label="containers"): with dpg.group() as containers: with dpg.collapsing_header(): btn = dpg.add_button() with dpg.group(width=150): dpg.add_button() with dpg.tree_node(): dpg.add_button() with dpg.child_window(width=150, height=100, payload_type="str", drop_callback=testy): pass # tab stuff with dpg.collapsing_header(label="tab bars"): with dpg.tab_bar(): with dpg.tab(label="tab", payload_type="str", drop_callback=testy): pass dpg.add_tab_button(label="tab button", payload_type="str", drop_callback=testy, drag_callback=testy) with dpg.drag_payload(parent=dpg.last_item(), drop_data="dropped", drag_data="dragged", user_data="user data", payload_type="str"): dpg.add_text(dpg.get_item_type(dpg.last_item())) dpg.add_text(f"Item ID: {dpg.last_item()}") # custom with dpg.collapsing_header(label="custom"): with dpg.group() as custom: dpg.add_date_picker() dpg.add_knob_float() dpg.add_3d_slider() dpg.add_time_picker() dpg.add_loading_indicator(payload_type="str", drop_callback=testy) # misc with dpg.collapsing_header(label="misc"): with dpg.group() as misc: dpg.add_progress_bar(label="progress bar", default_value=.5) # node with dpg.collapsing_header(label="node"): with dpg.node_editor() as node: with dpg.node(pos=[20,20], draggable=False): pass with dpg.node(pos=[100,100], draggable=False): pass # plots with dpg.collapsing_header(label="plot") as plot: with dpg.plot(): dpg.add_plot_legend(payload_type="str", drop_callback=testy) dpg.add_plot_axis(dpg.mvXAxis, label="x", payload_type="str", drop_callback=testy) with dpg.plot_axis(dpg.mvYAxis, label="y", payload_type="str", drop_callback=testy): dpg.add_line_series([0,1,2,3,4,5], [0,1,2,3,4,5], label="data") self.test_bind_items = dpg.get_item_children(basic, slot=1) self.test_bind_items += dpg.get_item_children(color, slot=1) self.test_bind_items += dpg.get_item_children(containers, slot=1) self.test_bind_items += dpg.get_item_children(custom, slot=1) self.test_bind_items += dpg.get_item_children(misc, slot=1) self.test_bind_items += dpg.get_item_children(node, slot=1) self.test_bind_items += dpg.get_item_children(plot, slot=1) dpg.setup_dearpygui()
last_command = {} if __name__ == '__main__': with dpg.window(label="Test"): for name, deltas in all_deltas.items(): # create plot with dpg.plot(label=f"{name} deltas", height=200, width=900): # optionally create legend dpg.add_plot_legend() # REQUIRED: create x and y axes dpg.add_plot_axis(dpg.mvXAxis, label="x") dpg.set_axis_limits(dpg.last_item(), 0, len(x_coords)) dpg.add_plot_axis(dpg.mvYAxis, label="y") dpg.set_axis_limits(dpg.last_item(), -1, 1) # series belong to a y axis dpg.add_line_series(x_coords[:len(deltas)], deltas, label=f'{name}', parent=dpg.last_item(), id=f'{name}_deltas') log_lock = RLock() def cb_state(state_msg): with log_lock: for name, velocity in zip(state_msg.name, state_msg.velocity): if name in last_command and name in all_deltas: delta = last_command[name] - velocity all_deltas[name].append(delta)
def cb_add_3d_coords(): with dpg.table_row(parent=ID.table1): for n in dpg.get_value(ID.values3): dpg.add_text(label=str(n)) with dpg.theme(tag='__round'): with dpg.theme_component(dpg.mvButton): dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 10) dpg.add_theme_style(dpg.mvStyleVar_FramePadding, 30, 30) with dpg.window(label='basic dpg app', tag='main'): with dpg.group(horizontal=False): dpg.add_3d_slider(tag=ID.values3, scale=.3, callback=lambda: print(dpg.get_value(ID.values3))) dpg.add_button(label='add coords', callback=cb_add_3d_coords) dpg.bind_item_theme(dpg.last_item(), '__round') with dpg.table(tag=ID.table1, source='src'): dpg.add_table_column(label='x') dpg.add_table_column(label='y') dpg.add_table_column(label='z') dpg.set_primary_window('main', True) dpg.show_viewport() dpg.start_dearpygui() dpg.destroy_context()