|
25 | 25 |
|
26 | 26 | from ._plotting import plot_heatmaps as _plot_heatmaps
|
27 | 27 | from ._stats import compute_stats as _compute_stats
|
28 |
| -from ._util import SharedMemoryManager, _Array, _as_str, _batch, _tqdm |
| 28 | +from ._util import SharedMemoryManager, _Array, _as_str, _batch, _tqdm, patch |
29 | 29 | from .backtesting import Backtest, Strategy
|
30 | 30 |
|
31 | 31 | __pdoc__ = {}
|
@@ -533,10 +533,28 @@ def __init__(self,
|
533 | 533 | 'Use `FractionalBacktest(..., fractional_unit=)`.',
|
534 | 534 | category=DeprecationWarning, stacklevel=2)
|
535 | 535 | fractional_unit = 1 / kwargs.pop('satoshi')
|
536 |
| - data = data.copy() |
537 |
| - data[['Open', 'High', 'Low', 'Close']] *= fractional_unit |
538 |
| - data['Volume'] /= fractional_unit |
539 |
| - super().__init__(data, *args, **kwargs) |
| 536 | + self._fractional_unit = fractional_unit |
| 537 | + with warnings.catch_warnings(record=True): |
| 538 | + warnings.filterwarnings(action='ignore', message='frac') |
| 539 | + super().__init__(data, *args, **kwargs) |
| 540 | + |
| 541 | + 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): |
| 546 | + result = super().run(**kwargs) |
| 547 | + |
| 548 | + trades: pd.DataFrame = result['_trades'] |
| 549 | + trades['Size'] *= self._fractional_unit |
| 550 | + trades[['EntryPrice', 'ExitPrice', 'TP', 'SL']] /= self._fractional_unit |
| 551 | + |
| 552 | + indicators = result['_strategy']._indicators |
| 553 | + for indicator in indicators: |
| 554 | + if indicator._opts['overlay']: |
| 555 | + indicator /= self._fractional_unit |
| 556 | + |
| 557 | + return result |
540 | 558 |
|
541 | 559 |
|
542 | 560 | # Prevent pdoc3 documenting __init__ signature of Strategy subclasses
|
|
0 commit comments