Skip to content

Commit d9d48f6

Browse files
committed
style: apply black formatting to backtest and test files
1 parent 10de107 commit d9d48f6

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/quant_research_starter/backtest/vectorized.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ def run(self, weight_scheme: str = "rank") -> Dict:
7777
for date in returns_df.index:
7878
if self._should_rebalance(date, prev_rebalance_date):
7979
# Rebalance: compute new target weights
80-
current_weights = self._calculate_weights(aligned_signals.loc[date], weight_scheme)
80+
current_weights = self._calculate_weights(
81+
aligned_signals.loc[date], weight_scheme
82+
)
8183
prev_rebalance_date = date
8284

8385
# Append current weights (maintain between rebalances)
8486
weights_list.append(current_weights)
8587

86-
weights = pd.DataFrame(weights_list, index=returns_df.index, columns=self.prices.columns).fillna(0.0)
88+
weights = pd.DataFrame(
89+
weights_list, index=returns_df.index, columns=self.prices.columns
90+
).fillna(0.0)
8791

8892
# Previous day weights for PnL calculation
8993
weights_prev = weights.shift(1).fillna(0.0)
@@ -114,7 +118,9 @@ def run(self, weight_scheme: str = "rank") -> Dict:
114118

115119
return self._generate_results()
116120

117-
def _should_rebalance(self, date: pd.Timestamp, prev_rebalance_date: Optional[pd.Timestamp] = None) -> bool:
121+
def _should_rebalance(
122+
self, date: pd.Timestamp, prev_rebalance_date: Optional[pd.Timestamp] = None
123+
) -> bool:
118124
"""Check if we should rebalance on given date.
119125
120126
Args:
@@ -133,15 +139,21 @@ def _should_rebalance(self, date: pd.Timestamp, prev_rebalance_date: Optional[pd
133139
return True
134140
elif self.rebalance_freq == "W":
135141
# Weekly rebalancing - rebalance if week changed
136-
return date.isocalendar()[1] != prev_rebalance_date.isocalendar()[1] or \
137-
date.year != prev_rebalance_date.year
142+
return (
143+
date.isocalendar()[1] != prev_rebalance_date.isocalendar()[1]
144+
or date.year != prev_rebalance_date.year
145+
)
138146
elif self.rebalance_freq == "M":
139147
# Monthly rebalancing - rebalance if month changed
140-
return date.month != prev_rebalance_date.month or \
141-
date.year != prev_rebalance_date.year
148+
return (
149+
date.month != prev_rebalance_date.month
150+
or date.year != prev_rebalance_date.year
151+
)
142152
else:
143-
raise ValueError(f"Unsupported rebalance frequency: {self.rebalance_freq}. "
144-
f"Supported frequencies: 'D' (daily), 'W' (weekly), 'M' (monthly)")
153+
raise ValueError(
154+
f"Unsupported rebalance frequency: {self.rebalance_freq}. "
155+
f"Supported frequencies: 'D' (daily), 'W' (weekly), 'M' (monthly)"
156+
)
145157

146158
def _calculate_weights(self, signals: pd.Series, scheme: str) -> pd.Series:
147159
"""Convert signals to portfolio weights."""

tests/test_backtest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,22 @@ def test_rebalance_reduces_turnover(self, sample_data):
186186
prices, signals = sample_data
187187

188188
# Daily rebalancing
189-
backtest_daily = VectorizedBacktest(prices, signals, rebalance_freq="D", transaction_cost=0.001)
189+
backtest_daily = VectorizedBacktest(
190+
prices, signals, rebalance_freq="D", transaction_cost=0.001
191+
)
190192
results_daily = backtest_daily.run()
191193

192194
# Monthly rebalancing
193-
backtest_monthly = VectorizedBacktest(prices, signals, rebalance_freq="M", transaction_cost=0.001)
195+
backtest_monthly = VectorizedBacktest(
196+
prices, signals, rebalance_freq="M", transaction_cost=0.001
197+
)
194198
results_monthly = backtest_monthly.run()
195199

196200
# Count position changes as proxy for turnover
197201
daily_changes = (results_daily["positions"].diff().abs().sum(axis=1) > 0).sum()
198-
monthly_changes = (results_monthly["positions"].diff().abs().sum(axis=1) > 0).sum()
202+
monthly_changes = (
203+
results_monthly["positions"].diff().abs().sum(axis=1) > 0
204+
).sum()
199205

200206
# Monthly should have fewer rebalances
201207
assert monthly_changes < daily_changes

0 commit comments

Comments
 (0)