From ebe00b83f7aa013ab998c05116754f2d49322d44 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Thu, 20 Jun 2013 12:36:49 -0400 Subject: [PATCH] MAINT: Pass order emitted from slippage up to tradesimulation. Instead of searching through the open orders to find the ones that match the current transactions, now that simulate returns the pair of transaction and order for which that transaction was created for, that order can be used where we previously searched for a modified order. This should be a runtime improvement since, but not yet verified via thorough profiling. --- tests/test_finance.py | 4 +--- zipline/finance/blotter.py | 16 ++++------------ zipline/gens/tradesimulation.py | 10 ++++++---- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/tests/test_finance.py b/tests/test_finance.py index f6c7dae7..ef68ba1f 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -362,9 +362,7 @@ class FinanceTestCase(TestCase): for event in events: if event.type == DATASOURCE_TYPE.TRADE: - txns, _ = blotter.process_trade(event) - - for txn in txns: + for txn, order in blotter.process_trade(event): transactions.append(txn) tracker.process_event(txn) diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index bdefba40..92f891a8 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -143,12 +143,12 @@ class Blotter(object): def process_trade(self, trade_event): if trade_event.type != zp.DATASOURCE_TYPE.TRADE: - return [], [] + raise StopIteration if zp_math.tolerant_equals(trade_event.volume, 0): # there are zero volume trade_events bc some stocks trade # less frequently than once per minute. - return [], [] + raise StopIteration if trade_event.sid in self.open_orders: orders = self.open_orders[trade_event.sid] @@ -158,9 +158,7 @@ class Blotter(object): lambda o: o.dt <= trade_event.dt, orders) else: - return [], [] - - txns = [] + raise StopIteration for order, txn in self.transact(trade_event, current_orders): order.filled += txn.amount @@ -168,11 +166,7 @@ class Blotter(object): # that is filling it. order.dt = txn.dt - txns.append(txn) - - modified_orders = [order for order - in self.open_orders[trade_event.sid] - if order.dt == trade_event.dt] + yield txn, order # update the open orders for the trade_event's sid self.open_orders[trade_event.sid] = \ @@ -180,8 +174,6 @@ class Blotter(object): in self.open_orders[trade_event.sid] if order.open] - return txns, modified_orders - class Order(object): def __init__(self, dt, sid, amount, stop=None, limit=None, filled=0, diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index ee1bf10b..167e73c3 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from itertools import chain from logbook import Logger, Processor import zipline.finance.trading as trading @@ -120,9 +119,12 @@ class AlgorithmSimulator(object): if event.type == DATASOURCE_TYPE.BENCHMARK: self.algo.set_datetime(event.dt) bm_updated = True - txns, orders = self.algo.blotter.process_trade(event) - for data in chain(txns, orders, [event]): - self.algo.perf_tracker.process_event(data) + + process_trade = self.algo.blotter.process_trade + for txn, order in process_trade(event): + self.algo.perf_tracker.process_event(txn) + self.algo.perf_tracker.process_event(order) + self.algo.perf_tracker.process_event(event) # Update our portfolio. self.algo.set_portfolio(