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

Drift rebalancer 5 #626

Merged
merged 15 commits into from
Nov 18, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test_bot.py
lumi_tradier
lumiwealth_tradier
ThetaTerminal.jar
pytest.ini

# Pypi deployment
build
Expand Down
Empty file.
426 changes: 426 additions & 0 deletions lumibot/components/drift_rebalancer_logic.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lumibot/data_sources/tradier_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def get_historical_prices(
# get twice as many days as we need to ensure we get enough bars
tcal_start_date = end_date - (td * length * 2)
trading_days = get_trading_days(market='NYSE', start_date=tcal_start_date, end_date=end_date)
# Filer out trading days when the market_open is after the end_date
trading_days = trading_days[trading_days['market_open'] < end_date]
# Now, start_date is the length bars before the last trading day
start_date = trading_days.index[-length]

Expand Down
17 changes: 13 additions & 4 deletions lumibot/example_strategies/classic_60_40.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime

from lumibot.components.drift_rebalancer_logic import DriftType
from lumibot.entities import Order
from lumibot.credentials import IS_BACKTESTING
from lumibot.example_strategies.drift_rebalancer import DriftRebalancer

"""
Strategy Description

This strategy demonstrates the DriftRebalancer by rebalancing to a classic 60% stocks, 40% bonds portfolio.
This is an implementation of a classic 60% stocks, 40% bonds portfolio, that demonstration the DriftRebalancer strategy.
It rebalances a portfolio of assets to a target weight every time the asset drifts
by a certain threshold. The strategy will sell the assets that has drifted the most and buy the
assets that has drifted the least to bring the portfolio back to the target weights.
Expand All @@ -23,7 +25,14 @@
parameters = {
"market": "NYSE",
"sleeptime": "1D",
"drift_threshold": "0.05",

# Pro tip: In live trading rebalance multiple times a day, more buys will be placed after the sells fill.
# This will make it really likely that you will complete the rebalance in a single day.
# "sleeptime": 60,

"drift_type": DriftType.RELATIVE,
"drift_threshold": "0.1",
"order_type": Order.OrderType.MARKET,
"acceptable_slippage": "0.005", # 50 BPS
"fill_sleeptime": 15,
"target_weights": {
Expand All @@ -44,11 +53,11 @@
backtesting_end,
benchmark_asset="SPY",
parameters=parameters,
show_plot=False,
show_plot=True,
show_tearsheet=False,
save_tearsheet=False,
show_indicators=False,
save_logfile=False,
save_logfile=True,
# show_progress_bar=False,
# quiet_logs=False
)
Expand Down
330 changes: 66 additions & 264 deletions lumibot/example_strategies/drift_rebalancer.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/test_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_tradier_data_source_generates_simple_returns(self):
assert len(prices.df) == self.length

# This shows a bug. The index a datetime.date but should be a timestamp
# assert isinstance(prices.df.index[0], pd.Timestamp)
assert isinstance(prices.df.index[0], pd.Timestamp)

# assert that the last row has a return value
assert prices.df["return"].iloc[-1] is not None
Loading
Loading