Merge pull request #14 from quantopian/portfolio

Portfolio
This commit is contained in:
fawce
2012-03-16 08:48:39 -07:00
+43 -5
View File
@@ -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,10 @@ 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. See header comments for a detailed description.
"""
positions = self.get_positions()
return {
'ending_value' : self.ending_value,
@@ -411,6 +414,41 @@ 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.
Properties are the same as the results of to_dict. See header comments
for a detailed description.
"""
positions = self.get_positions(namedicted=True)
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
})
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