Пример #1
0
    def test_week_and_time_composed_rule(self, rule_type):
        week_rule = NthTradingDayOfWeek(0) if rule_type == 'week_start' else \
            NDaysBeforeLastTradingDayOfWeek(4)
        time_rule = AfterOpen(minutes=60)

        week_rule.cal = self.cal
        time_rule.cal = self.cal

        composed_rule = week_rule & time_rule

        should_trigger = composed_rule.should_trigger

        week_minutes = self.cal.minutes_for_sessions_in_range(
            pd.Timestamp("2014-01-06", tz='UTC'),
            pd.Timestamp("2014-01-10", tz='UTC'))

        dt = pd.Timestamp('2014-01-06 14:30:00', tz='UTC')
        trigger_day_offset = 0
        trigger_minute_offset = 60
        n_triggered = 0

        for m in week_minutes:
            if should_trigger(m):
                self.assertEqual(
                    m, dt + timedelta(days=trigger_day_offset) +
                    timedelta(minutes=trigger_minute_offset))
                n_triggered += 1

        self.assertEqual(n_triggered, 1)
Пример #2
0
    def test_NDaysBeforeLastTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NDaysBeforeLastTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        for m in self.sept_week:
            if should_trigger(m):
                n_tdays = 0
                date = m.to_datetime().date()
                next_date = self.nyse_cal.next_trading_day(date)
                while next_date.weekday() > date.weekday():
                    date = next_date
                    next_date = self.nyse_cal.next_trading_day(date)
                    n_tdays += 1

                self.assertEqual(n_tdays, n)
Пример #3
0
    def test_week_and_time_composed_rule(self, type):
        week_rule = NthTradingDayOfWeek(0) if type == 'week_start' else \
            NDaysBeforeLastTradingDayOfWeek(4)
        time_rule = AfterOpen(minutes=60)

        week_rule.cal = self.nyse_cal
        time_rule.cal = self.nyse_cal

        composed_rule = week_rule & time_rule

        should_trigger = composed_rule.should_trigger

        week_minutes = self.nyse_cal.trading_minutes_for_days_in_range(
            datetime.date(year=2014, month=1, day=6),
            datetime.date(year=2014, month=1, day=10))

        dt = pd.Timestamp('2014-01-06 14:30:00', tz='UTC')
        trigger_day_offset = 0
        trigger_minute_offset = 60
        n_triggered = 0

        for m in week_minutes:
            if should_trigger(m):
                self.assertEqual(
                    m, dt + timedelta(days=trigger_day_offset) +
                    timedelta(minutes=trigger_minute_offset))
                n_triggered += 1

        self.assertEqual(n_triggered, 1)
Пример #4
0
    def test_NDaysBeforeLastTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NDaysBeforeLastTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        for m in self.sept_week:
            if should_trigger(m):
                n_tdays = 0
                date = m.to_datetime().date()
                next_date = self.nyse_cal.next_trading_day(date)
                while next_date.weekday() > date.weekday():
                    date = next_date
                    next_date = self.nyse_cal.next_trading_day(date)
                    n_tdays += 1

                self.assertEqual(n_tdays, n)
Пример #5
0
    def test_NDaysBeforeLastTradingDayOfWeek(self):
        for n in range(MAX_WEEK_RANGE):
            rule = NDaysBeforeLastTradingDayOfWeek(n)
            rule.cal = self.cal
            should_trigger = rule.should_trigger
            for minute in self.sept_week:
                if should_trigger(minute):
                    n_tdays = 0
                    session = self.cal.minute_to_session_label(minute, direction="none")
                    next_session = self.cal.next_session_label(session)
                    while next_session.dayofweek > session.dayofweek:
                        session = next_session
                        next_session = self.cal.next_session_label(session)
                        n_tdays += 1

                    self.assertEqual(n_tdays, n)
Пример #6
0
    def test_NDaysBeforeLastTradingDayOfWeek(self):
        for n in range(MAX_WEEK_RANGE):
            rule = NDaysBeforeLastTradingDayOfWeek(n)
            rule.cal = self.cal
            should_trigger = rule.should_trigger
            for minute in self.sept_week:
                if should_trigger(minute):
                    n_tdays = 0
                    session = self.cal.minute_to_session_label(
                        minute, direction="none")
                    next_session = self.cal.next_session_label(session)
                    while next_session.dayofweek > session.dayofweek:
                        session = next_session
                        next_session = self.cal.next_session_label(session)
                        n_tdays += 1

                    self.assertEqual(n_tdays, n)
Пример #7
0
    def test_NDaysBeforeLastTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NDaysBeforeLastTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        for minute in self.sept_week:
            if should_trigger(minute):
                n_tdays = 0
                session = self.nyse_cal.minute_to_session_label(
                    minute, direction="none")
                next_session = self.nyse_cal.next_session_label(session)
                while next_session.dayofweek > session.dayofweek:
                    session = next_session
                    next_session = self.nyse_cal.next_session_label(session)
                    n_tdays += 1

                self.assertEqual(n_tdays, n)
Пример #8
0
    def test_NDaysBeforeLastTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NDaysBeforeLastTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        for minute in self.sept_week:
            if should_trigger(minute):
                n_tdays = 0
                session = self.nyse_cal.minute_to_session_label(
                    minute,
                    direction="none"
                )
                next_session = self.nyse_cal.next_session_label(session)
                while next_session.dayofweek > session.dayofweek:
                    session = next_session
                    next_session = self.nyse_cal.next_session_label(session)
                    n_tdays += 1

                self.assertEqual(n_tdays, n)
Пример #9
0
 def test_NDaysBeforeLastTradingDayOfWeek(self, n):
     should_trigger = partial(
         NDaysBeforeLastTradingDayOfWeek(n).should_trigger, env=self.env
     )
     first_of_week = self.sept_week[0]
     n_triggered = 0
     for m in self.sept_week:
         if should_trigger(m):
             n_triggered += 1
             self.assertEqual(m, first_of_week + timedelta(days=4-n))
     self.assertEqual(n_triggered, 1)
Пример #10
0
    def test_NDaysBeforeLastTradingDayOfWeek(self, n):
        should_trigger = NDaysBeforeLastTradingDayOfWeek(n).should_trigger
        for m in self.sept_week:
            if should_trigger(m):
                n_tdays = 0
                date = m.to_datetime().date()
                next_date = self.env.next_trading_day(date)
                while next_date.weekday() > date.weekday():
                    date = next_date
                    next_date = self.env.next_trading_day(date)
                    n_tdays += 1

                self.assertEqual(n_tdays, n)
Пример #11
0
 def week_end(days_offset=0):
     return NDaysBeforeLastTradingDayOfWeek(n=days_offset)
Пример #12
0
    def test_edge_cases_for_TradingDayOfWeek(self):
        """
        Test that we account for midweek holidays. Monday 01/20 is a holiday.
        Ensure that the trigger date for that week is adjusted
        appropriately, or thrown out if not enough trading days. Also, test
        that if we start the simulation on a day where we miss the trigger
        for that week, that the trigger is recalculated for next week.
        """

        #    December 2013
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30 31

        #    January 2014
        # Su Mo Tu We Th Fr Sa
        #           1  2  3  4
        #  5  6  7  8  9 10 11
        # 12 13 14 15 16 17 18
        # 19 20 21 22 23 24 25
        # 26 27 28 29 30 31

        # Include last day of 2013 to exercise case where the first day of a
        # week is in the previous year.

        # `week_start`
        rule = NthTradingDayOfWeek(0)
        rule.cal = self.cal

        expected = {
            # A Monday before the New Year.
            '2013-12-30': True,
            # Should not trigger on day after.
            '2013-12-31': False,
            # Should not trigger at market open of 1-2, a Thursday,
            # day after a holiday.
            '2014-01-02': False,
            # Test that the next Monday, which is at a start of a
            # 'normal' week successfully triggers.
            '2014-01-06': True,
            # Test around a Monday holiday, MLK day, to exercise week
            # start on a Tuesday.
            # MLK is 2014-01-20 in 2014.
            '2014-01-21': True,
            # Should not trigger at market open of 01-22, a Wednesday.
            '2014-01-22': False,
        }

        results = {
            x: rule.should_trigger(self.cal.next_open(T(x)))
            for x in expected.keys()
        }

        self.assertEquals(expected, results)

        # Ensure that offset from start of week also works around edge cases.
        rule = NthTradingDayOfWeek(1)
        rule.cal = self.cal

        expected = {
            # Should trigger at market open of 12-31, day after week start.
            '2013-12-31': True,
            # Should not trigger at market open of 1-2, a Thursday,
            # day after a holiday.
            '2014-01-02': False,
            # Test around a Monday holiday, MLK day, to exercise
            # week start on a Tuesday.
            # MLK is 2014-01-20 in 2014.
            # Should trigger at market open, two days after Monday hoilday.
            '2014-01-22': True,
            # Should not trigger at market open of 01-23, a Thursday.
            '2014-01-23': False,
        }

        results = {
            x: rule.should_trigger(self.cal.next_open(T(x)))
            for x in expected.keys()
        }

        self.assertEquals(expected, results)

        # `week_end`
        rule = NDaysBeforeLastTradingDayOfWeek(0)
        rule.cal = self.cal

        expected = {
            # Should trigger at market open of the Friday of the first week.
            '2014-01-03': True,
            # Should not trigger day before the end of the week.
            '2014-01-02': False,
            # Test around a Monday holiday, MLK day, to exercise week
            # start on a Tuesday.
            # MLK is 2014-01-20 in 2014.
            # Should trigger at market open, on Friday after the holiday.
            '2014-01-24': True,
            # Should not trigger at market open of 01-23, a Thursday.
            '2014-01-23': False,
        }

        results = {
            x: rule.should_trigger(self.cal.next_open(T(x)))
            for x in expected.keys()
        }

        self.assertEquals(expected, results)