Skip to content

Commit d6b0ac8

Browse files
committed
Refactor scaling of OHLC data to avoid duplication.
1 parent 70280a2 commit d6b0ac8

File tree

3 files changed

+16
-40
lines changed

3 files changed

+16
-40
lines changed

backtesting/backtesting.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,9 @@ 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+
16261629
def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
16271630
plot_equity=True, plot_return=False, plot_pl=True,
16281631
plot_volume=True, plot_drawdown=False, plot_trades=True,
@@ -1716,7 +1719,7 @@ def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
17161719

17171720
return plot(
17181721
results=results,
1719-
df=self._data,
1722+
df=self._get_plot_data(),
17201723
indicators=results._strategy._indicators,
17211724
filename=filename,
17221725
plot_width=plot_width,

backtesting/lib.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import numpy as np
2525
import pandas as pd
2626

27-
from ._plotting import plot_heatmaps as _plot_heatmaps, plot
27+
from ._plotting import plot_heatmaps as _plot_heatmaps
2828
from ._stats import compute_stats as _compute_stats
2929
from ._util import SharedMemoryManager, _Array, _as_str, _batch, _tqdm
3030
from .backtesting import Backtest, Strategy
@@ -554,42 +554,11 @@ def run(self, **kwargs) -> pd.Series:
554554

555555
return result
556556

557-
def plot(self, *, results: pd.Series = None, filename=None, plot_width=None,
558-
plot_equity=True, plot_return=False, plot_pl=True,
559-
plot_volume=True, plot_drawdown=False, plot_trades=True,
560-
smooth_equity=False, relative_equity=True,
561-
superimpose: Union[bool, str] = True,
562-
resample=True, reverse_indicators=False,
563-
show_legend=True, open_browser=True):
564-
565-
data = self._data.copy()
566-
data[['Open', 'High', 'Low', 'Close']] /= self._fractional_unit
567-
data['Volume'] *= self._fractional_unit
568-
569-
if results is None:
570-
if self._results is None:
571-
raise RuntimeError('First issue `backtest.run()` to obtain results.')
572-
results = self._results
573-
574-
return plot(
575-
results=results,
576-
df=data,
577-
indicators=results._strategy._indicators,
578-
filename=filename,
579-
plot_width=plot_width,
580-
plot_equity=plot_equity,
581-
plot_return=plot_return,
582-
plot_pl=plot_pl,
583-
plot_volume=plot_volume,
584-
plot_drawdown=plot_drawdown,
585-
plot_trades=plot_trades,
586-
smooth_equity=smooth_equity,
587-
relative_equity=relative_equity,
588-
superimpose=superimpose,
589-
resample=resample,
590-
reverse_indicators=reverse_indicators,
591-
show_legend=show_legend,
592-
open_browser=open_browser)
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
593562

594563

595564
# Prevent pdoc3 documenting __init__ signature of Strategy subclasses

backtesting/test/_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,12 @@ def test_FractionalBacktest(self):
943943
self.assertAlmostEqual(first_trade['ExitPrice'][0], 261.7) # Fractional value 0.000261699
944944
indicators = stats['_strategy']._indicators
945945
self.assertEqual(len(indicators), 2)
946-
self.assertAlmostEqual(indicators[0][first_trade['EntryBar'][0]], 234.14, places=2) # Fractional value 0.000234139
947-
self.assertAlmostEqual(indicators[1][first_trade['EntryBar'][0]], 237.07, places=2) # Fractional value 0.000237067
946+
self.assertAlmostEqual(
947+
indicators[0][first_trade['EntryBar'][0]], 234.14, places=2
948+
) # Fractional value 0.000234139
949+
self.assertAlmostEqual(
950+
indicators[1][first_trade['EntryBar'][0]], 237.07, places=2
951+
) # Fractional value 0.000237067
948952

949953
def test_MultiBacktest(self):
950954
btm = MultiBacktest([GOOG, EURUSD, BTCUSD], SmaCross, cash=100_000)

0 commit comments

Comments
 (0)