Exemple #1
0
    def _create_rebalance_event_times(self):
        """
        Creates the list of rebalance timestamps used to determine when
        to execute the quant trading strategy throughout the backtest.

        Returns
        -------
        `List[pd.Timestamp]`
            The list of rebalance timestamps.
        """
        if self.rebalance == 'buy_and_hold':
            rebalancer = BuyAndHoldRebalance(self.start_dt)
        elif self.rebalance == 'daily':
            rebalancer = DailyRebalance(
                self.start_dt, self.end_dt
            )
        elif self.rebalance == 'weekly':
            rebalancer = WeeklyRebalance(
                self.start_dt, self.end_dt, self.rebalance_weekday
            )
        elif self.rebalance == 'end_of_month':
            rebalancer = EndOfMonthRebalance(self.start_dt, self.end_dt)
        else:
            raise ValueError(
                'Unknown rebalance frequency "%s" provided.' % self.rebalance
            )
        return rebalancer.rebalances
def test_monthly_rebalance(
    start_date, end_date, pre_market, expected_dates, expected_time
):
    """
    Checks that the end of month (business day) rebalance provides
    the correct datetimes for the provided range.
    """
    sd = pd.Timestamp(start_date, tz=pytz.UTC)
    ed = pd.Timestamp(end_date, tz=pytz.UTC)

    reb = EndOfMonthRebalance(
        start_dt=sd, end_dt=ed, pre_market=pre_market
    )

    actual_datetimes = reb._generate_rebalances()

    expected_datetimes = [
        pd.Timestamp('%s %s' % (expected_date, expected_time), tz=pytz.UTC)
        for expected_date in expected_dates
    ]

    assert actual_datetimes == expected_datetimes