Skip to content

Commit 1740444

Browse files
committed
REF: Fractionalize data df JIT before bt.run
1 parent d6b0ac8 commit 1740444

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

backtesting/backtesting.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,6 @@ def _mp_task(arg):
16231623
for shmem in shm:
16241624
shmem.close()
16251625

1626-
def _get_plot_data(self) -> pd.DataFrame:
1627-
return self._data
1628-
16291626
def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
16301627
plot_equity=True, plot_return=False, plot_pl=True,
16311628
plot_volume=True, plot_drawdown=False, plot_trades=True,
@@ -1719,7 +1716,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
17191716

17201717
return plot(
17211718
results=results,
1722-
df=self._get_plot_data(),
1719+
df=self._data,
17231720
indicators=results._strategy._indicators,
17241721
filename=filename,
17251722
plot_width=plot_width,

backtesting/lib.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from ._plotting import plot_heatmaps as _plot_heatmaps
2828
from ._stats import compute_stats as _compute_stats
29-
from ._util import SharedMemoryManager, _Array, _as_str, _batch, _tqdm
29+
from ._util import SharedMemoryManager, _Array, _as_str, _batch, _tqdm, patch
3030
from .backtesting import Backtest, Strategy
3131

3232
__pdoc__ = {}
@@ -534,14 +534,17 @@ def __init__(self,
534534
'Use `FractionalBacktest(..., fractional_unit=)`.',
535535
category=DeprecationWarning, stacklevel=2)
536536
fractional_unit = 1 / kwargs.pop('satoshi')
537-
data = data.copy()
538-
data[['Open', 'High', 'Low', 'Close']] *= fractional_unit
539-
data['Volume'] /= fractional_unit
540537
self._fractional_unit = fractional_unit
541-
super().__init__(data, *args, **kwargs)
538+
with warnings.catch_warnings(record=True):
539+
warnings.filterwarnings(action='ignore', message='frac')
540+
super().__init__(data, *args, **kwargs)
542541

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

546549
trades: pd.DataFrame = result['_trades']
547550
trades['Size'] *= self._fractional_unit
@@ -554,12 +557,6 @@ def run(self, **kwargs) -> pd.Series:
554557

555558
return result
556559

557-
def _get_plot_data(self) -> pd.DataFrame:
558-
plot_data = self._data.copy()
559-
plot_data[['Open', 'High', 'Low', 'Close']] /= self._fractional_unit
560-
plot_data['Volume'] *= self._fractional_unit
561-
return plot_data
562-
563560

564561
# Prevent pdoc3 documenting __init__ signature of Strategy subclasses
565562
for cls in list(globals().values()):

0 commit comments

Comments
 (0)