hard coded one-day warmup

This commit is contained in:
scottsanderson
2012-08-14 15:28:02 -04:00
committed by Eddie Hebert
parent 810dbe736a
commit 7ce3667bc2
2 changed files with 37 additions and 1 deletions
+20
View File
@@ -184,6 +184,8 @@ class TradingEnvironment(object):
self.first_open = self.calculate_first_open()
self.last_close = self.calculate_last_close()
self.prior_day_open = self.calculate_prior_day_open()
def calculate_first_open(self):
"""
Finds the first trading day on or after self.period_start.
@@ -197,6 +199,24 @@ class TradingEnvironment(object):
first_open = self.set_NYSE_time(first_open, 9, 30)
return first_open
def calculate_prior_day_open(self):
"""
Finds the first trading day open that falls at least a day
before period_start.
"""
one_day = datetime.timedelta(days=1)
first_open = self.period_start - one_day
if first_open <= self.trading_days[0]:
log.warn("Cannot calculate prior day open.")
return self.period_start
while not self.is_trading_day(first_open):
first_open = first_open - one_day
first_open = self.set_NYSE_time(first_open, 9, 30)
return first_open
def calculate_last_close(self):
"""
Finds the last trading day on or before self.period_end
+17 -1
View File
@@ -55,6 +55,9 @@ class TradeSimulationClient(object):
self.style = sim_style
self.algo_sim = None
self.warmup_start = self.environment.prior_day_open
self.algo_start = self.environment.first_open
def get_hash(self):
"""
There should only ever be one TSC in the system.
@@ -96,6 +99,7 @@ class TradeSimulationClient(object):
with_portfolio,
ordering_client.state,
self.algo,
self.algo_start
)
# The algorithm will yield a daily_results message (as
@@ -107,7 +111,7 @@ class TradeSimulationClient(object):
class AlgorithmSimulator(object):
def __init__(self, stream_in, order_book, algo):
def __init__(self, stream_in, order_book, algo, algo_start):
self.stream_in = stream_in
@@ -121,6 +125,7 @@ class AlgorithmSimulator(object):
self.algo = algo
self.sids = algo.get_sid_filter()
self.algo_start = algo_start
# Monkey patch the user algorithm to place orders in the
# TransactionSimulator's order book.
@@ -212,6 +217,17 @@ class AlgorithmSimulator(object):
self.algo.initialize()
for event in self.stream_in:
# We're still in the warmup period. Use the event to
# update our universe, but don't start a snapshot or
# pass anything to handle_data. Discard any
# perf messages.
if event.dt < self.algo_start:
self.update_universe(event)
if event.perf_message:
log.info("Discarding perf message because we're in warmup.")
continue
# Yield any perf messages received to be relayed back to
# the browser.