{"record":{"id":"a8b8ba02a4be9932","repo":"microsoft/qlib","slug":"weight-position-is-weight-position-is-not-in-t","errorCode":null,"errorMessage":"weight_position is {}, weight_position is not in the range of (0, 1).","messagePattern":"weight_position is (.+?), weight_position is not in the range of \\(0, 1\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/exchange.py","lineNumber":561,"sourceCode":"        Generates the target position according to the weight and the cash.\n        NOTE: All the cash will be assigned to the tradable stock.\n        Parameter:\n        weight_position : dict {stock_id : weight}; allocate cash by weight_position\n            among then, weight must be in this range: 0 < weight < 1\n        cash : cash\n        start_time : the start time point of the step\n        end_time : the end time point of the step\n        direction : the direction of the deal price for estimating the amount\n                    # NOTE: this function is used for calculating target position. So the default direction is buy\n        \"\"\"\n\n        # calculate the total weight of tradable value\n        tradable_weight = 0.0\n        for stock_id, wp in weight_position.items():\n            if self.is_stock_tradable(stock_id=stock_id, start_time=start_time, end_time=end_time):\n                # weight_position must be greater than 0 and less than 1\n                if wp < 0 or wp > 1:\n                    raise ValueError(\n                        \"weight_position is {}, \" \"weight_position is not in the range of (0, 1).\".format(wp),\n                    )\n                tradable_weight += wp\n\n        if tradable_weight - 1.0 >= 1e-5:\n            raise ValueError(\"tradable_weight is {}, can not greater than 1.\".format(tradable_weight))\n\n        amount_dict = {}\n        for stock_id in weight_position:\n            if weight_position[stock_id] > 0.0 and self.is_stock_tradable(\n                stock_id=stock_id,\n                start_time=start_time,\n                end_time=end_time,\n            ):\n                amount_dict[stock_id] = (\n                    cash\n                    * weight_position[stock_id]\n                    / tradable_weight","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/exchange.py#L543-L579","documentation":"In Exchange's target-weight order flow, each per-stock weight in weight_position must lie in [0, 1]; a negative weight or a weight above 1 is rejected with ValueError (only tradable stocks are validated). The method then converts weights into share amounts proportional to tradable_weight, so out-of-range weights would produce nonsensical amounts.","triggerScenarios":"Passing a weight dict to the amount-from-weights path (get_amount_from_weight / target-weight order creation) containing values like -0.1 or 1.5, or weights expressed in percent (e.g. 30 for 30%).","commonSituations":"Alpha model outputs unnormalized or signed scores fed directly as weights; percentages not divided by 100; NaN-free but unbounded factor z-scores used as weights.","solutions":["Clip weights before calling: {k: min(max(v, 0.0), 1.0) for k, v in weight_position.items()}","If weights are percentages, divide by 100 first","If you intend short positions, note this API does not support them; use positive weights plus cash remainder"],"exampleFix":"# before\namounts = exch.get_amount_from_weight({'SH600000': 35.0}, ...)  # percent -> ValueError\n# after\nweights = {'SH600000': 35.0 / 100}\namounts = exch.get_amount_from_weight(weights, ...)","handlingStrategy":"validation","validationCode":"bad = {k: v for k, v in weight_position.items() if not (0.0 <= v <= 1.0)}\nassert not bad, f'weights out of [0,1]: {bad}'\nweights = {k: min(max(v, 0.0), 1.0) for k, v in weight_position.items()}","typeGuard":"def weights_in_range(w: dict) -> bool:\n    return all(0.0 <= v <= 1.0 for v in w.values())","tryCatchPattern":null,"preventionTips":["Clip or assert weights to [0,1] before target-weight order creation","Convert percent weights (0-100) to fractions (0-1) first"],"tags":["qlib","exchange","weights","value-error"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}