From 9376556e68aa37994cd1649b2e096784708a519d Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Fri, 23 Aug 2013 12:33:09 -0400 Subject: [PATCH] BUG: Use dt as a max value in trading get_index. Instead of sliding to the next trading day because of the behavior of `searchsorted`, if dt argument is not a trading day use it as a max value for corresponding date of the index. Fixes a bug where if the end of the quarter is calculated with disregard to trading days, get_index would return the first day of the next quarter, instead of the last trading day of the intended quarter. --- zipline/finance/trading.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index fd492b1b..0bd4cc6d 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -230,8 +230,15 @@ Last successful date: %s" % self.last_trading_day) return j - i def get_index(self, dt): + """ + Return the index of the given @dt, or the index of the preceding + trading day if the given dt is not in the trading calendar. + """ ndt = self.normalize_date(dt) - return self.trading_days.searchsorted(ndt) + if ndt in self.trading_days: + return self.trading_days.searchsorted(ndt) + else: + return self.trading_days.searchsorted(ndt) - 1 class SimulationParameters(object):