Esempio n. 1
0
    def _plot_superimposed_ohlc():
        """Superimposed, downsampled vbars"""
        resample_rule = (superimpose if isinstance(superimpose, str) else dict(
            day='W', hour='D', minute='H', second='T',
            millisecond='S').get(time_resolution))
        if not resample_rule:
            warnings.warn(
                "'Can't superimpose OHLC data with rule '{}' (index datetime resolution: '{}'). "
                "Skipping.".format(resample_rule, time_resolution),
                stacklevel=4)
            return

        orig_df['_width'] = 1
        from .lib import OHLCV_AGG
        df2 = orig_df.resample(resample_rule, label='left').agg(
            dict(OHLCV_AGG, _width='count'))

        # Check if resampling was downsampling; error on upsampling
        orig_freq = _data_period(orig_df)
        resample_freq = _data_period(df2)
        if resample_freq < orig_freq:
            raise ValueError(
                'Invalid value for `superimpose`: Upsampling not supported.')
        if resample_freq == orig_freq:
            warnings.warn(
                'Superimposed OHLC plot matches the original plot. Skipping.',
                stacklevel=4)
            return

        if omit_missing:
            width2 = '_width'
            df2.index = df2['_width'].cumsum().shift(1).fillna(0)
            df2.index += df2['_width'] / 2 - .5
            df2['_width'] -= .1  # Candles don't touch
        else:
            del df['_width']
            width2 = dict(day=86400 * 5, hour=86400, minute=3600,
                          second=60)[time_resolution] * 1000
            df2.index += pd.Timedelta(
                width2 / 2 +
                (width2 /
                 5 if resample_rule == 'W' else 0),  # Sunday week start
                unit='ms')
        df2['inc'] = (df2.Close >= df2.Open).astype(np.uint8).astype(str)
        df2.index.name = None
        source2 = ColumnDataSource(df2)
        fig_ohlc.segment('index',
                         'High',
                         'index',
                         'Low',
                         source=source2,
                         color='#bbbbbb')
        fig_ohlc.vbar('index',
                      width2,
                      'Open',
                      'Close',
                      source=source2,
                      line_color=None,
                      fill_color=factor_cmap('inc', COLORS_LIGHT, ['0', '1']))
Esempio n. 2
0
    def _plot_superimposed_ohlc():
        """Superimposed, downsampled vbars"""
        time_resolution = pd.DatetimeIndex(df['datetime']).resolution
        resample_rule = (superimpose if isinstance(superimpose, str) else dict(
            day='M', hour='D', minute='H', second='T',
            millisecond='S').get(time_resolution))
        if not resample_rule:
            warnings.warn(
                f"'Can't superimpose OHLC data with rule '{resample_rule}'"
                f"(index datetime resolution: '{time_resolution}'). Skipping.",
                stacklevel=4,
            )
            return

        df2 = df.assign(_width=1).set_index('datetime').resample(
            resample_rule, label='left').agg(dict(OHLCV_AGG, _width='count'))

        # Check if resampling was downsampling; error on upsampling
        orig_freq = _data_period(df['datetime'])
        resample_freq = _data_period(df2.index)
        if resample_freq < orig_freq:
            raise ValueError(
                'Invalid value for `superimpose`: Upsampling not supported.')
        if resample_freq == orig_freq:
            warnings.warn(
                'Superimposed OHLC plot matches the original plot. Skipping.',
                stacklevel=4,
            )
            return

        df2.index = df2['_width'].cumsum().shift(1).fillna(0)
        df2.index += df2['_width'] / 2 - 0.5
        df2['_width'] -= 0.1  # Candles don't touch

        df2['inc'] = (df2.Close >= df2.Open).astype(int).astype(str)
        df2.index.name = None
        source2 = ColumnDataSource(df2)
        fig_ohlc.segment('index',
                         'High',
                         'index',
                         'Low',
                         source=source2,
                         color='#bbbbbb')
        colors_lighter = [
            lightness(BEAR_COLOR, 0.92),
            lightness(BULL_COLOR, 0.92)
        ]
        fig_ohlc.vbar(
            'index',
            '_width',
            'Open',
            'Close',
            source=source2,
            line_color=None,
            fill_color=factor_cmap('inc', colors_lighter, ['0', '1']),
        )
Esempio n. 3
0
 def _round_timedelta(value, _period=_data_period(df)):
     if not isinstance(value, pd.Timedelta):
         return value
     resolution = getattr(_period, 'resolution_string',
                          None) or _period.resolution
     return value.ceil(resolution)