Merge pull request #1497 from quantopian/you-count-way-too-slowly

PERF: Be smarter about counting the number of minutes across a contiguous bunch of sessions.
This commit is contained in:
Jean Bredeche
2016-09-19 14:34:11 -04:00
committed by GitHub
2 changed files with 28 additions and 4 deletions
+5 -4
View File
@@ -1129,10 +1129,11 @@ class DataPortal(object):
previous_session,
)
minutes_count = sum(
len(self.trading_calendar.minutes_for_session(session))
for session in sessions
)
minutes_count = \
self.trading_calendar.minutes_count_for_sessions_in_range(
sessions[0],
sessions[-1]
)
# add the minutes for today
today_open = self.trading_calendar.open_and_close_for_session(
@@ -149,6 +149,29 @@ class TradingCalendar(with_metaclass(ABCMeta)):
def close_offset(self):
return 0
@lazyval
def _minutes_per_session(self):
diff = self.schedule.market_close - self.schedule.market_open
diff = diff.astype('timedelta64[m]')
return diff + 1
def minutes_count_for_sessions_in_range(self, start_session, end_session):
"""
Parameters
----------
start_session: pd.Timestamp
The first session.
end_session: pd.Timestamp
The last session.
Returns
-------
int: The total number of minutes for the contiguous chunk of sessions.
between start_session and end_session, inclusive.
"""
return int(self._minutes_per_session[start_session:end_session].sum())
@property
def regular_holidays(self):
"""