From 54f8d7d01379fcba969d9a9c3a35f6610166d200 Mon Sep 17 00:00:00 2001 From: fawce Date: Tue, 24 Apr 2012 17:51:02 -0400 Subject: [PATCH] removed lines sending messages to the control channel, simplified logic for handling max loss case, DONE'ing trading client on max loss. Zipline continues to run until all components are finished. --- zipline/finance/performance.py | 28 +++++++++++++++------------- zipline/finance/trading.py | 25 +++++++++++++------------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 49600b0a..02d018b6 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -224,6 +224,10 @@ class PerformanceTracker(): self.order_log.append(order) def process_event(self, event): + + if self.exceeded_max_loss: + return + assert isinstance(event, zp.namedict) self.event_count += 1 @@ -265,7 +269,13 @@ class PerformanceTracker(): self.day_count += 1.0 # calculate progress of test self.progress = self.day_count / self.total_days - + + # Output results + if self.result_stream: + msg = zp.PERF_FRAME(self.to_dict()) + self.result_stream.send(msg) + + # if self.trading_environment.max_drawdown: returns = self.todays_performance.returns max_dd = -1 * self.trading_environment.max_drawdown @@ -276,16 +286,8 @@ class PerformanceTracker(): # so it shows up in the update, but don't end the test # here. Let the update go out before stopping self.exceeded_max_loss = True - - # Output results - if self.result_stream: - msg = zp.PERF_FRAME(self.to_dict()) - self.result_stream.send(msg) + return - if self.exceeded_max_loss: - # now that we've sent the day's update, kill this test - self.handle_simulation_end(skip_close=True) - return #move the market day markers forward self.market_open = self.market_open + self.calendar_day @@ -307,7 +309,7 @@ class PerformanceTracker(): keep_transactions = True ) - def handle_simulation_end(self, skip_close=False): + def handle_simulation_end(self): """ When the simulation is complete, run the full period risk report and send it out on the result_stream. @@ -319,8 +321,8 @@ class PerformanceTracker(): # the stream will end on the last trading day, but will not trigger # an end of day, so we trigger the final market close here. - # In the case of errors, we needn't close again. - if not skip_close: + # In the case of max drawdown, we needn't close again. + if not self.exceeded_max_loss: self.handle_market_close() self.risk_report = risk.RiskReport( diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index b503e501..03e45fa8 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -80,14 +80,7 @@ class TradeSimulationClient(qmsg.Component): # if the feed is done, shut 'er down if msg == str(zp.CONTROL_PROTOCOL.DONE): - qutil.LOGGER.info("Client is DONE!") - # signal the performance tracker that the simulation has - # ended. Perf will internally calculate the full risk report. - self.perf.handle_simulation_end() - - # signal Simulator, our ComponentHost, that this component is - # done and Simulator needn't block exit on this component. - self.signal_done() + self.finish_simulation() return # result_feed is a merge component, so unframe accordingly @@ -95,14 +88,22 @@ class TradeSimulationClient(qmsg.Component): self.received_count += 1 # update performance and relay the event to the algorithm self.process_event(event) + if self.perf.exceeded_max_loss: + self.finish_simulation() + def finish_simulation(self): + qutil.LOGGER.info("Client is DONE!") + # signal the performance tracker that the simulation has + # ended. Perf will internally calculate the full risk report. + self.perf.handle_simulation_end() + + # signal Simulator, our ComponentHost, that this component is + # done and Simulator needn't block exit on this component. + self.signal_done() def process_event(self, event): - if self.perf.exceeded_max_loss: - self.control_out.send(str(zp.CONTROL_PROTOCOL.SHUTDOWN)) - return - + # generate transactions, if applicable txn = self.txn_sim.apply_trade_to_open_orders(event) if txn: