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.
This commit is contained in:
Eddie Hebert
2014-02-10 21:02:01 -05:00
parent e1608ad3f3
commit 7aeaa69acf
2 changed files with 18 additions and 5 deletions
+8 -1
View File
@@ -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)
+10 -4
View File
@@ -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