mirror of
https://github.com/kernc/backtesting.py.git
synced 2024-01-28 15:29:30 +03:00
BUG: Fix iterable color= param for non-overlay indicators
This commit is contained in:
@@ -25,8 +25,7 @@ from bokeh.layouts import gridplot
|
||||
from bokeh.palettes import Category10
|
||||
from bokeh.transform import factor_cmap
|
||||
|
||||
from backtesting._util import _data_period
|
||||
|
||||
from backtesting._util import _data_period, _as_list
|
||||
|
||||
IS_JUPYTER_NOTEBOOK = 'JPY_PARENT_PID' in os.environ
|
||||
|
||||
@@ -425,12 +424,12 @@ return this.labels[index] || "";
|
||||
if not value._opts.get('plot') or _too_many_dims(value):
|
||||
continue
|
||||
|
||||
color = value._opts['color']
|
||||
tooltips = []
|
||||
|
||||
# Overlay indicators on the OHLC figure
|
||||
if value._opts['overlay']:
|
||||
color = color or next(ohlc_colors)
|
||||
color = value._opts['color']
|
||||
color = color and _as_list(color)[0] or next(ohlc_colors)
|
||||
legend = LegendStr(value.name)
|
||||
for i, arr in enumerate(value):
|
||||
source_name = '{}_{}'.format(value.name, i)
|
||||
@@ -442,7 +441,8 @@ return this.labels[index] || "";
|
||||
ohlc_tooltips.append((value.name, NBSP.join(tooltips)))
|
||||
else:
|
||||
# Standalone indicator sections at the bottom
|
||||
color = color or colorgen()
|
||||
color = value._opts['color']
|
||||
color = color and cycle(_as_list(color)) or colorgen()
|
||||
fig = new_indicator_figure()
|
||||
for i, arr in enumerate(value, 1):
|
||||
legend = '{}-{}'.format(value.name, i) if len(value) > 1 else value.name
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
from collections import Sequence
|
||||
from numbers import Number
|
||||
|
||||
import numpy as np
|
||||
@@ -18,6 +18,12 @@ def _as_str(value):
|
||||
return name
|
||||
|
||||
|
||||
def _as_list(value):
|
||||
if isinstance(value, Sequence) and not isinstance(value, str):
|
||||
return list(value)
|
||||
return [value]
|
||||
|
||||
|
||||
def _data_period(df):
|
||||
"""Return data index period as pd.Timedelta"""
|
||||
return df.index[:100].to_series().diff().median()
|
||||
|
||||
@@ -409,6 +409,22 @@ class TestPlot(TestCase):
|
||||
# Give browser time to open before tempfile is removed
|
||||
time.sleep(5)
|
||||
|
||||
def test_indicator_color(self):
|
||||
class S(Strategy):
|
||||
def init(self):
|
||||
a = self.I(SMA, self.data.Close, 5, overlay=True, color='red')
|
||||
b = self.I(SMA, self.data.Close, 10, overlay=False, color='blue')
|
||||
self.I(lambda: (a, b), overlay=False, color=('green', 'orange'))
|
||||
def next(self):
|
||||
pass
|
||||
|
||||
bt = Backtest(GOOG, S)
|
||||
bt.run()
|
||||
with _tempfile() as f:
|
||||
bt.plot(filename=f,
|
||||
plot_drawdown=False, plot_equity=False, plot_pl=False, plot_volume=False,
|
||||
open_browser=False)
|
||||
|
||||
|
||||
class TestLib(TestCase):
|
||||
def test_barssince(self):
|
||||
|
||||
Reference in New Issue
Block a user