From ca9fdcfe84491883829ddcae891278e2da386b7b Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Fri, 11 Jan 2013 14:50:00 -0500 Subject: [PATCH] Uses a Portfolio object instead of an ndict. Gains some performance by using a 'regular' object instead of an ndict. Also, directly sets up the values that we return, instead of going in between with __core_dict and then removing values. In it's entirety performanc.as_portfolio is the current highest bottleneck, working on reducing time spent in that function. --- zipline/finance/performance.py | 36 ++++++++++++---------------------- zipline/protocol.py | 10 ++++++++++ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index d8776220..8d0325de 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -139,6 +139,7 @@ import math from zipline.utils.protocol_utils import ndict import zipline.protocol as zp import zipline.finance.risk as risk +from zipline.protocol import Portfolio log = logbook.Logger('Performance') @@ -543,30 +544,17 @@ class PerformancePeriod(object): PerformancePeriod, and in this method we rename some fields for usability and remove extraneous fields. """ - portfolio = self.__core_dict() - # rename: - # ending_cash -> cash - # period_open -> backtest_start - # - # remove: - # period_close, starting_value, - # cumulative_capital_used, max_leverage, max_capital_used - portfolio['cash'] = portfolio['ending_cash'] - portfolio['start_date'] = portfolio['period_open'] - portfolio['positions_value'] = portfolio['ending_value'] - - del(portfolio['ending_cash']) - del(portfolio['period_open']) - del(portfolio['period_close']) - del(portfolio['starting_value']) - del(portfolio['ending_value']) - del(portfolio['cumulative_capital_used']) - del(portfolio['max_leverage']) - del(portfolio['max_capital_used']) - - portfolio['positions'] = self.get_positions() - - return ndict(portfolio) + return Portfolio({ + 'capital_used': self.period_capital_used, + 'starting_cash': self.starting_cash, + 'portfolio_value': self.ending_cash + self.ending_value, + 'pnl': self.pnl, + 'returns': self.returns, + 'cash': self.ending_cash, + 'start_date': self.period_open, + 'positions': self.get_positions(), + 'positions_value': self.ending_value + }) def get_positions(self): diff --git a/zipline/protocol.py b/zipline/protocol.py index 99d33d39..9e71d3ae 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -55,3 +55,13 @@ class Event(object): def __repr__(self): return "Event({0})".format(self.__dict__) + + +class Portfolio(object): + + def __init__(self, initial_values=None): + if initial_values: + self.__dict__ = initial_values + + def __repr__(self): + return "Portfolio({0})".format(self.__dict__)