From 2eeb92442e0ee3458f7ea83cb39e74da38746eeb Mon Sep 17 00:00:00 2001 From: fawce Date: Tue, 10 Apr 2012 00:02:54 -0400 Subject: [PATCH 1/2] added transactions to the daily update objects. --- zipline/finance/performance.py | 6 ++++++ zipline/protocol.py | 16 +++++++++++++++- zipline/protocol_utils.py | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 93d06f2c..081d5282 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -81,6 +81,9 @@ Position Tracking | last_sale_date | datetime of the last trade of the position's | | | security on the exchange | +-----------------+----------------------------------------------------+ + | transactions | all the transactions that were acrued into this | + | | position. | + +-----------------+----------------------------------------------------+ | timestamp | System time event occurs in zipilne | +-----------------+----------------------------------------------------+ @@ -344,11 +347,13 @@ class Position(): self.cost_basis = 0.0 ##per share self.last_sale_price = None self.last_sale_date = None + self.transactions = [] def update(self, txn): if(self.sid != txn.sid): raise NameError('updating position with txn for a different sid') + self.transactions.append(txn) #we're covering a short or closing a position if(self.amount + txn.amount == 0): self.cost_basis = 0.0 @@ -387,6 +392,7 @@ class Position(): 'last_sale_price' : self.last_sale_price, 'last_sale_date' : self.last_sale_date, 'timestamp' : datetime.datetime.now(), + 'transactions' : self.transactions } diff --git a/zipline/protocol.py b/zipline/protocol.py index f8db09bc..f40b3475 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -631,6 +631,19 @@ def PERF_FRAME(perf): tp = perf['todays_perf'] cp = perf['cumulative_perf'] risk = perf['cumulative_risk_metrics'] + + # aggregate the day's transactions, which are nested in their + # respsective positions. + transactions = [] + for sid, position in tp['positions'].iteritems(): + for txn in position['transactions']: + cur = { + 'date':EPOCH(txn.dt), + 'amount': txn.amount, + 'price': txn.price, + 'sid':txn.sid + } + transactions.append(cur) daily_perf = { 'date' : EPOCH(date), @@ -640,7 +653,8 @@ def PERF_FRAME(perf): 'portfolio_value' : tp['portfolio_value'], 'starting_cash' : tp['starting_cash'], 'ending_cash' : tp['ending_cash'], - 'capital_used' : tp['capital_used'] + 'capital_used' : tp['capital_used'], + 'transactions' : transactions } cumulative_perf = { diff --git a/zipline/protocol_utils.py b/zipline/protocol_utils.py index ba805b3b..4581d732 100644 --- a/zipline/protocol_utils.py +++ b/zipline/protocol_utils.py @@ -60,7 +60,7 @@ class namedict(object): def __getitem__(self, key): return self.__dict__[key] - + def keys(self): return self.__dict__.keys() From 368341ce616dad52b26f0c257ec3dc48f50ac259 Mon Sep 17 00:00:00 2001 From: fawce Date: Tue, 10 Apr 2012 14:12:03 -0400 Subject: [PATCH 2/2] moved transaction store to PerformancePeriod. added the position data to the performance message. --- zipline/finance/performance.py | 10 +++++----- zipline/protocol.py | 30 ++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 081d5282..79e73c12 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -347,13 +347,11 @@ class Position(): self.cost_basis = 0.0 ##per share self.last_sale_price = None self.last_sale_date = None - self.transactions = [] def update(self, txn): if(self.sid != txn.sid): raise NameError('updating position with txn for a different sid') - self.transactions.append(txn) #we're covering a short or closing a position if(self.amount + txn.amount == 0): self.cost_basis = 0.0 @@ -391,8 +389,7 @@ class Position(): 'cost_basis' : self.cost_basis, 'last_sale_price' : self.last_sale_price, 'last_sale_date' : self.last_sale_date, - 'timestamp' : datetime.datetime.now(), - 'transactions' : self.transactions + 'timestamp' : datetime.datetime.now() } @@ -408,6 +405,7 @@ class PerformancePeriod(): #cash balance at start of period self.starting_cash = starting_cash self.ending_cash = starting_cash + self.processed_transactions = [] self.calculate_performance() @@ -429,6 +427,7 @@ class PerformancePeriod(): self.positions[txn.sid] = Position(txn.sid) self.positions[txn.sid].update(txn) self.period_capital_used += -1 * txn.price * txn.amount + self.processed_transactions.append(txn) def calculate_positions_value(self): mktValue = 0.0 @@ -459,7 +458,8 @@ class PerformancePeriod(): 'positions' : positions, 'timestamp' : datetime.datetime.now(), 'pnl' : self.pnl, - 'returns' : self.returns + 'returns' : self.returns, + 'transactions' : self.processed_transactions, } def to_namedict(self): diff --git a/zipline/protocol.py b/zipline/protocol.py index f40b3475..17501c05 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -635,15 +635,24 @@ def PERF_FRAME(perf): # aggregate the day's transactions, which are nested in their # respsective positions. transactions = [] - for sid, position in tp['positions'].iteritems(): - for txn in position['transactions']: - cur = { - 'date':EPOCH(txn.dt), - 'amount': txn.amount, - 'price': txn.price, - 'sid':txn.sid - } - transactions.append(cur) + for txn in tp['transactions']: + cur = { + 'date':EPOCH(txn.dt), + 'amount': txn.amount, + 'price': txn.price, + 'sid':txn.sid + } + transactions.append(cur) + + positions = [] + for sid, pos in tp['positions'].iteritems(): + cur = { + 'cost_basis':pos['cost_basis'], + 'sid' :pos['sid'], + 'last_sale' :pos['last_sale_price'], + 'amount' :pos['amount'] + } + positions.append(cur) daily_perf = { 'date' : EPOCH(date), @@ -654,7 +663,8 @@ def PERF_FRAME(perf): 'starting_cash' : tp['starting_cash'], 'ending_cash' : tp['ending_cash'], 'capital_used' : tp['capital_used'], - 'transactions' : transactions + 'transactions' : transactions, + 'positions' : positions } cumulative_perf = {