Example #1
0
    def init_class_fixtures(cls):
        super().init_class_fixtures()

        readers = {
            Equity:
            ReindexMinuteBarReader(cls.trading_calendar,
                                   cls.bcolz_equity_minute_bar_reader,
                                   cls.START_DATE, cls.END_DATE),
            Future:
            cls.bcolz_future_minute_bar_reader
        }
        cls.dispatch_reader = AssetDispatchMinuteBarReader(
            cls.trading_calendar, cls.asset_finder, readers)
Example #2
0
    def __init__(self,
                 asset_finder,
                 trading_calendar,
                 first_trading_day,
                 equity_daily_reader=None,
                 equity_minute_reader=None,
                 future_daily_reader=None,
                 future_minute_reader=None,
                 adjustment_reader=None,
                 last_available_session=None,
                 last_available_minute=None,
                 minute_history_prefetch_length=_DEF_M_HIST_PREFETCH,
                 daily_history_prefetch_length=_DEF_D_HIST_PREFETCH):

        self.trading_calendar = trading_calendar
        self.asset_finder = asset_finder

        self._adjustment_reader = adjustment_reader

        # caches of sid -> adjustment list
        self._splits_dict = {}
        self._mergers_dict = {}
        self._dividends_dict = {}

        # Cache of sid -> the first trading day of an asset.
        self._asset_start_dates = {}
        self._asset_end_dates = {}

        # Handle extra sources, like Fetcher.
        self._augmented_sources_map = {}
        self._extra_source_df = None

        self._first_trading_session = first_trading_day

        _last_sessions = [
            r.last_available_dt
            for r in [equity_daily_reader, future_daily_reader]
            if r is not None
        ]
        if _last_sessions:
            self._last_trading_session = min(_last_sessions)
        else:
            self._last_trading_session = None

        aligned_equity_minute_reader = self._ensure_reader_aligned(
            equity_minute_reader)
        aligned_equity_session_reader = self._ensure_reader_aligned(
            equity_daily_reader)
        aligned_future_minute_reader = self._ensure_reader_aligned(
            future_minute_reader)
        aligned_future_session_reader = self._ensure_reader_aligned(
            future_daily_reader)

        self._roll_finders = {
            'calendar':
            CalendarRollFinder(self.trading_calendar, self.asset_finder),
        }

        aligned_minute_readers = {}
        aligned_session_readers = {}

        if aligned_equity_minute_reader is not None:
            aligned_minute_readers[Equity] = aligned_equity_minute_reader
        if aligned_equity_session_reader is not None:
            aligned_session_readers[Equity] = aligned_equity_session_reader

        if aligned_future_minute_reader is not None:
            aligned_minute_readers[Future] = aligned_future_minute_reader
            aligned_minute_readers[ContinuousFuture] = \
                ContinuousFutureMinuteBarReader(
                    aligned_future_minute_reader,
                    self._roll_finders,
                )

        if aligned_future_session_reader is not None:
            aligned_session_readers[Future] = aligned_future_session_reader
            self._roll_finders['volume'] = VolumeRollFinder(
                self.trading_calendar,
                self.asset_finder,
                aligned_future_session_reader,
            )
            aligned_session_readers[ContinuousFuture] = \
                ContinuousFutureSessionBarReader(
                    aligned_future_session_reader,
                    self._roll_finders,
                )

        _dispatch_minute_reader = AssetDispatchMinuteBarReader(
            self.trading_calendar,
            self.asset_finder,
            aligned_minute_readers,
            last_available_minute,
        )

        _dispatch_session_reader = AssetDispatchSessionBarReader(
            self.trading_calendar,
            self.asset_finder,
            aligned_session_readers,
            last_available_session,
        )

        self._pricing_readers = {
            'minute': _dispatch_minute_reader,
            'daily': _dispatch_session_reader,
        }

        self._daily_aggregator = DailyHistoryAggregator(
            self.trading_calendar.schedule.market_open,
            _dispatch_minute_reader, self.trading_calendar)
        self._history_loader = DailyHistoryLoader(
            self.trading_calendar,
            _dispatch_session_reader,
            self._adjustment_reader,
            self.asset_finder,
            self._roll_finders,
            prefetch_length=daily_history_prefetch_length,
        )
        self._minute_history_loader = MinuteHistoryLoader(
            self.trading_calendar,
            _dispatch_minute_reader,
            self._adjustment_reader,
            self.asset_finder,
            self._roll_finders,
            prefetch_length=minute_history_prefetch_length,
        )

        self._first_trading_day = first_trading_day

        # Get the first trading minute
        self._first_trading_minute, _ = (
            self.trading_calendar.open_and_close_for_session(
                self._first_trading_day)
            if self._first_trading_day is not None else (None, None))

        # Store the locs of the first day and first minute
        self._first_trading_day_loc = (
            self.trading_calendar.all_sessions.get_loc(self._first_trading_day)
            if self._first_trading_day is not None else None)