MCPcopy Index your code
hub / github.com/kernc/backtesting.py / _plot_superimposed_ohlc

Function _plot_superimposed_ohlc

backtesting/_plotting.py:467–508  ·  view source on GitHub ↗

Superimposed, downsampled vbars

()

Source from the content-addressed store, hash-verified

465 return fig
466
467 def _plot_superimposed_ohlc():
468 """Superimposed, downsampled vbars"""
469 time_resolution = pd.DatetimeIndex(df['datetime']).resolution
470 resample_rule = (superimpose if isinstance(superimpose, str) else
471 dict(day='ME',
472 hour='D',
473 minute='h',
474 second='min',
475 millisecond='s').get(time_resolution))
476 if not resample_rule:
477 warnings.warn(
478 f"'Can't superimpose OHLC data with rule '{resample_rule}'"
479 f"(index datetime resolution: '{time_resolution}'). Skipping.",
480 stacklevel=4)
481 return
482
483 df2 = (df.assign(_width=1).set_index('datetime')
484 .resample(resample_rule, label='left')
485 .agg(dict(OHLCV_AGG, _width='count')))
486
487 # Check if resampling was downsampling; error on upsampling
488 orig_freq = _data_period(df['datetime'])
489 resample_freq = _data_period(df2.index)
490 if resample_freq < orig_freq:
491 raise ValueError('Invalid value for `superimpose`: Upsampling not supported.')
492 if resample_freq == orig_freq:
493 warnings.warn('Superimposed OHLC plot matches the original plot. Skipping.',
494 stacklevel=4)
495 return
496
497 df2.index = df2['_width'].cumsum().shift(1).fillna(0)
498 df2.index += df2['_width'] / 2 - .5
499 df2['_width'] -= .1 # Candles don't touch
500
501 df2['inc'] = (df2.Close >= df2.Open).astype(int).astype(str)
502 df2.index.name = None
503 source2 = ColumnDataSource(df2)
504 fig_ohlc.segment('index', 'High', 'index', 'Low', source=source2, color='#bbbbbb')
505 colors_lighter = [lightness(BEAR_COLOR, .92),
506 lightness(BULL_COLOR, .92)]
507 fig_ohlc.vbar('index', '_width', 'Open', 'Close', source=source2, line_color=None,
508 fill_color=factor_cmap('inc', colors_lighter, ['0', '1']))
509
510 def _plot_ohlc():
511 """Main OHLC bars"""

Callers 1

plotFunction · 0.85

Calls 2

_data_periodFunction · 0.90
lightnessFunction · 0.85

Tested by

no test coverage detected