{"record":{"id":"6f04e8761c7d49da","repo":"microsoft/qlib","slug":"do-not-support-order-direction","errorCode":null,"errorMessage":"do not support order direction {}","messagePattern":"do not support order direction (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/position.py","lineNumber":399,"sourceCode":"        else:\n            raise NotImplementedError(f\"This type of input is not supported\")\n\n    def _del_stock(self, stock_id: str) -> None:\n        del self.position[stock_id]\n\n    def check_stock(self, stock_id: str) -> bool:\n        return stock_id in self.position\n\n    def update_order(self, order: Order, trade_val: float, cost: float, trade_price: float) -> None:\n        # handle order, order is a order class, defined in exchange.py\n        if order.direction == Order.BUY:\n            # BUY\n            self._buy_stock(order.stock_id, trade_val, cost, trade_price)\n        elif order.direction == Order.SELL:\n            # SELL\n            self._sell_stock(order.stock_id, trade_val, cost, trade_price)\n        else:\n            raise NotImplementedError(\"do not support order direction {}\".format(order.direction))\n\n    def update_stock_price(self, stock_id: str, price: float) -> None:\n        self.position[stock_id][\"price\"] = price\n\n    def update_stock_count(self, stock_id: str, bar: str, count: float) -> None:  # TODO: check type of `bar`\n        self.position[stock_id][f\"count_{bar}\"] = count\n\n    def update_stock_weight(self, stock_id: str, weight: float) -> None:\n        self.position[stock_id][\"weight\"] = weight\n\n    def calculate_stock_value(self) -> float:\n        stock_list = self.get_stock_list()\n        value = 0\n        for stock_id in stock_list:\n            value += self.position[stock_id][\"amount\"] * self.position[stock_id][\"price\"]\n        return value\n\n    def calculate_value(self) -> float:","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/position.py#L381-L417","documentation":"Position.update_order dispatches on order.direction: Order.BUY routes to _buy_stock, Order.SELL to _sell_stock; every other value raises NotImplementedError('do not support order direction {}'). Order directions are integers (Order.BUY=1, Order.SELL=-1 in qlib's Order class), so 0 or arbitrary ints/None are rejected.","triggerScenarios":"update_order called with an Order whose direction is not Order.BUY/Order.SELL: direction left as 0/None by a constructor default, or a custom Order subclass with an extra direction enum passed to the standard Position.","commonSituations":"Constructing Order objects manually with direction=0; deserializing orders from dicts where direction lost its value; subclassing OrderDir with a 'hold' direction but reusing Position as the position handler.","solutions":["Always set direction from Order.BUY / Order.SELL when constructing orders","Filter out non-trade orders (amount_delta == 0 / 'hold') before calling update_order","If you genuinely need extra directions, override update_order in a custom Position subclass"],"exampleFix":"# before\norder = Order(stock_id, 0, amount, trade_info)  # direction unset\n\n# after\nfrom qlib.backtest.order import Order\norder = Order(stock_id, Order.SELL if signal < 0 else Order.BUY, amount, trade_info)","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.order import Order\nassert order.direction in (Order.BUY, Order.SELL), f\"bad direction {order.direction}\"","typeGuard":"def is_trade_direction(d) -> bool:\n    return d in (Order.BUY, Order.SELL)","tryCatchPattern":"try:\n    position.update_order(order, trade_val, cost, trade_price)\nexcept NotImplementedError:\n    logger.warning(\"skipping order with direction %s\", order.direction)","preventionTips":["Construct orders with Order.BUY/Order.SELL","Drop no-op (amount_delta == 0) orders before updates","Log order.direction in custom executors to catch defaults"],"tags":["qlib","backtest","order-execution","position","invalid-argument"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}