diff --git a/lumibot/components/drift_rebalancer_logic.py b/lumibot/components/drift_rebalancer_logic.py index 01db1b51..1d637107 100644 --- a/lumibot/components/drift_rebalancer_logic.py +++ b/lumibot/components/drift_rebalancer_logic.py @@ -206,7 +206,7 @@ def _add_positions(self) -> None: current_value = Decimal(position.quantity) else: is_quote_asset = False - last_price = self.strategy.get_last_price(position.asset) + last_price = Decimal(self.strategy.get_last_price(position.asset)) current_value = current_quantity * last_price self._add_position( symbol=symbol, @@ -359,7 +359,7 @@ def _rebalance(self, df: pd.DataFrame = None) -> None: # Sell everything (or create 100% short position) base_asset = row["base_asset"] quantity = row["current_quantity"] - last_price = self.strategy.get_last_price(base_asset) + last_price = Decimal(self.strategy.get_last_price(base_asset)) limit_price = self.calculate_limit_price(last_price=last_price, side="sell") if quantity == 0 and self.shorting: # Create a 100% short position. @@ -380,7 +380,7 @@ def _rebalance(self, df: pd.DataFrame = None) -> None: elif row["drift"] < 0: base_asset = row["base_asset"] - last_price = self.strategy.get_last_price(base_asset) + last_price = Decimal(self.strategy.get_last_price(base_asset)) limit_price = self.calculate_limit_price(last_price=last_price, side="sell") quantity = (row["current_value"] - row["target_value"]) / limit_price if self.fractional_shares: @@ -414,7 +414,7 @@ def _rebalance(self, df: pd.DataFrame = None) -> None: # Cover our short position base_asset = row["base_asset"] quantity = abs(row["current_quantity"]) - last_price = self.strategy.get_last_price(base_asset) + last_price = Decimal(self.strategy.get_last_price(base_asset)) limit_price = self.calculate_limit_price(last_price=last_price, side="buy") order = self.place_order( base_asset=base_asset, @@ -427,7 +427,7 @@ def _rebalance(self, df: pd.DataFrame = None) -> None: elif row["drift"] > 0: base_asset = row["base_asset"] - last_price = self.strategy.get_last_price(base_asset) + last_price = Decimal(self.strategy.get_last_price(base_asset)) limit_price = self.calculate_limit_price(last_price=last_price, side="buy") order_value = row["target_value"] - row["current_value"] quantity = min(order_value, cash_position) / limit_price diff --git a/lumibot/example_strategies/crypto_50_50.py b/lumibot/example_strategies/crypto_50_50.py index ef1f8c66..7161c29a 100644 --- a/lumibot/example_strategies/crypto_50_50.py +++ b/lumibot/example_strategies/crypto_50_50.py @@ -35,11 +35,11 @@ parameters["portfolio_weights"] = [ { "base_asset": Asset(symbol='BTC-USD', asset_type='stock'), - "weight": Decimal("0.6") + "weight": Decimal("0.5") }, { "base_asset": Asset(symbol='ETH-USD', asset_type='stock'), - "weight": Decimal("0.4") + "weight": Decimal("0.5") } ] results = DriftRebalancer.backtest( @@ -64,11 +64,11 @@ parameters["portfolio_weights"] = [ { "base_asset": Asset(symbol='BTC', asset_type='crypto'), - "weight": Decimal("0.6") + "weight": Decimal("0.5") }, { "base_asset": Asset(symbol='ETH', asset_type='crypto'), - "weight": Decimal("0.4") + "weight": Decimal("0.5") } ] strategy = DriftRebalancer(broker, parameters=parameters) diff --git a/lumibot/example_strategies/drift_rebalancer.py b/lumibot/example_strategies/drift_rebalancer.py index d881a7a5..62c1b2be 100644 --- a/lumibot/example_strategies/drift_rebalancer.py +++ b/lumibot/example_strategies/drift_rebalancer.py @@ -119,10 +119,4 @@ def on_trading_iteration(self) -> None: ) self.drift_df = self.drift_rebalancer_logic.calculate(portfolio_weights=self.portfolio_weights) - self.drift_rebalancer_logic.rebalance(drift_df=self.drift_df) - - def get_last_price(self, asset: Union[Asset, str], quote=None, exchange=None): - """Override get_last_price to use the strategy's quote asset and return a decimal.""" - quote_asset = self.quote_asset or Asset(symbol="USD", asset_type="forex") - return Decimal(super().get_last_price(asset=asset, quote=quote_asset)) - \ No newline at end of file + self.drift_rebalancer_logic.rebalance(drift_df=self.drift_df) \ No newline at end of file