{"record":{"id":"282a950231cb725d","repo":"microsoft/qlib","slug":"please-implement-the-mul-method","errorCode":null,"errorMessage":"Please implement the `__mul__` method","messagePattern":"Please implement the `__mul__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":243,"sourceCode":"                SZ300692    NaN\n                SZ300719    NaN,\n        \"\"\"\n        raise NotImplementedError(f\"Please implement the `__init__` method\")\n\n    def __add__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__add__` method\")\n\n    def __radd__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        return self + other\n\n    def __sub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__sub__` method\")\n\n    def __rsub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__rsub__` method\")\n\n    def __mul__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__mul__` method\")\n\n    def __truediv__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__truediv__` method\")\n\n    def __eq__(self, other: object) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__eq__` method\")\n\n    def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__gt__` method\")\n\n    def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__lt__` method\")\n\n    def __len__(self) -> int:\n        raise NotImplementedError(f\"Please implement the `__len__` method\")\n\n    def sum(self) -> float:\n        raise NotImplementedError(f\"Please implement the `sum` method\")","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L225-L261","documentation":"BaseSingleMetric.__mul__ is the abstract multiplication operator for per-stock metric vectors. The base class only declares the protocol; SingleMetric supplies the element-wise implementation. The error appears only when multiplying a bare BaseSingleMetric (or an incomplete subclass) by another metric or a scalar.","triggerScenarios":"m * 2.0, m1 * m2, or m * position_metric where m is a raw BaseSingleMetric; weighting code such as weight * price inside custom indicators; subclasses defining __rmul__ without __mul__.","commonSituations":"Scaling account metrics (e.g. applying leverage or FX multipliers); element-wise products of two metric vectors (price * shares); partial operator implementations in user subclasses.","solutions":["Use SingleMetric for multiplication","Implement __mul__(self, other) in your subclass following SingleMetric's NaN-aware element-wise pattern","Validate operand types before multiplying in generic pipelines"],"exampleFix":"# before\nnotional = BaseSingleMetric(price) * 100\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nnotional = SingleMetric(price) * 100","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__mul__ is not BaseSingleMetric.__mul__, \"object does not implement __mul__\"","typeGuard":"def supports_mul(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__mul__ is not BaseSingleMetric.__mul__","tryCatchPattern":null,"preventionTips":["Use SingleMetric for scaling and element-wise products","Implement operators in complete sets (__mul__ with __rmul__ semantics, __sub__ with __rsub__)","For weight * value products, ensure both operands cover the same stock ids"],"tags":["python","qlib","operator-overloading","abstract-method","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}