예제 #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_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)
예제 #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.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)
예제 #4
0
 def test_NthTradingDayOfWeek_day_zero(self):
     """
     Test that we don't blow up when trying to call week_start's
     should_trigger on the first day of a trading environment.
     """
     cal = get_calendar('NYSE')
     rule = NthTradingDayOfWeek(0)
     rule.cal = cal
     self.assertTrue(rule.should_trigger(self.nyse_cal.all_trading_days[0]))
예제 #5
0
 def test_NthTradingDayOfWeek_day_zero(self):
     """
     Test that we don't blow up when trying to call week_start's
     should_trigger on the first day of a trading environment.
     """
     rule = NthTradingDayOfWeek(0)
     rule.cal = self.cal
     first_open = self.cal.open_and_close_for_session(self.cal.all_sessions[0])
     self.assertTrue(first_open)
예제 #6
0
 def test_NthTradingDayOfWeek_day_zero(self):
     """
     Test that we don't blow up when trying to call week_start's
     should_trigger on the first day of a trading environment.
     """
     rule = NthTradingDayOfWeek(0)
     rule.cal = self.cal
     first_open = self.cal.open_and_close_for_session(
         self.cal.all_sessions[0])
     self.assertTrue(first_open)
예제 #7
0
 def test_NthTradingDayOfWeek_day_zero(self):
     """
     Test that we don't blow up when trying to call week_start's
     should_trigger on the first day of a trading environment.
     """
     cal = get_calendar('NYSE')
     rule = NthTradingDayOfWeek(0)
     rule.cal = cal
     self.assertTrue(
         rule.should_trigger(self.nyse_cal.all_trading_days[0])
     )
예제 #8
0
    def test_NthTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NthTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        prev_day = self.sept_week[0].date()
        n_tdays = 0
        for m in self.sept_week:
            if prev_day < m.date():
                n_tdays += 1
                prev_day = m.date()

            if should_trigger(m):
                self.assertEqual(n_tdays, n)
            else:
                self.assertNotEqual(n_tdays, n)
예제 #9
0
    def test_NthTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NthTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        prev_day = self.sept_week[0].date()
        n_tdays = 0
        for m in self.sept_week:
            if prev_day < m.date():
                n_tdays += 1
                prev_day = m.date()

            if should_trigger(m):
                self.assertEqual(n_tdays, n)
            else:
                self.assertNotEqual(n_tdays, n)
예제 #10
0
    def test_NthTradingDayOfWeek(self):
        for n in range(MAX_WEEK_RANGE):
            rule = NthTradingDayOfWeek(n)
            rule.cal = self.cal
            should_trigger = rule.should_trigger
            prev_period = self.cal.minute_to_session_label(self.sept_week[0])
            n_tdays = 0
            for minute in self.sept_week:
                period = self.cal.minute_to_session_label(minute)

                if prev_period < period:
                    n_tdays += 1
                    prev_period = period

                if should_trigger(minute):
                    self.assertEqual(n_tdays, n)
                else:
                    self.assertNotEqual(n_tdays, n)
예제 #11
0
    def test_NthTradingDayOfWeek(self):
        for n in range(MAX_WEEK_RANGE):
            rule = NthTradingDayOfWeek(n)
            rule.cal = self.cal
            should_trigger = rule.should_trigger
            prev_period = self.cal.minute_to_session_label(self.sept_week[0])
            n_tdays = 0
            for minute in self.sept_week:
                period = self.cal.minute_to_session_label(minute)

                if prev_period < period:
                    n_tdays += 1
                    prev_period = period

                if should_trigger(minute):
                    self.assertEqual(n_tdays, n)
                else:
                    self.assertNotEqual(n_tdays, n)
예제 #12
0
    def test_NthTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NthTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        prev_period = self.nyse_cal.minute_to_session_label(self.sept_week[0])
        n_tdays = 0
        for minute in self.sept_week:
            period = self.nyse_cal.minute_to_session_label(minute,
                                                           direction="none")

            if prev_period < period:
                n_tdays += 1
                prev_period = period

            if should_trigger(minute):
                self.assertEqual(n_tdays, n)
            else:
                self.assertNotEqual(n_tdays, n)
예제 #13
0
    def test_NthTradingDayOfWeek(self, n):
        cal = get_calendar('NYSE')
        rule = NthTradingDayOfWeek(n)
        rule.cal = cal
        should_trigger = rule.should_trigger
        prev_period = self.nyse_cal.minute_to_session_label(self.sept_week[0])
        n_tdays = 0
        for minute in self.sept_week:
            period = self.nyse_cal.minute_to_session_label(
                minute, direction="none"
            )

            if prev_period < period:
                n_tdays += 1
                prev_period = period

            if should_trigger(minute):
                self.assertEqual(n_tdays, n)
            else:
                self.assertNotEqual(n_tdays, n)
예제 #14
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)
예제 #15
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)