{"record":{"id":"16a3d016e7647954","repo":"microsoft/qlib","slug":"please-implement-the-lt-method","errorCode":null,"errorMessage":"Please implement the `__lt__` method","messagePattern":"Please implement the `__lt__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":255,"sourceCode":"        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\")\n\n    def mean(self) -> float:\n        raise NotImplementedError(f\"Please implement the `mean` method\")\n\n    def count(self) -> int:\n        \"\"\"Return the count of the single metric, NaN is not included.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `count` method\")\n\n    def abs(self) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `abs` method\")\n","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L237-L273","documentation":"BaseSingleMetric.__lt__ is the abstract less-than operator, designed to yield an element-wise boolean metric. The base class only declares it and raises NotImplementedError; SingleMetric (line 436) implements it. Using < on a raw BaseSingleMetric instance triggers the error.","triggerScenarios":"m < threshold, m1 < m2, or sorted(metrics) where m is a bare BaseSingleMetric; stop-loss checks (price < stop_level); risk filters (exposure < limit); subclasses that implement __gt__ but not __lt__.","commonSituations":"Building sell/exit masks from metric thresholds; min()/sorted() over metric objects invoking __lt__; partial implementations in custom metric backends.","solutions":["Use SingleMetric for less-than comparisons","Implement __lt__ in your subclass mirroring SingleMetric (element-wise, returns the metric type)","Extract the underlying Series via .storage for sorting/min operations"],"exampleFix":"# before\nexit_mask = BaseSingleMetric(drawdown) < -0.1\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nexit_mask = SingleMetric(drawdown) < -0.1","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__lt__ is not BaseSingleMetric.__lt__, \"object does not implement __lt__\"","typeGuard":"def supports_lt(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__lt__ is not BaseSingleMetric.__lt__","tryCatchPattern":null,"preventionTips":["Use SingleMetric for less-than filters (stop-loss, limits)","sorted()/min()/max() call __lt__ — apply them to the underlying Series, not metric objects","Implement __lt__ whenever you implement __gt__"],"tags":["python","qlib","operator-overloading","comparison","abstract-method","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}