|
40 | 40 | from bokeh.palettes import Category10
|
41 | 41 | from bokeh.transform import factor_cmap
|
42 | 42 |
|
43 |
| -from backtesting._util import _data_period, _as_list, _Indicator |
| 43 | +from backtesting._util import _data_period, _as_list, _Indicator, try_ |
44 | 44 |
|
45 | 45 | with open(os.path.join(os.path.dirname(__file__), 'autoscale_cb.js'),
|
46 | 46 | encoding='utf-8') as _f:
|
@@ -128,8 +128,15 @@ def _maybe_resample_data(resample_rule, df, indicators, equity_data, trades):
|
128 | 128 | from .lib import OHLCV_AGG, TRADES_AGG, _EQUITY_AGG
|
129 | 129 | df = df.resample(freq, label='right').agg(OHLCV_AGG).dropna()
|
130 | 130 |
|
131 |
| - indicators = [_Indicator(i.df.resample(freq, label='right').mean() |
132 |
| - .dropna().reindex(df.index).values.T, |
| 131 | + def try_mean_first(indicator): |
| 132 | + nonlocal freq |
| 133 | + resampled = indicator.df.fillna(np.nan).resample(freq, label='right') |
| 134 | + try: |
| 135 | + return resampled.mean() |
| 136 | + except Exception: |
| 137 | + return resampled.first() |
| 138 | + |
| 139 | + indicators = [_Indicator(try_mean_first(i).dropna().reindex(df.index).values.T, |
133 | 140 | **dict(i._opts, name=i.name,
|
134 | 141 | # Replace saved index with the resampled one
|
135 | 142 | index=df.index))
|
@@ -586,7 +593,7 @@ def __eq__(self, other):
|
586 | 593 | legend_label=legend_labels[j], line_color=color,
|
587 | 594 | line_width=1.3)
|
588 | 595 | # Add dashed centerline just because
|
589 |
| - mean = float(pd.Series(arr).mean()) |
| 596 | + mean = try_(lambda: float(pd.Series(arr).mean()), default=np.nan) |
590 | 597 | if not np.isnan(mean) and (abs(mean) < .1 or
|
591 | 598 | round(abs(mean), 1) == .5 or
|
592 | 599 | round(abs(mean), -1) in (50, 100, 200)):
|
|
0 commit comments