diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 98a0fcea..4a6473ca 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -66,6 +66,13 @@ log = logbook.Logger('Trading') environment = None +class NoFurtherDataError(Exception): + """ + Thrown when next trading is attempted at the end of available data. + """ + pass + + class TradingEnvironment(object): def __init__( @@ -175,7 +182,7 @@ class TradingEnvironment(object): next_open = self.next_trading_day(start_date) if next_open is None: - raise Exception( + raise NoFurtherDataError( "Attempt to backtest beyond available history. \ Last successful date: %s" % self.last_trading_day) diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index c95b63de..2f053134 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -189,10 +189,16 @@ class AlgorithmSimulator(object): tp = self.algo.perf_tracker.todays_performance tp.rollover() if mkt_close <= self.algo.perf_tracker.last_close: - _, mkt_close = \ - trading.environment.next_open_and_close( - mkt_close - ) + try: + _, mkt_close = \ + trading.environment.\ + next_open_and_close( + mkt_close + ) + except trading.NoFurtherDataError: + # If at the end of backtest history, + # skip advancing market close. + pass self.algo.perf_tracker.handle_intraday_close() self.algo.portfolio_needs_update = True