Skip to content

Commit d79adf2

Browse files
author
goblincomet
committed
REF: Darken trades indicator lines on OHLC plot
1 parent f68a93f commit d79adf2

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

backtesting/_plotting.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import numpy as np
77
import pandas as pd
88

9+
from bokeh.colors.named import (
10+
lime as BULL_COLOR,
11+
tomato as BEAR_COLOR
12+
)
913
from bokeh.plotting import figure as _figure
1014
from bokeh.models import (
1115
CustomJS,
@@ -59,6 +63,12 @@ def colorgen():
5963
yield from cycle(Category10[10])
6064

6165

66+
def lightness(color, lightness=.94):
67+
color = color.to_hsl()
68+
color.l = lightness
69+
return color.to_rgb()
70+
71+
6272
def plot(*, results, df, indicators, filename='', plot_width=1200,
6373
plot_equity=True, plot_pl=True,
6474
plot_volume=True, plot_drawdown=False,
@@ -73,8 +83,7 @@ def plot(*, results, df, indicators, filename='', plot_width=1200,
7383
# TestPlot.test_file_size() test was failing).
7484
_bokeh_reset(filename)
7585

76-
COLORS = ['tomato', 'lime']
77-
COLORS_LIGHT = ['#ffe3e3', '#e3ffe3']
86+
COLORS = [BEAR_COLOR, BULL_COLOR]
7887

7988
orig_trade_data = trade_data = results._trade_data.copy(False)
8089

@@ -134,6 +143,9 @@ def plot(*, results, df, indicators, filename='', plot_width=1200,
134143

135144
inc_cmap = factor_cmap('inc', COLORS, ['0', '1'])
136145
cmap = factor_cmap('returns_pos', COLORS, ['0', '1'])
146+
colors_darker = [lightness(BEAR_COLOR, .35),
147+
lightness(BULL_COLOR, .35)]
148+
trades_cmap = factor_cmap('returns_pos', colors_darker, ['0', '1'])
137149

138150
if is_datetime_index and omit_missing:
139151
fig_ohlc.xaxis.formatter = FuncTickFormatter(
@@ -368,8 +380,10 @@ def _plot_superimposed_ohlc():
368380
df2.index.name = None
369381
source2 = ColumnDataSource(df2)
370382
fig_ohlc.segment('index', 'High', 'index', 'Low', source=source2, color='#bbbbbb')
383+
colors_lighter = [lightness(BEAR_COLOR, .92),
384+
lightness(BULL_COLOR, .92)]
371385
fig_ohlc.vbar('index', width2, 'Open', 'Close', source=source2, line_color=None,
372-
fill_color=factor_cmap('inc', COLORS_LIGHT, ['0', '1']))
386+
fill_color=factor_cmap('inc', colors_lighter, ['0', '1']))
373387

374388
def _plot_ohlc():
375389
"""Main OHLC bars"""
@@ -378,16 +392,16 @@ def _plot_ohlc():
378392
line_color="black", fill_color=inc_cmap)
379393
return r
380394

381-
def _plot_ohlc_orders():
382-
"""Order entry / exit markers on OHLC plot"""
395+
def _plot_ohlc_trades():
396+
"""Trade entry / exit markers on OHLC plot"""
383397
exit_price = trade_data['Exit Price'].dropna()
384398
entry_price = trade_data['Entry Price'].dropna().iloc[:exit_price.size] # entry can be one more at the end # noqa: E501
385399
trade_source.add(np.column_stack((entry_price.index, exit_price.index)).tolist(),
386400
'position_lines_xs')
387401
trade_source.add(np.column_stack((entry_price, exit_price)).tolist(),
388402
'position_lines_ys')
389403
fig_ohlc.multi_line(xs='position_lines_xs', ys='position_lines_ys',
390-
source=trade_source, line_color=cmap,
404+
source=trade_source, line_color=trades_cmap,
391405
legend='Trades',
392406
line_width=8, line_alpha=1, line_dash='dotted')
393407

@@ -501,7 +515,7 @@ def __eq__(self, other):
501515
_plot_superimposed_ohlc()
502516

503517
ohlc_bars = _plot_ohlc()
504-
_plot_ohlc_orders()
518+
_plot_ohlc_trades()
505519
_plot_indicators()
506520

507521
set_tooltips(fig_ohlc, ohlc_tooltips, vline=True, renderers=[ohlc_bars])

0 commit comments

Comments
 (0)