{"record":{"id":"e2af07af5b49b63e","repo":"microsoft/qlib","slug":"infposition-doesn-t-support-get-stock-weight-dict","errorCode":null,"errorMessage":"InfPosition doesn't support get_stock_weight_dict","messagePattern":"InfPosition doesn't support get_stock_weight_dict","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/position.py","lineNumber":553,"sourceCode":"\n    def get_stock_list(self) -> List[str]:\n        raise NotImplementedError(f\"InfPosition doesn't support stock list position\")\n\n    def get_stock_price(self, code: str) -> float:\n        \"\"\"the price of the inf position is meaningless\"\"\"\n        return np.nan\n\n    def get_stock_amount(self, code: str) -> float:\n        return np.inf\n\n    def get_cash(self, include_settle: bool = False) -> float:\n        return np.inf\n\n    def get_stock_amount_dict(self) -> dict:\n        raise NotImplementedError(f\"InfPosition doesn't support get_stock_amount_dict\")\n\n    def get_stock_weight_dict(self, only_stock: bool = False) -> dict:\n        raise NotImplementedError(f\"InfPosition doesn't support get_stock_weight_dict\")\n\n    def add_count_all(self, bar: str) -> None:\n        raise NotImplementedError(f\"InfPosition doesn't support add_count_all\")\n\n    def update_weight_all(self) -> None:\n        raise NotImplementedError(f\"InfPosition doesn't support update_weight_all\")\n\n    def settle_start(self, settle_type: str) -> None:\n        pass\n\n    def settle_commit(self) -> None:\n        pass\n","sourceCodeStart":535,"sourceCodeEnd":566,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/position.py#L535-L566","documentation":"InfPosition.get_stock_weight_dict() raises NotImplementedError: weights are amount/value ratios, and with infinite amounts and infinite value they are undefined (inf/inf), so qlib blocks the API rather than returning NaNs. Only Position (finite holdings) supports weight dicts.","triggerScenarios":"Calling get_stock_weight_dict(only_stock=...) on an InfPosition; typical callers are performance reporting, order sizing from current weights, and nested-executor weight-rebalancing code.","commonSituations":"Reusing a strategy or report pipeline that reads current weights with an exchange/account configured for infinite cash (e.g. randomized order generation runs).","solutions":["Use Position when weight-based logic or reporting is involved","Skip weight reads for InfPosition: isinstance(position, InfPosition) guard","Track target weights in the strategy itself instead of reading them back from the position"],"exampleFix":"# before\nweights = position.get_stock_weight_dict(only_stock=True)\n\n# after\nweights = ({}) if isinstance(position, InfPosition) else position.get_stock_weight_dict(only_stock=True)","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.position import InfPosition\nweights = {} if isinstance(position, InfPosition) else position.get_stock_weight_dict(only_stock)","typeGuard":"def has_weight_dict(pos) -> bool:\n    from qlib.backtest.position import InfPosition\n    return not isinstance(pos, InfPosition)","tryCatchPattern":"try:\n    weights = position.get_stock_weight_dict()\nexcept NotImplementedError:\n    weights = {}","preventionTips":["Persist target weights in the strategy instead of reading them from InfPosition","Branch reporting on position type"],"tags":["qlib","backtest","position","inf-position","not-implemented"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}