def trend_chart(self, target_stock: str): """Trend chart of the result using Bokeh.""" result = self.__check_result(target_stock) tabs = [] for i in result.keys(): label_name = i tickers = result[i] cumprod = (self.daily_return[tickers] + 1).cumprod() source = ColumnDataSource(data=cumprod) p = figure(x_axis_type="datetime", title="Trend Line", plot_height=350, plot_width=800) p.xgrid.grid_line_color = None p.ygrid.grid_line_alpha = 0.5 p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Total Return' lines = [] for i in range(len(cumprod.columns)): lines.append( p.line("Date", cumprod.columns[i], source=source, line_width=2, line_alpha=0.8, line_color=Spectral10[i % 10], legend_label=cumprod.columns[i], muted_color=Spectral10[i % 10], muted_alpha=0.1)) p.legend.location = "top_left" p.legend.click_policy = "mute" LABELS = list(cumprod.columns) checkbox_group = CheckboxGroup(labels=LABELS) checkbox_group.active = list(range(len(LABELS))) code = """ for (var i = 0; i < lines.length; i++) { lines[i].visible = false; if (cb_obj.active.includes(i)){lines[i].visible = true;} } """ callback = CustomJS(code=code, args={'lines': lines}) checkbox_group.js_on_click(callback) layout = row(p, checkbox_group) tabs.append(Panel(child=layout, title=label_name)) tabs = Tabs(tabs=tabs) show(tabs)
d3['ann'].push(d2['ann'][i]) d3['noise'].push(d2['noise'][i]) d3['change'].push(0) } } } s3.change.emit(); s4.change.emit(); """) checkboxes = CheckboxGroup(name="Select beats to display",labels=['N','V','Q'], active=[0, 1,2],callback = callback3,height = 120) checkboxes.active = [] checkboxes.js_on_click(callback3) div = Div(text="""<b>Select annotations to change: </b>""", width=200, height=0) div2 = Div(text="""<b>Change annotations to : </b>""", width=200, height=0) div3 = Div(text="""<b>Select noise level : </b>""", width=200, height=0)
def trend_chart(self, policy_names: list, compounding: bool = False, height: int = 350, width: int = 800): """Trend chart of the result using Bokeh. Parameters ---------- policy_names : list List of selected policy names compounding : bool, default False Whether returns are reinvested back into the account. height : int Height of the plot width : int Width of the plot Returns ------- None """ selected_rst_dict = {key: self.rst_dict[key] for key in policy_names} data = pd.DataFrame(selected_rst_dict) data["strategy_return"] = self.strategy_return if compounding: cum = (data + 1).cumprod() else: cum = data.cumsum() + 1 if compounding: mdd = (cum / cum.cummax() - 1) else: mdd = cum - cum.cummax() source = ColumnDataSource(data=cum) source_mdd = ColumnDataSource(data=mdd) p = figure(x_axis_type="datetime", title="Trend Line", plot_height=height, plot_width=width) p.xgrid.grid_line_color = None p.ygrid.grid_line_alpha = 0.5 p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Total Return' p_mdd = figure(x_axis_type="datetime", title="Max Drawdown", plot_height=height, plot_width=width, x_range=p.x_range) p_mdd.xgrid.grid_line_color = None p_mdd.ygrid.grid_line_alpha = 0.5 p_mdd.xaxis.axis_label = 'Time' p_mdd.yaxis.axis_label = 'MDD' lines = [] for i in range(len(cum.columns)): lines.append( p.line("Date", cum.columns[i], source=source, line_width=2, line_alpha=0.8, line_color=Spectral10[i % 10], legend_label=cum.columns[i], muted_color=Spectral10[i % 10], muted_alpha=0.1)) lines_mdd = [] for i in range(len(mdd.columns)): lines_mdd.append( p_mdd.line("Date", mdd.columns[i], source=source_mdd, line_width=2, line_alpha=0.8, line_color=Spectral10[i % 10], legend_label=mdd.columns[i], muted_color=Spectral10[i % 10], muted_alpha=0.1)) p.legend.location = "top_left" p.legend.click_policy = "mute" p_mdd.legend.location = "bottom_left" p_mdd.legend.click_policy = "mute" LABELS = list(cum.columns) checkbox_group = CheckboxGroup(labels=LABELS) checkbox_group.active = list(range(len(LABELS))) code = """ for (var i = 0; i < lines.length; i++) { lines[i].visible = false; if (cb_obj.active.includes(i)){lines[i].visible = true;} } """ callback = CustomJS(code=code, args={'lines': lines}) checkbox_group.js_on_click(callback) callback = CustomJS(code=code, args={'lines': lines_mdd}) checkbox_group.js_on_click(callback) grid = gridplot([[p, checkbox_group], [p_mdd]]) show(grid)
def trend_chart(returns_series, compounding: bool = False, height: int = 350, width: int = 800): """Trend chart of the result using Bokeh. Parameters ---------- returns_series : Pandas Series Series contains returns. compounding : bool, default False Whether returns are reinvested back into the account. height : int Height of the plot width : int Width of the plot Returns ------- None """ data = returns_series if compounding: cum = (data + 1).cumprod() else: cum = data.cumsum() + 1 cum = pd.DataFrame(cum) source = ColumnDataSource(data=cum) p = figure(x_axis_type="datetime", title="Trend Line", plot_height=height, plot_width=width) p.xgrid.grid_line_color = None p.ygrid.grid_line_alpha = 0.5 p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Total Return' lines = [] for i in range(len(cum.columns)): lines.append( p.line("Date", cum.columns[i], source=source, line_width=2, line_alpha=0.8, line_color=Spectral10[i % 10], legend_label=cum.columns[i], muted_color=Spectral10[i % 10], muted_alpha=0.1)) p.legend.location = "top_left" p.legend.click_policy = "mute" LABELS = list(cum.columns) checkbox_group = CheckboxGroup(labels=LABELS) checkbox_group.active = list(range(len(LABELS))) code = """ for (var i = 0; i < lines.length; i++) { lines[i].visible = false; if (cb_obj.active.includes(i)){lines[i].visible = true;} } """ callback = CustomJS(code=code, args={'lines': lines}) checkbox_group.js_on_click(callback) layout = row(p, checkbox_group) show(layout)