From fff54558943ff66f1377da51cbea0cea8aebc81c Mon Sep 17 00:00:00 2001 From: Camilo1704 Date: Tue, 17 Mar 2020 15:01:16 -0300 Subject: [PATCH] rebalancing days calculation with days in data --- backtester/backtester.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backtester/backtester.py b/backtester/backtester.py index 5862705..f5543d7 100644 --- a/backtester/backtester.py +++ b/backtester/backtester.py @@ -105,12 +105,11 @@ class Backtest: if sma_days: self.stocks_data.sma(sma_days) - rebalancing_days = pd.date_range( - self._stocks_data.start_date, self.stocks_data.end_date, freq=str(rebalance_freq) + - 'BMS') if rebalance_freq else [] - - # Prepend the first day to the rebalancing days - rebalancing_days = pd.DatetimeIndex([self.stocks_data.start_date]).append(rebalancing_days) + dates = pd.DataFrame(self.options_data._data[['quotedate', + 'volume']]).drop_duplicates('quotedate').set_index('quotedate') + rebalancing_days = pd.to_datetime( + dates.groupby(pd.Grouper(freq=str(rebalance_freq) + + 'BMS')).apply(lambda x: x.index.min()).values) if rebalance_freq else [] data_iterator = self._data_iterator(monthly) bar = pyprind.ProgBar(len(stock_dates), bar_char='█')