Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backtesting/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def _mp_task_run(args):
data_shm, strategy, bt_kwargs, run_kwargs = args
dfs, shms = zip(*(SharedMemoryManager.shm2df(i) for i in data_shm))
try:
return [stats.filter(regex='^[^_]') if stats['# Trades'] else None
return [stats.filter(regex='^[^_]')
for stats in (Backtest(df, strategy, **bt_kwargs).run(**run_kwargs)
for df in dfs)]
finally:
Expand Down
25 changes: 25 additions & 0 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,31 @@ def test_MultiBacktest(self):
print(start_method, time.monotonic() - start_time)
plot_heatmaps(heatmap.mean(axis=1), open_browser=False)

def test_MultiBacktest_handles_mixed_no_trade_results(self):
class SometimesNoTrade(Strategy):
def init(self):
self._will_trade = len(self.data.index) == 20
self._has_bought = False

def next(self):
if not self._will_trade:
return
if self.position:
self.position.close()
elif not self._has_bought:
self.buy()
self._has_bought = True

btm = MultiBacktest([GOOG.iloc[:20], GOOG.iloc[:21], GOOG.iloc[:22]],
SometimesNoTrade, cash=100_000)
res = btm.run()
self.assertIsInstance(res, pd.DataFrame)
self.assertEqual(res.columns.tolist(), [0, 1, 2])
self.assertGreater(res.loc['# Trades', 0], 0)
self.assertEqual(res.loc['# Trades', 1], 0)
self.assertEqual(res.loc['# Trades', 2], 0)
self.assertFalse(isinstance(res.iloc[0, 0], pd.Series))
Comment thread
kernc marked this conversation as resolved.
Outdated


class TestUtil(TestCase):
def test_as_str(self):
Expand Down
Loading