From e5c040484ddfc15d568b8003a6fdbe2f260fd7b3 Mon Sep 17 00:00:00 2001 From: Lawik974 Date: Sun, 14 Sep 2025 12:04:41 +0200 Subject: [PATCH] Fix backtesting short positions buy_to_close quantity sign --- lumibot/backtesting/backtesting_broker.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lumibot/backtesting/backtesting_broker.py b/lumibot/backtesting/backtesting_broker.py index b599fdc7c..695684072 100644 --- a/lumibot/backtesting/backtesting_broker.py +++ b/lumibot/backtesting/backtesting_broker.py @@ -448,6 +448,12 @@ def _process_filled_order(self, order, price, quantity): """ BackTesting needs to create/update positions when orders are filled becuase there is no broker to do it """ + # If the order has a "buy_to_open" or "buy_to_close" side, then we should change it to "buy" + if order.is_buy_order(): + order.side = Order.OrderSide.BUY + # If the order has a "sell_to_open" or "sell_to_close" side, then we should change it to "sell" + if order.is_sell_order(): + order.side = Order.OrderSide.SELL # This is a parent order, typically for a Multileg strategy. The parent order itself is expected to be # filled after all child orders are filled. if order.is_parent() and order.order_class in [Order.OrderClass.MULTILEG, Order.OrderClass.OCO]: