mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-29 17:51:22 +08:00
@@ -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
|
||||
|
||||
@@ -101,10 +101,16 @@ class SpecificEquityTrades(object):
|
||||
def get_hash(self):
|
||||
return self.__class__.__name__ + "-" + self.arg_string
|
||||
|
||||
def update_source_id(self, gen):
|
||||
for event in gen:
|
||||
event.source_id = self.get_hash()
|
||||
yield event
|
||||
|
||||
def create_fresh_generator(self):
|
||||
|
||||
if self.event_list:
|
||||
unfiltered = (event for event in self.event_list)
|
||||
event_gen = (event for event in self.event_list)
|
||||
unfiltered = self.update_source_id(event_gen)
|
||||
|
||||
# Set up iterators for each expected field.
|
||||
else:
|
||||
|
||||
@@ -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 != 'DONE' and 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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user