diff --git a/zipline/finance/performance.py b/zipline/finance/performance.py index 05af596f..e3564c54 100644 --- a/zipline/finance/performance.py +++ b/zipline/finance/performance.py @@ -18,6 +18,8 @@ Performance Tracking +-----------------+----------------------------------------------------+ | progress | percentage of test completed | +-----------------+----------------------------------------------------+ + | started_at | datetime in utc marking the start of this test | + +-----------------+----------------------------------------------------+ | cumulative_capti| The net capital used (positive is spent) through | | al_used | the course of all the events sent to this tracker | +-----------------+----------------------------------------------------+ @@ -105,6 +107,12 @@ Performance Period | | :py:meth:`Position.to_dict()` | | | for details on the contents of the dict | +---------------+------------------------------------------------------+ + | pnl | Dollar value profit and loss, for both realized and | + | | unrealized gains. | + +---------------+------------------------------------------------------+ + | returns | percentage returns for the entire portfolio over the | + | | period | + +---------------+------------------------------------------------------+ | timestamp | System time evevent occurs in zipilne | +---------------+------------------------------------------------------+ @@ -135,9 +143,10 @@ class PerformanceTracker(): def __init__(self, trading_environment): - self.trading_environment = trading_environment - self.trading_day = datetime.timedelta(hours = 6, minutes = 30) - self.calendar_day = datetime.timedelta(hours = 24) + self.trading_environment = trading_environment + self.trading_day = datetime.timedelta(hours = 6, minutes = 30) + self.calendar_day = datetime.timedelta(hours = 24) + self.started_at = datetime.datetime.utcnow() self.period_start = self.trading_environment.period_start self.period_end = self.trading_environment.period_end @@ -191,6 +200,7 @@ class PerformanceTracker(): returns_list = [x.to_dict() for x in self.returns] return { + 'started_at' : self.started_at, 'period_start' : self.period_start, 'period_end' : self.period_end, 'progress' : self.progress, @@ -421,6 +431,8 @@ class PerformancePeriod(): 'ending_cash' : self.ending_cash, 'positions' : positions, 'timestamp' : datetime.datetime.now(), + 'pnl' : self.pnl, + 'returns' : self.returns } def to_namedict(self): diff --git a/zipline/protocol.py b/zipline/protocol.py index f4ee5d68..3b4bfa05 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -629,40 +629,37 @@ def PERF_FRAME(perf): #pull some special fields from the perf for easy access date = perf['last_close'] tp = perf['todays_perf'] + cp = perf['cumulative_perf'] risk = perf['cumulative_risk_metrics'] - #create the daily nested message - #TODO: add daily PnL in dollars daily_perf = { 'date' : EPOCH(date), - 'returns' : perf['returns'][-1]['returns'], - 'pnl' : 0.0, + 'returns' : tp['returns'], + 'pnl' : tp['pnl'], 'portfolio_value' : tp['ending_value'] } - #TODO: add total returns to the message from perf - #TODO: add total bm returns to the message from perf - #TODO: add daily PnL in dollars cumulative_perf = { 'alpha' : risk['alpha'], 'beta' : risk['beta'], 'sharpe' : risk['sharpe'], - 'total_returns' : 0.0, + 'total_returns' : cp['returns'], 'volatility' : risk['algo_volatility'], 'benchmark_volatility' : risk['benchmark_volatility'], - 'benchmark_returns' : 0, + 'benchmark_returns' : risk['benchmark_period_return'], 'max_drawdown' : risk['max_drawdown'], - 'pnl' : 0.0, + 'pnl' : cp['pnl'] } + + # nest the cumulative performance data in the daily. + daily_perf['cumulative'] = cumulative_perf - #TODO: perf needs to track start time of the bt #TODO: pass the cursor value in. result = { - 'started_at' : 0, + 'started_at' : EPOCH(perf['started_at']), + 'cursor' : 0, 'daily' : [daily_perf], 'percent_complete' : perf['progress'], - 'cumulative' : cumulative_perf, - 'cursor' : 0, } return msgpack.dumps(tuple(['PERF', result]))