Beispiel #1
0
    def __init__(self, start=start_default, end=end_default):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            _all_days = date_range(start, end, freq=self.day, tz='UTC')

        self._lunch_break_starts = days_at_time(_all_days, lunch_break_start,
                                                self.tz, 0)
        self._lunch_break_ends = days_at_time(_all_days, lunch_break_end,
                                              self.tz, 0)

        TradingCalendar.__init__(self, start=start_default, end=end_default)
Beispiel #2
0
    def verify_bts_during_session(self, bts_time, bts_session_times, bts_idx):
        def _check_session_bts_during(session_label, events, bts_dt):
            minutes = self.nyse_calendar.minutes_for_session(session_label)

            self.assertEqual(393, len(events))

            self.assertEqual(events[0], (session_label, SESSION_START))

            for i in range(1, bts_idx):
                self.assertEqual(events[i], (minutes[i - 1], BAR))

            self.assertEqual(events[bts_idx],
                             (bts_dt, BEFORE_TRADING_START_BAR))

            for i in range(bts_idx + 1, 391):
                self.assertEqual(events[i], (minutes[i - 2], BAR))

            self.assertEqual(events[392], (minutes[-1], SESSION_END))

        clock = MinuteSimulationClock(
            self.sessions, self.opens, self.closes,
            days_at_time(self.sessions, bts_time, "US/Eastern"), False)

        all_events = list(clock)

        _check_session_bts_during(self.sessions[0], all_events[0:393],
                                  bts_session_times[0])

        _check_session_bts_during(self.sessions[1], all_events[393:786],
                                  bts_session_times[1])

        _check_session_bts_during(self.sessions[2], all_events[786:],
                                  bts_session_times[2])
Beispiel #3
0
    def test_bts_before_session(self):
        clock = MinuteSimulationClock(
            self.sessions, self.opens, self.closes,
            days_at_time(self.sessions, time(6, 17), "US/Eastern"), False)

        all_events = list(clock)

        def _check_session_bts_first(session_label, events, bts_dt):
            minutes = self.nyse_calendar.minutes_for_session(session_label)

            self.assertEqual(393, len(events))

            self.assertEqual(events[0], (session_label, SESSION_START))
            self.assertEqual(events[1], (bts_dt, BEFORE_TRADING_START_BAR))
            for i in range(2, 392):
                self.assertEqual(events[i], (minutes[i - 2], BAR))
            self.assertEqual(events[392], (minutes[-1], SESSION_END))

        _check_session_bts_first(
            self.sessions[0], all_events[0:393],
            pd.Timestamp("2016-07-15 6:17", tz='US/Eastern'))

        _check_session_bts_first(
            self.sessions[1], all_events[393:786],
            pd.Timestamp("2016-07-18 6:17", tz='US/Eastern'))

        _check_session_bts_first(
            self.sessions[2], all_events[786:],
            pd.Timestamp("2016-07-19 6:17", tz='US/Eastern'))
Beispiel #4
0
    def test_bts_after_session(self):
        clock = MinuteSimulationClock(
            self.sessions, self.opens, self.closes,
            days_at_time(self.sessions, time(19, 5), "US/Eastern"), False)

        all_events = list(clock)

        # since 19:05 Eastern is after the NYSE is closed, we don't emit
        # BEFORE_TRADING_START.  therefore, each day has SESSION_START,
        # 390 BARs, and then SESSION_END

        def _check_session_bts_after(session_label, events):
            minutes = self.nyse_calendar.minutes_for_session(session_label)

            self.assertEqual(392, len(events))
            self.assertEqual(events[0], (session_label, SESSION_START))

            for i in range(1, 391):
                self.assertEqual(events[i], (minutes[i - 1], BAR))

            self.assertEqual(events[-1], (minutes[389], SESSION_END))

        for i in range(0, 2):
            _check_session_bts_after(self.sessions[i],
                                     all_events[(i * 392):((i + 1) * 392)])
    def test_before_trading_start(self, test_name, num_days, freq,
                                  emission_rate):
        params = factory.create_simulation_parameters(
            num_days=num_days, data_frequency=freq,
            emission_rate=emission_rate)

        def fake_benchmark(self, dt):
            return 0.01

        with patch.object(BenchmarkSource, "get_value",
                          self.fake_minutely_benchmark):
            algo = BeforeTradingAlgorithm(sim_params=params, env=self.env)
            algo.run(FakeDataPortal(self.env))

            self.assertEqual(
                len(algo.perf_tracker.sim_params.sessions),
                num_days
            )

            bts_minutes = days_at_time(
                params.sessions, time(8, 45), "US/Eastern"
            )

            self.assertTrue(
                bts_minutes.equals(
                    pd.DatetimeIndex(algo.before_trading_at)
                ),
                "Expected %s but was %s." % (params.sessions,
                                             algo.before_trading_at))
Beispiel #6
0
    def test_current_session(self):
        regular_minutes = self.trading_calendar.minutes_for_sessions_in_range(
            self.equity_minute_bar_days[0], self.equity_minute_bar_days[-1])

        bts_minutes = days_at_time(self.equity_minute_bar_days, time(8, 45),
                                   "US/Eastern")

        # some other non-market-minute
        three_oh_six_am_minutes = days_at_time(self.equity_minute_bar_days,
                                               time(3, 6), "US/Eastern")

        all_minutes = [regular_minutes, bts_minutes, three_oh_six_am_minutes]
        for minute in list(concat(all_minutes)):
            bar_data = self.create_bardata(lambda: minute)

            self.assertEqual(
                self.trading_calendar.minute_to_session_label(minute),
                bar_data.current_session)
    def __init__(self, start=start_default, end=end_default):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            _all_days = date_range(start, end, freq=self.day, tz='UTC')

        self._lunch_break_starts = days_at_time(_all_days, lunch_break_start, self.tz, 0)
        self._lunch_break_ends = days_at_time(_all_days, lunch_break_end, self.tz, 0)

        TradingCalendar.__init__(self, start=start_default, end=end_default)

        self.schedule = pd.DataFrame(
            index=_all_days,
            columns=['market_open', 'market_close', 'lunch_break_start', 'lunch_break_end'],
            data={
                'market_open': self._opens,
                'market_close': self._closes,
                'lunch_break_start': self._lunch_break_starts,
                'lunch_break_end': self._lunch_break_ends
            },
            dtype='datetime64[ns]',
        )
Beispiel #8
0
 def test_days_at_time(self, day, day_offset, time_offset, tz, expected):
     days = pd.DatetimeIndex([pd.Timestamp(day, tz=tz)])
     result = days_at_time(days, time_offset, tz, day_offset)[0]
     expected = pd.Timestamp(expected, tz=tz).tz_convert('UTC')
     self.assertEqual(result, expected)