microsoft/qlib · error · NotImplementedError

pred_price_trend method is not implemented!

Error message

pred_price_trend method is not implemented!

What it means

SBBStrategyBase._pred_price_trend (qlib/contrib/strategy/rule_strategy.py) is an abstract prediction hook: subclasses like TWAPStrategy/SBBStrategyEMA must implement the trend prediction (up/down/mid) used by generate_trade_decision. The base raises NotImplementedError('pred_price_trend method is not implemented!') so a missing implementation fails loudly the first time a decision is generated.

Source

Thrown at qlib/contrib/strategy/rule_strategy.py:155

    # 3. Supporting checking the availability of trade decision

    def reset(self, outer_trade_decision: BaseTradeDecision = None, **kwargs):
        """
        Parameters
        ----------
        outer_trade_decision : BaseTradeDecision, optional
        """
        super(SBBStrategyBase, self).reset(outer_trade_decision=outer_trade_decision, **kwargs)
        if outer_trade_decision is not None:
            self.trade_trend = {}
            self.trade_amount = {}
            # init the trade amount of order and  predicted trade trend
            for order in outer_trade_decision.get_decision():
                self.trade_trend[order.stock_id] = self.TREND_MID
                self.trade_amount[order.stock_id] = order.amount

    def _pred_price_trend(self, stock_id, pred_start_time=None, pred_end_time=None):
        raise NotImplementedError("pred_price_trend method is not implemented!")

    def generate_trade_decision(self, execute_result=None):
        # get the number of trading step finished, trade_step can be [0, 1, 2, ..., trade_len - 1]
        trade_step = self.trade_calendar.get_trade_step()
        # get the total count of trading step
        trade_len = self.trade_calendar.get_trade_len()

        # update the order amount
        if execute_result is not None:
            for order, _, _, _ in execute_result:
                self.trade_amount[order.stock_id] -= order.deal_amount

        trade_start_time, trade_end_time = self.trade_calendar.get_step_time(trade_step)
        pred_start_time, pred_end_time = self.trade_calendar.get_step_time(trade_step, shift=1)
        order_list = []
        # for each order in in self.outer_trade_decision
        for order in self.outer_trade_decision.get_decision():
            # get the price trend

View on GitHub (pinned to 79633dd950)

Solutions

  1. Implement _pred_price_trend(self, stock_id, pred_start_time=None, pred_end_time=None) returning one of the TREND_* constants (UP/DOWN/MID)
  2. If you did implement it, verify the exact method name and that it is defined on the class actually instantiated
  3. Reference SBBStrategyEMA (in the same module) for a concrete implementation pattern

Example fix

class MySBBStrategy(SBBStrategyBase):
    def _pred_price_trend(self, stock_id, pred_start_time=None, pred_end_time=None):
        # your model logic here
        return self.TREND_UP if score > 0 else self.TREND_DOWN
Defensive patterns

Strategy: type-guard

Type guard

def strategy_implements_trend_pred(strategy) -> bool:
    return type(strategy)._pred_price_trend is not SBBStrategyBase._pred_price_trend

Try / catch

try:
    strategy.generate_trade_decision()
except NotImplementedError as e:
    if 'pred_price_trend' in str(e):
        raise TypeError(f'{type(strategy).__name__} must implement _pred_price_trend')
    raise

Prevention

When it happens

Trigger: Running a backtest with a strategy that inherits SBBStrategyBase without overriding _pred_price_trend; the error surfaces when the executor calls generate_trade_decision on the first trading step.

Common situations: Writing a custom SBB-style strategy and only overriding reset/generate_trade_decision but not the prediction hook; renaming the method so the base implementation is invoked.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/1361be0e9e36538d. Report an issue: GitHub.