Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion backtesting/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ def __exit__(self, *args, **kwargs):
def arr2shm(self, vals):
"""Array to shared memory. Returns (shm_name, shape, dtype) used for restore."""
assert vals.ndim == 1, (vals.ndim, vals.shape, vals)
shm = self.SharedMemory(size=vals.nbytes, create=True)
# Not `vals.nbytes`; lazy indexes (e.g. RangeIndex) report their own,
# constant size rather than that of the array they materialize into
shm = self.SharedMemory(size=vals.size * vals.dtype.base.itemsize, create=True)
# np.array can't handle pandas' tz-aware datetimes
# https://github.com/numpy/numpy/issues/18279
buf = np.ndarray(vals.shape, dtype=vals.dtype.base, buffer=shm.buf)
Expand Down
10 changes: 10 additions & 0 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,16 @@ def test_optimize_datetime_index_with_timezone(self):
res = Backtest(data, SmaCross).optimize(fast=range(2, 3), slow=range(4, 5))
self.assertGreater(res['# Trades'], 0)

def test_optimize_range_index(self):
# Data is passed to the workers through shared memory sized by
# `.nbytes`, which a lazy RangeIndex under-reports, previously raising
# "TypeError: buffer is too small for requested array". See GH issue #1237.
data: pd.DataFrame = GOOG.iloc[:100].reset_index(drop=True)
with self.assertWarnsRegex(UserWarning, 'index is not datetime'):
bt = Backtest(data, SmaCross)
res = bt.optimize(fast=range(2, 3), slow=range(4, 5))
self.assertGreater(res['# Trades'], 0)

def test_sl_tp_values_in_trades_df(self):
class S(_S):
def next(self):
Expand Down