From 23d9d5a0a90c05fcb0a8448853f2ef3337314f05 Mon Sep 17 00:00:00 2001 From: Chia-hung Tai Date: Thu, 26 Dec 2024 15:56:41 +0800 Subject: [PATCH] Fix the empty price_s case and self.instruments in SBBStrategyEMA. (#1677) * Fix the empty price_s case and self.instruments in SBBStrategyEMA. * Update qlib/contrib/strategy/rule_strategy.py * Update qlib/contrib/strategy/rule_strategy.py --------- Co-authored-by: you-n-g Co-authored-by: Linlang --- qlib/backtest/report.py | 4 ++++ qlib/contrib/strategy/rule_strategy.py | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/qlib/backtest/report.py b/qlib/backtest/report.py index 89f595df7..d0d327d03 100644 --- a/qlib/backtest/report.py +++ b/qlib/backtest/report.py @@ -427,6 +427,10 @@ class Indicator: # NOTE ~(price_s < 1e-08) is different from price_s >= 1e-8 # ~(np.nan < 1e-8) -> ~(False) -> True + # if price_s is empty + if price_s.empty: + return None, None + assert isinstance(price_s, idd.SingleData) if agg == "vwap": volume_s = trade_exchange.get_volume(inst, trade_start_time, trade_end_time, method=None) diff --git a/qlib/contrib/strategy/rule_strategy.py b/qlib/contrib/strategy/rule_strategy.py index f2b919739..4c1fc2f16 100644 --- a/qlib/contrib/strategy/rule_strategy.py +++ b/qlib/contrib/strategy/rule_strategy.py @@ -326,8 +326,10 @@ class SBBStrategyEMA(SBBStrategyBase): if instruments is None: warnings.warn("`instruments` is not set, will load all stocks") self.instruments = "all" - if isinstance(instruments, str): + elif isinstance(instruments, str): self.instruments = D.instruments(instruments) + elif isinstance(instruments, List): + self.instruments = instruments self.freq = freq super(SBBStrategyEMA, self).__init__( outer_trade_decision, level_infra, common_infra, trade_exchange=trade_exchange, **kwargs