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 shorting tests #680

Merged
merged 2 commits into from
Jan 10, 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
2 changes: 0 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down
41 changes: 38 additions & 3 deletions tests/test_drift_rebalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand Down
Loading