{"record":{"id":"c1075894cb1e5f79","repo":"microsoft/qlib","slug":"infposition-doesn-t-support-get-stock-amount-dict","errorCode":null,"errorMessage":"InfPosition doesn't support get_stock_amount_dict","messagePattern":"InfPosition doesn't support get_stock_amount_dict","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/position.py","lineNumber":550,"sourceCode":"\n    def calculate_value(self) -> float:\n        raise NotImplementedError(f\"InfPosition doesn't support calculating value\")\n\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":532,"sourceCodeEnd":566,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/position.py#L532-L566","documentation":"InfPosition.get_stock_amount_dict() raises NotImplementedError because an infinite-cash position has no finite per-stock amounts to map. The amount dict API exists for Position (used by weight reporting and order generation against real holdings) and is intentionally unsupported for InfPosition.","triggerScenarios":"Calling get_stock_amount_dict() on an InfPosition instance; commonly from code that snapshots current holdings to compute deltas for the next rebalance (e.g. exchange.get_amount_info or custom strategy logic).","commonSituations":"Order-generation code reused against an InfPosition-based exchange; reporters trying to serialize holdings after backtests run with infinite cash.","solutions":["Switch the position to Position where amount dicts are needed","Gate the call: skip amount-dict logic for InfPosition instances","Derive intended amounts from the strategy's target weights and prices instead of the position snapshot"],"exampleFix":"# before\namounts = position.get_stock_amount_dict()\n\n# after\namounts = None if isinstance(position, InfPosition) else position.get_stock_amount_dict()","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.position import InfPosition\namounts = {} if isinstance(position, InfPosition) else position.get_stock_amount_dict()","typeGuard":"def has_amount_dict(pos) -> bool:\n    from qlib.backtest.position import InfPosition\n    return not isinstance(pos, InfPosition)","tryCatchPattern":"try:\n    amounts = position.get_stock_amount_dict()\nexcept NotImplementedError:\n    amounts = {}","preventionTips":["Keep amount-snapshot logic away from infinite-cash runs","Compute target amounts from weights x capital instead of position reads"],"tags":["qlib","backtest","position","inf-position","not-implemented"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}