Skip to content

Commit 730e354

Browse files
committed
BUG: More robust index computation in lib.resample_apply
Fixes #19
1 parent 2145e01 commit 730e354

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

backtesting/lib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ def strategy_I(func, *args, **kwargs):
221221

222222
# Resample back to data index
223223
def wrap_func(resampled, *args, **kwargs):
224-
ind = func(resampled, *args, **kwargs)
225-
ind = ind.reindex(index=series.index | ind.index,
224+
ind = pd.Series(np.asarray(func(resampled, *args, **kwargs)),
225+
index=resampled.index,
226+
name=resampled.name)
227+
ind = ind.reindex(index=series.index | resampled.index,
226228
method='ffill').reindex(series.index)
227229
return ind
228230

backtesting/test/_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,13 @@ def test_resample_apply(self):
475475
self.assertEqual(res.iloc[-48:].unique().tolist(),
476476
[1.2426429999999997, 1.2423809999999995, 1.2422749999999998])
477477

478+
def resets_index(*args):
479+
return pd.Series(SMA(*args).values)
480+
481+
res2 = resample_apply('D', resets_index, EURUSD.Close, 10)
482+
self.assertTrue((res.dropna() == res2.dropna()).all())
483+
self.assertTrue((res.index == res2.index).all())
484+
478485
def test_plot_heatmaps(self):
479486
bt = Backtest(GOOG, SmaCross)
480487
stats, heatmap = bt.optimize(fast=range(2, 7, 2),

0 commit comments

Comments
 (0)