mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-01 18:20:24 +08:00
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:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user