Ejemplo n.º 1
0
 def test_get_value(self):
     calendar = self.trading_calendar
     session_bar_reader = MinuteResampleSessionBarReader(
         calendar, self.bcolz_future_minute_bar_reader)
     for sid in self.ASSET_FINDER_FUTURE_SIDS:
         expected = EXPECTED_SESSIONS[sid]
         for dt_str, values in expected.iterrows():
             dt = pd.Timestamp(dt_str, tz='UTC')
             for col in OHLCV:
                 result = session_bar_reader.get_value(sid, dt, col)
                 assert_almost_equal(
                     result,
                     values[col],
                     err_msg="sid={0} col={1} dt={2}".format(sid, col, dt))
Ejemplo n.º 2
0
 def test_get_value(self):
     calendar = self.trading_calendar
     session_bar_reader = MinuteResampleSessionBarReader(
         calendar,
         self.bcolz_future_minute_bar_reader
     )
     for sid in self.ASSET_FINDER_FUTURE_SIDS:
         expected = EXPECTED_SESSIONS[sid]
         for dt_str, values in expected.iterrows():
             dt = pd.Timestamp(dt_str, tz='UTC')
             for col in OHLCV:
                 result = session_bar_reader.get_value(sid, dt, col)
                 assert_almost_equal(result,
                                     values[col],
                                     err_msg="sid={0} col={1} dt={2}".
                                     format(sid, col, dt))
Ejemplo n.º 3
0
    def init_class_fixtures(cls):
        super(AssetDispatchSessionBarTestCase, cls).init_class_fixtures()

        readers = {
            Equity:
            ReindexSessionBarReader(cls.trading_calendar,
                                    cls.bcolz_equity_daily_bar_reader,
                                    cls.START_DATE, cls.END_DATE),
            Future:
            MinuteResampleSessionBarReader(
                cls.trading_calendar,
                cls.bcolz_future_minute_bar_reader,
            )
        }
        cls.dispatch_reader = AssetDispatchSessionBarReader(
            cls.trading_calendar, cls.asset_finder, readers)
Ejemplo n.º 4
0
    def test_last_available_dt(self):
        calendar = self.trading_calendar
        session_bar_reader = MinuteResampleSessionBarReader(
            calendar, self.bcolz_future_minute_bar_reader)

        self.assertEqual(self.END_DATE, session_bar_reader.last_available_dt)
Ejemplo n.º 5
0
 def init_instance_fixtures(self):
     super(TestResampleSessionBars, self).init_instance_fixtures()
     self.session_bar_reader = MinuteResampleSessionBarReader(
         self.trading_calendar, self.bcolz_future_minute_bar_reader)
Ejemplo n.º 6
0
class TestResampleSessionBars(WithBcolzFutureMinuteBarReader, ZiplineTestCase):

    TRADING_CALENDAR_STRS = ('us_futures', )
    TRADING_CALENDAR_PRIMARY_CAL = 'us_futures'

    ASSET_FINDER_FUTURE_SIDS = 1001, 1002, 1003, 1004

    START_DATE = pd.Timestamp('2016-03-16', tz='UTC')
    END_DATE = pd.Timestamp('2016-03-17', tz='UTC')
    NUM_SESSIONS = 2

    @classmethod
    def make_futures_info(cls):
        future_dict = {}

        for future_sid in cls.ASSET_FINDER_FUTURE_SIDS:
            future_dict[future_sid] = {
                'multiplier': 1000,
                'exchange': 'CME',
                'root_symbol': "ABC"
            }

        return pd.DataFrame.from_dict(future_dict, orient='index')

    @classmethod
    def make_future_minute_bar_data(cls):
        for sid in cls.ASSET_FINDER_FUTURE_SIDS:
            frame = FUTURE_CASES[sid]
            yield sid, frame

    def init_instance_fixtures(self):
        super(TestResampleSessionBars, self).init_instance_fixtures()
        self.session_bar_reader = MinuteResampleSessionBarReader(
            self.trading_calendar, self.bcolz_future_minute_bar_reader)

    def test_resample(self):
        calendar = self.trading_calendar
        for sid in self.ASSET_FINDER_FUTURE_SIDS:
            case_frame = FUTURE_CASES[sid]
            first = calendar.minute_to_session_label(case_frame.index[0])
            last = calendar.minute_to_session_label(case_frame.index[-1])
            result = self.session_bar_reader.load_raw_arrays(
                OHLCV, first, last, [sid])
            for i, field in enumerate(OHLCV):
                assert_almost_equal(EXPECTED_SESSIONS[sid][[field]],
                                    result[i],
                                    err_msg="sid={0} field={1}".format(
                                        sid, field))

    def test_sessions(self):
        sessions = self.session_bar_reader.sessions

        self.assertEqual(self.NUM_SESSIONS, len(sessions))
        self.assertEqual(self.START_DATE, sessions[0])
        self.assertEqual(self.END_DATE, sessions[-1])

    def test_last_available_dt(self):
        calendar = self.trading_calendar
        session_bar_reader = MinuteResampleSessionBarReader(
            calendar, self.bcolz_future_minute_bar_reader)

        self.assertEqual(self.END_DATE, session_bar_reader.last_available_dt)

    def test_get_value(self):
        calendar = self.trading_calendar
        session_bar_reader = MinuteResampleSessionBarReader(
            calendar, self.bcolz_future_minute_bar_reader)
        for sid in self.ASSET_FINDER_FUTURE_SIDS:
            expected = EXPECTED_SESSIONS[sid]
            for dt_str, values in expected.iterrows():
                dt = pd.Timestamp(dt_str, tz='UTC')
                for col in OHLCV:
                    result = session_bar_reader.get_value(sid, dt, col)
                    assert_almost_equal(
                        result,
                        values[col],
                        err_msg="sid={0} col={1} dt={2}".format(sid, col, dt))

    def test_first_trading_day(self):
        self.assertEqual(self.START_DATE,
                         self.session_bar_reader.first_trading_day)

    def test_get_last_traded_dt(self):
        future = self.asset_finder.retrieve_asset(
            self.ASSET_FINDER_FUTURE_SIDS[0])

        self.assertEqual(
            self.trading_calendar.previous_session_label(self.END_DATE),
            self.session_bar_reader.get_last_traded_dt(future, self.END_DATE))
Ejemplo n.º 7
0
 def init_instance_fixtures(self):
     super(TestResampleSessionBars, self).init_instance_fixtures()
     self.session_bar_reader = MinuteResampleSessionBarReader(
         self.trading_calendar,
         self.bcolz_future_minute_bar_reader
     )
Ejemplo n.º 8
0
class TestResampleSessionBars(WithBcolzFutureMinuteBarReader,
                              ZiplineTestCase):

    TRADING_CALENDAR_STRS = ('us_futures',)
    TRADING_CALENDAR_PRIMARY_CAL = 'us_futures'

    ASSET_FINDER_FUTURE_SIDS = 1001, 1002, 1003, 1004

    START_DATE = pd.Timestamp('2016-03-16', tz='UTC')
    END_DATE = pd.Timestamp('2016-03-17', tz='UTC')
    NUM_SESSIONS = 2

    @classmethod
    def make_futures_info(cls):
        future_dict = {}

        for future_sid in cls.ASSET_FINDER_FUTURE_SIDS:
            future_dict[future_sid] = {
                'multiplier': 1000,
                'exchange': 'CME',
                'root_symbol': "ABC"
            }

        return pd.DataFrame.from_dict(future_dict, orient='index')

    @classmethod
    def make_future_minute_bar_data(cls):
        for sid in cls.ASSET_FINDER_FUTURE_SIDS:
            frame = FUTURE_CASES[sid]
            yield sid, frame

    def init_instance_fixtures(self):
        super(TestResampleSessionBars, self).init_instance_fixtures()
        self.session_bar_reader = MinuteResampleSessionBarReader(
            self.trading_calendar,
            self.bcolz_future_minute_bar_reader
        )

    def test_resample(self):
        calendar = self.trading_calendar
        for sid in self.ASSET_FINDER_FUTURE_SIDS:
            case_frame = FUTURE_CASES[sid]
            first = calendar.minute_to_session_label(
                case_frame.index[0])
            last = calendar.minute_to_session_label(
                case_frame.index[-1])
            result = self.session_bar_reader.load_raw_arrays(
                OHLCV, first, last, [sid])
            for i, field in enumerate(OHLCV):
                assert_almost_equal(
                    EXPECTED_SESSIONS[sid][[field]],
                    result[i],
                    err_msg="sid={0} field={1}".format(sid, field))

    def test_sessions(self):
        sessions = self.session_bar_reader.sessions

        self.assertEqual(self.NUM_SESSIONS, len(sessions))
        self.assertEqual(self.START_DATE, sessions[0])
        self.assertEqual(self.END_DATE, sessions[-1])

    def test_last_available_dt(self):
        calendar = self.trading_calendar
        session_bar_reader = MinuteResampleSessionBarReader(
            calendar,
            self.bcolz_future_minute_bar_reader
        )

        self.assertEqual(self.END_DATE, session_bar_reader.last_available_dt)

    def test_get_value(self):
        calendar = self.trading_calendar
        session_bar_reader = MinuteResampleSessionBarReader(
            calendar,
            self.bcolz_future_minute_bar_reader
        )
        for sid in self.ASSET_FINDER_FUTURE_SIDS:
            expected = EXPECTED_SESSIONS[sid]
            for dt_str, values in expected.iterrows():
                dt = pd.Timestamp(dt_str, tz='UTC')
                for col in OHLCV:
                    result = session_bar_reader.get_value(sid, dt, col)
                    assert_almost_equal(result,
                                        values[col],
                                        err_msg="sid={0} col={1} dt={2}".
                                        format(sid, col, dt))

    def test_first_trading_day(self):
        self.assertEqual(self.START_DATE,
                         self.session_bar_reader.first_trading_day)

    def test_get_last_traded_dt(self):
        future = self.asset_finder.retrieve_asset(
            self.ASSET_FINDER_FUTURE_SIDS[0]
        )

        self.assertEqual(
            self.trading_calendar.previous_session_label(self.END_DATE),
            self.session_bar_reader.get_last_traded_dt(future, self.END_DATE)
        )