Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/Lumiwealth/lumibot into dev
Browse files Browse the repository at this point in the history
grzesir committed Jan 14, 2025
2 parents a16216d + 7be4373 commit 1337664
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 0 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -49,8 +49,6 @@ def pandas_data_fixture() -> Dict[Asset, Data]:
parse_dates=True,
index_col=0,
header=0,
usecols=[0, 1, 2, 3, 4, 6, 7],
names=["Date", "Open", "High", "Low", "Close", "Volume", "Dividends"],
)
df = df.rename(
columns={
41 changes: 38 additions & 3 deletions tests/test_drift_rebalancer.py
Original file line number Diff line number Diff line change
@@ -22,8 +22,8 @@

class MockStrategyWithDriftCalculationLogic(Strategy):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, broker: BacktestingBroker, *args, **kwargs):
super().__init__(broker=broker, *args, **kwargs)
self.orders = []
self.target_weights = {}
self.drift_rebalancer_logic = DriftRebalancerLogic(
@@ -96,7 +96,7 @@ def mock_add_positions(self):

def test_calculate_absolute_drift(self, mocker):
strategy = MockStrategyWithDriftCalculationLogic(
broker= self.backtesting_broker,
broker=self.backtesting_broker,
drift_threshold=Decimal("0.05"),
drift_type=DriftType.ABSOLUTE
)
@@ -650,6 +650,41 @@ def mock_add_positions(self):
assert df["target_value"].tolist() == [Decimal("-500"), Decimal("500")]
assert df["drift"].tolist() == [Decimal("-1.0"), Decimal("0")]

def test_shorting_more_when_price_goes_up_short_something(self, mocker):
strategy = MockStrategyWithDriftCalculationLogic(
broker=self.backtesting_broker,
drift_threshold=Decimal("0.05"),
drift_type=DriftType.ABSOLUTE
)
# patch the strategy so get_last_price returns 110
mocker.patch.object(strategy, "get_last_price", return_value=110.0)

target_weights = {
"AAPL": Decimal("-0.50"),
"USD": Decimal("0.50")
}

def mock_add_positions(self):
self._add_position(
symbol="USD",
is_quote_asset=True,
current_quantity=Decimal("1500"), # original $1000 plus $500 from the short
current_value=Decimal("1500")
)
self._add_position(
symbol="AAPL",
is_quote_asset=False,
current_quantity=Decimal("-5"),
current_value=Decimal("-550")
)

mocker.patch.object(DriftCalculationLogic, "_add_positions", mock_add_positions)
df = strategy.drift_rebalancer_logic.calculate(target_weights=target_weights)

assert df["current_weight"].tolist() == [Decimal('-0.5789473684210526315789473684'), Decimal('1.578947368421052631578947368')]
assert df["target_value"].tolist() == [Decimal("-475"), Decimal("475")]
assert df["drift"].tolist() == [Decimal('0.0789473684210526315789473684'), Decimal('0')]

def test_calculate_absolute_drift_when_we_want_a_100_percent_short_position(self, mocker):
strategy = MockStrategyWithDriftCalculationLogic(
broker=self.backtesting_broker,

0 comments on commit 1337664

Please sign in to comment.