@@ -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."""
0 commit comments