From 7aeaa69acf1998986d1001e8d59d24ce3be64283 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Mon, 10 Feb 2014 21:02:01 -0500 Subject: [PATCH] BUG: Prevent minute emission from crashing at end of available data. The next day calculation was causing an error when a minute emission algorithm reached the end of available data. Instead of a generic exception when available data is reached, raise and catch a named exception so that the tradesimulation loop can skip over, since the next market close is not needed at the end. --- zipline/finance/trading.py | 9 ++++++++- zipline/gens/tradesimulation.py | 14 ++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) 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