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 e177a8c

Browse files
committed
BUG: Fix "'CAGR [%]' must match a key in pd.Series result of bt.run()"
1 parent 65d61d6 commit e177a8c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

‎backtesting/_stats.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ def __repr__(self):
197197
# 'format.na_rep', '--', # TODO: Enable once it works
198198
):
199199
return super().__repr__()
200+
201+
202+
def dummy_stats():
203+
from .backtesting import Trade, _Broker
204+
index = pd.DatetimeIndex(['2025'])
205+
data = pd.DataFrame({col: [np.nan] for col in ('Close',)}, index=index)
206+
trade = Trade(_Broker(data=data, cash=10000, spread=.01, commission=.01, margin=.1,
207+
trade_on_close=True, hedging=True, exclusive_orders=False, index=index),
208+
1, 1, 0, None)
209+
trade._replace(exit_price=1, exit_bar=0)
210+
trade._commissions = np.nan
211+
return compute_stats([trade], np.r_[[np.nan]], data, None, 0)

‎backtesting/backtesting.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from numpy.random import default_rng
2525

2626
from ._plotting import plot # noqa: I001
27-
from ._stats import compute_stats
27+
from ._stats import compute_stats, dummy_stats
2828
from ._util import (
2929
SharedMemoryManager, _as_str, _Indicator, _Data, _batch, _indicator_warmup_nbars,
3030
_strategy_indicators, patch, try_, _tqdm,
@@ -1425,9 +1425,7 @@ def optimize(self, *,
14251425
maximize_key = None
14261426
if isinstance(maximize, str):
14271427
maximize_key = str(maximize)
1428-
stats_keys = compute_stats(
1429-
[], np.r_[[np.nan]], pd.DataFrame({col: [np.nan] for col in ('Close',)}), None, 0).index
1430-
if maximize not in stats_keys:
1428+
if maximize not in dummy_stats().index:
14311429
raise ValueError('`maximize`, if str, must match a key in pd.Series '
14321430
'result of backtest.run()')
14331431

0 commit comments

Comments
(0)

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