Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DriftRebalancer.get_last_price and just use Strategy.get_last_price #690

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lumibot/components/drift_rebalancer_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lumibot/example_strategies/crypto_50_50.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions lumibot/example_strategies/drift_rebalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

self.drift_rebalancer_logic.rebalance(drift_df=self.drift_df)
Loading