Skip to content

Commit 9c93ad2

Browse files
committed
REF: lib.FractionalBacktest: Precompute patched data once instead of before each run
1 parent 270fec6 commit 9c93ad2

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

backtesting/lib.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,17 @@ def __init__(self,
534534
category=DeprecationWarning, stacklevel=2)
535535
fractional_unit = 1 / kwargs.pop('satoshi')
536536
self._fractional_unit = fractional_unit
537+
self.__data: pd.DataFrame = data.copy(deep=False) # Shallow copy
538+
for col in ('Open', 'High', 'Low', 'Close',):
539+
self.__data[col] = self.__data[col] * self._fractional_unit
540+
for col in ('Volume',):
541+
self.__data[col] = self.__data[col] / self._fractional_unit
537542
with warnings.catch_warnings(record=True):
538543
warnings.filterwarnings(action='ignore', message='frac')
539544
super().__init__(data, *args, **kwargs)
540545

541546
def run(self, **kwargs) -> pd.Series:
542-
data = self._data.copy()
543-
data[['Open', 'High', 'Low', 'Close']] *= self._fractional_unit
544-
data['Volume'] /= self._fractional_unit
545-
with patch(self, '_data', data):
547+
with patch(self, '_data', self.__data):
546548
result = super().run(**kwargs)
547549

548550
trades: pd.DataFrame = result['_trades']

0 commit comments

Comments
 (0)