def adapt_yranges(y_range, data_min, data_max=None): dmin = min(nanfilt(data_min), default=None) dmax = max(nanfilt(data_max if data_max is not None else data_min), default=None) if dmin is None or dmax is None: return diff = ((dmax - dmin) or dmin) * 0.1 dmin -= diff dmax += diff if y_range.start is not None: dmin = min(dmin, y_range.start) y_range.start = dmin if y_range.end is not None: dmax = max(dmax, y_range.end) y_range.end = dmax
def plot_volume(self, obj, alpha, extra_axis=False): src_prefix = sanitize_source_name(obj._name) df = convert_to_pandas(obj, self._start, self._end) if len(nanfilt(df.volume)) == 0: return colorup = convert_color(self._scheme.volup) colordown = convert_color(self._scheme.voldown) is_up = df.close > df.open colors = [colorup if x else colordown for x in is_up] self._add_to_cds(df.volume, src_prefix + 'volume') self._add_to_cds(colors, src_prefix + 'volume_colors') kwargs = { 'fill_alpha': alpha, 'line_alpha': alpha, 'name': 'Volume', 'legend': 'Volume' } ax_formatter = NumeralTickFormatter(format='0.000 a') if extra_axis: self.figure.extra_y_ranges = {'axvol': DataRange1d()} adapt_yranges(self.figure.extra_y_ranges['axvol'], df.volume) self.figure.extra_y_ranges['axvol'].end /= self._scheme.volscaling ax_color = colorup ax = LinearAxis(y_range_name="axvol", formatter=ax_formatter, axis_label_text_color=ax_color, axis_line_color=ax_color, major_label_text_color=ax_color, major_tick_line_color=ax_color, minor_tick_line_color=ax_color) self.figure.add_layout(ax, 'left') kwargs['y_range_name'] = "axvol" else: self.figure.yaxis.formatter = ax_formatter adapt_yranges(self.figure.y_range, df.volume) self.figure.y_range.end /= self._scheme.volscaling self.figure.vbar('datetime', get_bar_length_ms(obj) * 0.7, src_prefix + 'volume', 0, source=self._cds, fill_color=src_prefix + 'volume_colors', line_color="black", **kwargs) self._hoverc.add_hovertip("Volume", f"@{src_prefix}volume{{(0.00 a)}}")
def plot_volume(self, data: bt.feeds.DataBase, strat_clk: array, alpha, extra_axis=False): source_id = Figure._source_id(data) df = convert_to_pandas(strat_clk, data, self._start, self._end) if len(nanfilt(df.volume)) == 0: return colorup = convert_color(self._scheme.volup) colordown = convert_color(self._scheme.voldown) is_up = df.close > df.open colors = [colorup if x else colordown for x in is_up] self._add_to_cds(df.volume, f'{source_id}volume') self._add_to_cds(colors, f'{source_id}volume_colors') kwargs = {'fill_alpha': alpha, 'line_alpha': alpha, 'name': 'Volume', 'legend': 'Volume'} ax_formatter = NumeralTickFormatter(format=self._scheme.number_format) if extra_axis: self.figure.extra_y_ranges = {'axvol': DataRange1d()} adapt_yranges(self.figure.extra_y_ranges['axvol'], df.volume) self.figure.extra_y_ranges['axvol'].end /= self._scheme.volscaling ax_color = colorup ax = LinearAxis(y_range_name="axvol", formatter=ax_formatter, axis_label_text_color=ax_color, axis_line_color=ax_color, major_label_text_color=ax_color, major_tick_line_color=ax_color, minor_tick_line_color=ax_color) self.figure.add_layout(ax, 'left') kwargs['y_range_name'] = "axvol" else: self.figure.yaxis.formatter = ax_formatter adapt_yranges(self.figure.y_range, df.volume) self.figure.y_range.end /= self._scheme.volscaling self.figure.vbar('index', get_bar_width(), f'{source_id}volume', 0, source=self._cds, fill_color=f'{source_id}volume_colors', line_color="black", **kwargs) hover_target = None if self._scheme.merge_data_hovers else self.figure self._hoverc.add_hovertip("Volume", f"@{source_id}volume{{({self._scheme.number_format})}}", hover_target)
def adapt_yranges(y_range, data, padding_factor=200.0): nnan_data = nanfilt(data) dmin = min(nnan_data, default=None) dmax = max(nnan_data, default=None) if dmin is None or dmax is None: return diff = ((dmax - dmin) or dmin) * padding_factor dmin -= diff dmax += diff if y_range.start is not None: dmin = min(dmin, y_range.start) y_range.start = dmin if y_range.end is not None: dmax = max(dmax, y_range.end) y_range.end = dmax