Skip to content

Commit 551e7b0

Browse files
committed
REF: Remove pandas/bokeh deprecated API
1 parent de05c59 commit 551e7b0

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

backtesting/_plotting.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _group_trades(column):
144144
def f(s, new_index=pd.Index(df.index.view(int)), bars=trades[column]):
145145
if s.size:
146146
# Via int64 because on pandas recently broken datetime
147-
mean_time = int(bars.loc[s.index].view(int).mean())
147+
mean_time = int(bars.loc[s.index].astype(int).mean())
148148
new_bar_idx = new_index.get_indexer([mean_time], method='nearest')[0]
149149
return new_bar_idx
150150
return f
@@ -440,11 +440,11 @@ def _plot_superimposed_ohlc():
440440
"""Superimposed, downsampled vbars"""
441441
time_resolution = pd.DatetimeIndex(df['datetime']).resolution
442442
resample_rule = (superimpose if isinstance(superimpose, str) else
443-
dict(day='M',
443+
dict(day='ME',
444444
hour='D',
445-
minute='H',
446-
second='T',
447-
millisecond='S').get(time_resolution))
445+
minute='h',
446+
second='min',
447+
millisecond='s').get(time_resolution))
448448
if not resample_rule:
449449
warnings.warn(
450450
f"'Can't superimpose OHLC data with rule '{resample_rule}'"
@@ -548,22 +548,22 @@ def __eq__(self, other):
548548
if is_overlay:
549549
ohlc_extreme_values[source_name] = arr
550550
if is_scatter:
551-
fig.scatter(
551+
fig.circle(
552552
'index', source_name, source=source,
553553
legend_label=legend_label, color=color,
554554
line_color='black', fill_alpha=.8,
555-
marker='circle', radius=BAR_WIDTH / 2 * 1.5)
555+
radius=BAR_WIDTH / 2 * .9)
556556
else:
557557
fig.line(
558558
'index', source_name, source=source,
559559
legend_label=legend_label, line_color=color,
560560
line_width=1.3)
561561
else:
562562
if is_scatter:
563-
r = fig.scatter(
563+
r = fig.circle(
564564
'index', source_name, source=source,
565565
legend_label=LegendStr(legend_label), color=color,
566-
marker='circle', radius=BAR_WIDTH / 2 * .9)
566+
radius=BAR_WIDTH / 2 * .6)
567567
else:
568568
r = fig.line(
569569
'index', source_name, source=source,

backtesting/backtesting.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1444,13 +1444,12 @@ def _batch(seq):
14441444
finally:
14451445
del Backtest._mp_backtests[backtest_uuid]
14461446

1447-
best_params = heatmap.idxmax()
1448-
1449-
if pd.isnull(best_params):
1447+
if pd.isnull(heatmap).all():
14501448
# No trade was made in any of the runs. Just make a random
14511449
# run so we get some, if empty, results
14521450
stats = self.run(**param_combos[0])
14531451
else:
1452+
best_params = heatmap.idxmax(skipna=True)
14541453
stats = self.run(**dict(zip(heatmap.index.names, best_params)))
14551454

14561455
if return_heatmap:

backtesting/lib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def compute_stats(
194194
equity[:] = stats._equity_curve.Equity.iloc[0]
195195
for t in trades.itertuples(index=False):
196196
equity.iloc[t.EntryBar:] += t.PnL
197-
return _compute_stats(trades=trades, equity=equity, ohlc_data=data,
197+
return _compute_stats(trades=trades, equity=equity.values, ohlc_data=data,
198198
risk_free_rate=risk_free_rate, strategy_instance=stats._strategy)
199199

200200

backtesting/test/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def _read_file(filename):
66
from os.path import dirname, join
77

88
return pd.read_csv(join(dirname(__file__), filename),
9-
index_col=0, parse_dates=True, infer_datetime_format=True)
9+
index_col=0, parse_dates=True)
1010

1111

1212
GOOG = _read_file('GOOG.csv')

backtesting/test/_test.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def test_hide_legend(self):
672672

673673
def test_resolutions(self):
674674
with _tempfile() as f:
675-
for rule in 'LSTHDWM':
675+
for rule in 'ms s min h D W ME'.split():
676676
with self.subTest(rule=rule):
677677
df = EURUSD.iloc[:2].resample(rule).agg(OHLCV_AGG).dropna().iloc[:1100]
678678
bt = Backtest(df, SmaCross)
@@ -980,7 +980,8 @@ def next(self):
980980
'Low': [100, 100, 100, 50, 50],
981981
'Close': [100, 100, 100, 50, 50],
982982
})
983-
bt = Backtest(df, S, cash=100, trade_on_close=True)
983+
with self.assertWarnsRegex(UserWarning, 'index is not datetime'):
984+
bt = Backtest(df, S, cash=100, trade_on_close=True)
984985
self.assertEqual(bt.run()._trades['ExitPrice'][0], 50)
985986

986987

0 commit comments

Comments
 (0)