From 2492feb938c21f7577e6c05fa8cd7bfd57863d25 Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Wed, 30 Oct 2013 15:05:09 -0400 Subject: [PATCH] ENH: Keep track of total commissions as attribute on Order Value is summed from TRANSACTION and COMMISSION events. Defaults to None, meaning unset. --- tests/test_perf_tracking.py | 3 ++- zipline/finance/blotter.py | 31 ++++++++++++++++--------------- zipline/finance/slippage.py | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index ad86e00a..ec47132c 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -1209,7 +1209,8 @@ class TestPerformanceTracker(unittest.TestCase): amount=-25, dt=foo_event_1.dt, price=10.0, - commission=0.50) + commission=0.50, + order_id=order_event_1.id) benchmark_event_1 = Event({ 'dt': start_dt, 'returns': 0.01, diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index cab04045..0cdbee46 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -125,14 +125,11 @@ class Blotter(object): dt=self.current_dt, sid=sid, amount=amount, - filled=0, stop=stop_price, limit=limit_price, id=order_id ) - # initialized filled field. - order.filled = 0 self.open_orders[order.sid].append(order) self.orders[order.id] = order self.new_orders.append(order) @@ -211,19 +208,22 @@ class Blotter(object): for order, txn in self.transact(trade_event, current_orders): if txn.type == zp.DATASOURCE_TYPE.COMMISSION: - yield txn, order - continue + order.commission = (order.commission or 0.0) + txn.cost + else: + if txn.amount == 0: + raise zipline.errors.TransactionWithNoAmount(txn=txn) + if math.copysign(1, txn.amount) != order.direction: + raise zipline.errors.TransactionWithWrongDirection( + txn=txn, order=order) + if abs(txn.amount) > abs(self.orders[txn.order_id].amount): + raise zipline.errors.TransactionVolumeExceedsOrder( + txn=txn, order=order) - if txn.amount == 0: - raise zipline.errors.TransactionWithNoAmount(txn=txn) - if math.copysign(1, txn.amount) != order.direction: - raise zipline.errors.TransactionWithWrongDirection( - txn=txn, order=order) - if abs(txn.amount) > abs(self.orders[txn.order_id].amount): - raise zipline.errors.TransactionVolumeExceedsOrder( - txn=txn, order=order) + order.filled += txn.amount + if txn.commission is not None: + order.commission = ((order.commission or 0.0) + + txn.commission) - order.filled += txn.amount # mark the date of the order to match the transaction # that is filling it. order.dt = txn.dt @@ -239,7 +239,7 @@ class Blotter(object): class Order(object): def __init__(self, dt, sid, amount, stop=None, limit=None, filled=0, - id=None): + commission=None, id=None): """ @dt - datetime.datetime that the order was placed @sid - stock sid of the order @@ -255,6 +255,7 @@ class Order(object): self.sid = sid self.amount = amount self.filled = filled + self.commission = commission self.status = ORDER_STATUS.OPEN self.stop = stop self.limit = limit diff --git a/zipline/finance/slippage.py b/zipline/finance/slippage.py index 2fab4e97..6f112318 100644 --- a/zipline/finance/slippage.py +++ b/zipline/finance/slippage.py @@ -113,7 +113,7 @@ def transact_partial(slippage, commission): class Transaction(object): - def __init__(self, sid, amount, dt, price, order_id=None, commission=None): + def __init__(self, sid, amount, dt, price, order_id, commission=None): self.sid = sid self.amount = amount self.dt = dt