Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0c6589

Browse files
PERF: Improve _MAX_CANDLES downsampling on large datasets (#329)
* Fixed __maybe_resample_data freq performance * Fixed formatting * Use more pandas
1 parent 7a6434b commit a0c6589

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

‎backtesting/_plotting.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,23 @@ def _maybe_resample_data(resample_rule, df, indicators, equity_data, trades):
9999
if resample_rule is False or len(df) <= _MAX_CANDLES:
100100
return df, indicators, equity_data, trades
101101

102-
from_index = dict(day=-2, hour=-6, minute=1, second=0, millisecond=0,
103-
microsecond=0, nanosecond=0)[df.index.resolution]
104-
FREQS = ('1T', '5T', '10T', '15T', '30T', '1H', '2H', '4H', '8H', '1D', '1W', '1M')
105-
freq = next((f for f in FREQS[from_index:]
106-
if len(df.resample(f)) <= _MAX_CANDLES), FREQS[-1])
102+
freq_minutes = pd.Series({
103+
"1T": 1,
104+
"5T": 5,
105+
"10T": 10,
106+
"15T": 15,
107+
"30T": 30,
108+
"1H": 60,
109+
"2H": 60*2,
110+
"4H": 60*4,
111+
"8H": 60*8,
112+
"1D": 60*24,
113+
"1W": 60*24*7,
114+
"1M": np.inf,
115+
})
116+
timespan = df.index[-1] - df.index[0]
117+
require_minutes = (timespan / _MAX_CANDLES).total_seconds() // 60
118+
freq = freq_minutes.where(freq_minutes >= require_minutes).first_valid_index()
107119
warnings.warn(f"Data contains too many candlesticks to plot; downsampling to {freq!r}. "
108120
"See `Backtest.plot(resample=...)`")
109121

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /