From 1ab84ae2ca5e3da653eecce34604aed604c4328c Mon Sep 17 00:00:00 2001 From: fawce Date: Thu, 15 Mar 2012 22:52:49 -0400 Subject: [PATCH 1/3] extended namedict and performance tracker to allow portfolio calculations to be accessed during a live test as a namedict. --- zipline/finance/performance.py | 38 +++++++++++++++++++++++++++++----- zipline/protocol_utils.py | 3 +++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 3a80171d..3458dbab 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -79,7 +79,7 @@ Position Tracking | last_sale_date | datetime of the last trade of the position's | | | security on the exchange | +-----------------+----------------------------------------------------+ - | timestamp | System time evevent occurs in zipilne | + | timestamp | System time event occurs in zipilne | +-----------------+----------------------------------------------------+ Performance Period @@ -154,6 +154,7 @@ class PerformanceTracker(): self.txn_count = 0 self.event_count = 0 self.result_stream = None + self.last_dict = None self.cumulative_performance = PerformancePeriod( {}, @@ -167,6 +168,8 @@ class PerformanceTracker(): starting_cash = self.capital_base ) + def get_portfolio(self): + return self.cumulative_performance.to_namedict() def publish_to(self, zmq_socket, context=None): """ @@ -400,10 +403,12 @@ class PerformancePeriod(): def to_dict(self): """ - Creates a dictionary representing the state of this performance period - Returns a dict object of the form: - + Creates a dictionary representing the state of this performance + period. """ + positions = {} + for sid, pos in self.positions.iteritems(): + positions[sid] = pos.to_dict() return { 'ending_value' : self.ending_value, @@ -411,6 +416,29 @@ class PerformancePeriod(): 'starting_value' : self.starting_value, 'starting_cash' : self.starting_cash, 'ending_cash' : self.ending_cash, - 'positions' : self.positions, + 'positions' : positions, 'timestamp' : datetime.datetime.now(), } + + def to_namedict(self): + """ + Creates a namedict representing the state of this perfomance period. + + """ + positions = {} + for sid, pos in self.positions.iteritems(): + positions[sid] = zp.namedict(pos.to_dict()) + + positions = zp.namedict(positions) + + return zp.namedict({ + 'ending_value' : self.ending_value, + 'capital_used' : self.period_capital_used, + 'starting_value' : self.starting_value, + 'starting_cash' : self.starting_cash, + 'ending_cash' : self.ending_cash, + 'positions' : positions + }) + + + diff --git a/zipline/protocol_utils.py b/zipline/protocol_utils.py index ba805b3b..62ab340c 100644 --- a/zipline/protocol_utils.py +++ b/zipline/protocol_utils.py @@ -86,6 +86,9 @@ class namedict(object): def has_attr(self, name): return self.__dict__.has_key(name) + + def __getitem__(self, key): + return self.__dict__[key] def as_series(self): s = pandas.Series(self.__dict__) From 77993ee6ca1557931668dbca9436f61a34021aa3 Mon Sep 17 00:00:00 2001 From: fawce Date: Fri, 16 Mar 2012 09:52:08 -0400 Subject: [PATCH 2/3] trivial mod to force push to happen. --- zipline/finance/performance.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 3458dbab..b07dc081 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -404,7 +404,7 @@ class PerformancePeriod(): def to_dict(self): """ Creates a dictionary representing the state of this performance - period. + period. See header comments for a detailed description. """ positions = {} for sid, pos in self.positions.iteritems(): @@ -422,7 +422,9 @@ class PerformancePeriod(): def to_namedict(self): """ - Creates a namedict representing the state of this perfomance period. + Creates a namedict representing the state of this perfomance period. + Properties are the same as the results of to_dict. See header comments + for a detailed description. """ positions = {} From 65253737cd9e32d207d4a552dbfb5dbb15c896ea Mon Sep 17 00:00:00 2001 From: fawce Date: Fri, 16 Mar 2012 11:47:06 -0400 Subject: [PATCH 3/3] taking code recommendations from @sdiehl PR notes... as always :) --- zipline/finance/performance.py | 20 ++++++++++++++------ zipline/protocol_utils.py | 3 --- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index b07dc081..346aa205 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -406,9 +406,7 @@ class PerformancePeriod(): Creates a dictionary representing the state of this performance period. See header comments for a detailed description. """ - positions = {} - for sid, pos in self.positions.iteritems(): - positions[sid] = pos.to_dict() + positions = self.get_positions() return { 'ending_value' : self.ending_value, @@ -427,9 +425,7 @@ class PerformancePeriod(): for a detailed description. """ - positions = {} - for sid, pos in self.positions.iteritems(): - positions[sid] = zp.namedict(pos.to_dict()) + positions = self.get_positions(namedicted=True) positions = zp.namedict(positions) @@ -442,5 +438,17 @@ class PerformancePeriod(): 'positions' : positions }) + def get_positions(self, namedicted=False): + positions = {} + for sid, pos in self.positions.iteritems(): + cur = pos.to_dict() + if namedicted: + positions[sid] = zp.namedict(cur) + else: + positions[sid] = cur + + return positions + + diff --git a/zipline/protocol_utils.py b/zipline/protocol_utils.py index 62ab340c..ba805b3b 100644 --- a/zipline/protocol_utils.py +++ b/zipline/protocol_utils.py @@ -86,9 +86,6 @@ class namedict(object): def has_attr(self, name): return self.__dict__.has_key(name) - - def __getitem__(self, key): - return self.__dict__[key] def as_series(self): s = pandas.Series(self.__dict__)