From 354e0048d3f0d543dc725df974db75a432c2c5f5 Mon Sep 17 00:00:00 2001 From: Brett Elliot Date: Thu, 19 Dec 2024 06:10:15 -0500 Subject: [PATCH] fix division by zero bug when shorting --- lumibot/components/drift_rebalancer_logic.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lumibot/components/drift_rebalancer_logic.py b/lumibot/components/drift_rebalancer_logic.py index fdeec0b6..c5263eda 100644 --- a/lumibot/components/drift_rebalancer_logic.py +++ b/lumibot/components/drift_rebalancer_logic.py @@ -222,19 +222,23 @@ def _calculate_drift_row(self, row: pd.Series) -> Decimal: return Decimal(0) elif row["current_weight"] == Decimal(0) and row["target_weight"] == Decimal(0): - # Should nothing change? + # Do nothing return Decimal(0) elif row["current_quantity"] > Decimal(0) and row["target_weight"] == Decimal(0): - # Should we sell everything + # Sell everything return Decimal(-1) + elif row["current_quantity"] < Decimal(0) and row["target_weight"] == Decimal(0): + # Cover our short position + return Decimal(1) + elif row["current_quantity"] == Decimal(0) and row["target_weight"] > Decimal(0): # We don't have any of this asset, but we want to buy some. return Decimal(1) elif row["current_quantity"] == Decimal(0) and row["target_weight"] == Decimal(-1): - # Should we short everything we have + # Short everything we have return Decimal(-1) elif row["current_quantity"] == Decimal(0) and row["target_weight"] < Decimal(0):