From ea439bc1420f5ae25367509d5620b7986597b047 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Thu, 22 Feb 2018 15:44:03 +0000 Subject: [PATCH 1/2] MAINT: Pass asset instead of sid to dispatch reader. So that the asset does not need to be re-retrieved from the asset finder, pass assets to the dispatch reader's `get_value`. --- zipline/assets/roll_finder.py | 6 +++--- zipline/data/data_portal.py | 6 +++--- zipline/data/dispatch_bar_reader.py | 3 +-- zipline/data/history_loader.py | 8 ++++---- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/zipline/assets/roll_finder.py b/zipline/assets/roll_finder.py index 4c85ab8092..64abd570da 100644 --- a/zipline/assets/roll_finder.py +++ b/zipline/assets/roll_finder.py @@ -96,7 +96,7 @@ def get_rolls(self, root_symbol, start, end, offset): else: first = front first_contract = oc.sid_to_contract[first] - rolls = [((first_contract >> offset).contract.sid, None)] + rolls = [((first_contract >> offset).contract, None)] tc = self.trading_calendar sessions = tc.sessions_in_range(tc.minute_to_session_label(start), tc.minute_to_session_label(end)) @@ -115,7 +115,7 @@ def get_rolls(self, root_symbol, start, end, offset): session = sessions[-1] while session > start and curr is not None: - front = curr.contract.sid + front = curr.contract back = rolls[0][0] prev_c = curr.prev while session > start: @@ -127,7 +127,7 @@ def get_rolls(self, root_symbol, start, end, offset): # TODO: Instead of listing each contract with its roll date # as tuples, create a series which maps every day to the # active contract on that day. - rolls.insert(0, ((curr >> offset).contract.sid, session)) + rolls.insert(0, ((curr >> offset).contract, session)) break session = prev curr = curr.prev diff --git a/zipline/data/data_portal.py b/zipline/data/data_portal.py index 0256dc96ac..cd6bd5023f 100644 --- a/zipline/data/data_portal.py +++ b/zipline/data/data_portal.py @@ -634,7 +634,7 @@ def _get_minute_spot_value(self, asset, column, dt, ffill=False): if not ffill: try: - return reader.get_value(asset.sid, dt, column) + return reader.get_value(asset, dt, column) except NoDataOnDate: if column != 'volume': return np.nan @@ -646,7 +646,7 @@ def _get_minute_spot_value(self, asset, column, dt, ffill=False): try: # Optimize the best case scenario of a liquid asset # returning a valid price. - result = reader.get_value(asset.sid, dt, column) + result = reader.get_value(asset, dt, column) if not pd.isnull(result): return result except NoDataOnDate: @@ -662,7 +662,7 @@ def _get_minute_spot_value(self, asset, column, dt, ffill=False): # no last traded dt, bail return np.nan - result = reader.get_value(asset.sid, query_dt, column) + result = reader.get_value(asset, query_dt, column) if (dt == query_dt) or (dt.date() == query_dt.date()): return result diff --git a/zipline/data/dispatch_bar_reader.py b/zipline/data/dispatch_bar_reader.py index 6a39a2ecd6..d50ae26959 100644 --- a/zipline/data/dispatch_bar_reader.py +++ b/zipline/data/dispatch_bar_reader.py @@ -91,8 +91,7 @@ def last_available_dt(self): def first_trading_day(self): return max(r.first_trading_day for r in self._readers.values()) - def get_value(self, sid, dt, field): - asset = self._asset_finder.retrieve_asset(sid) + def get_value(self, asset, dt, field): r = self._readers[type(asset)] return r.get_value(asset, dt, field) diff --git a/zipline/data/history_loader.py b/zipline/data/history_loader.py index f7002044d0..7de4edd367 100644 --- a/zipline/data/history_loader.py +++ b/zipline/data/history_loader.py @@ -221,14 +221,14 @@ def _get_adjustments_in_range(self, cf, dts, field): adjs = {} for front, back in sliding_window(2, rolls): - front_sid, roll_dt = front - back_sid = back[0] + front_contract, roll_dt = front + back_contract = back[0] dt = tc.previous_session_label(roll_dt) if self._frequency == 'minute': dt = tc.open_and_close_for_session(dt)[1] roll_dt = tc.open_and_close_for_session(roll_dt)[0] - partitions.append((front_sid, - back_sid, + partitions.append((front_contract, + back_contract, dt, roll_dt)) for partition in partitions: From cb0a5e8a7650f1bbbc74d28192b5d6386d9403e3 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Fri, 23 Feb 2018 15:21:07 +0000 Subject: [PATCH 2/2] MAINT: Remove now unneeded calls to retrieve_asset. To squash. --- zipline/data/history_loader.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/zipline/data/history_loader.py b/zipline/data/history_loader.py index 7de4edd367..7f71796d27 100644 --- a/zipline/data/history_loader.py +++ b/zipline/data/history_loader.py @@ -160,12 +160,10 @@ class ContinuousFutureAdjustmentReader(object): def __init__(self, trading_calendar, - asset_finder, bar_reader, roll_finders, frequency): self._trading_calendar = trading_calendar - self._asset_finder = asset_finder self._bar_reader = bar_reader self._roll_finders = roll_finders self._frequency = frequency @@ -232,17 +230,19 @@ def _get_adjustments_in_range(self, cf, dts, field): dt, roll_dt)) for partition in partitions: - front_sid, back_sid, dt, roll_dt = partition + front_contract, back_contract, dt, roll_dt = partition last_front_dt = self._bar_reader.get_last_traded_dt( - self._asset_finder.retrieve_asset(front_sid), dt) + front_contract, dt + ) last_back_dt = self._bar_reader.get_last_traded_dt( - self._asset_finder.retrieve_asset(back_sid), dt) + back_contract, dt + ) if isnull(last_front_dt) or isnull(last_back_dt): continue front_close = self._bar_reader.get_value( - front_sid, last_front_dt, 'close') + front_contract, last_front_dt, 'close') back_close = self._bar_reader.get_value( - back_sid, last_back_dt, 'close') + back_contract, last_back_dt, 'close') adj_loc = dts.searchsorted(roll_dt) end_loc = adj_loc - 1 adj = self._make_adjustment(cf.adjustment, @@ -325,7 +325,6 @@ def __init__(self, trading_calendar, reader, equity_adjustment_reader, if roll_finders: self._adjustment_readers[ContinuousFuture] =\ ContinuousFutureAdjustmentReader(trading_calendar, - asset_finder, reader, roll_finders, self._frequency)