diff --git a/tests/test_events_through_risk.py b/tests/test_events_through_risk.py index 8c463fa6..7a8b42b0 100644 --- a/tests/test_events_through_risk.py +++ b/tests/test_events_through_risk.py @@ -160,7 +160,7 @@ class TestEventsThroughRisk(unittest.TestCase): np.testing.assert_almost_equal( expected_sharpe[current_dt], - crm.sharpe[-1], + crm.metrics.sharpe[current_dt], decimal=6) def test_minute_buy_and_hold(self): diff --git a/zipline/finance/risk/cumulative.py b/zipline/finance/risk/cumulative.py index 9271e21c..3d9eb710 100644 --- a/zipline/finance/risk/cumulative.py +++ b/zipline/finance/risk/cumulative.py @@ -92,7 +92,12 @@ class RiskMetricsCumulative(object): self.algorithm_period_returns = [] self.benchmark_period_returns = [] - self.sharpe = [] + self.latest_dt = cont_index[0] + + metric_names = ('sharpe',) + + self.metrics = pd.DataFrame(index=cont_index, columns=metric_names) + self.sortino = [] self.information = [] self.beta = [] @@ -168,11 +173,15 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" self.algorithm_period_returns[-1] - self.treasury_period_return) self.beta.append(self.calculate_beta()) self.alpha.append(self.calculate_alpha()) - self.sharpe.append(self.calculate_sharpe()) + self.metrics.sharpe[dt] = self.calculate_sharpe() self.sortino.append(self.calculate_sortino()) self.information.append(self.calculate_information()) self.max_drawdown = self.calculate_max_drawdown() + # Keep track of latest dt for use in to_dict and other methods + # that report current state. + self.latest_dt = dt + def to_dict(self): """ Creates a dictionary representing the state of the risk report. @@ -193,7 +202,7 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" 'period_label': period_label } - rval['sharpe'] = self.sharpe[-1] + rval['sharpe'] = self.metrics.sharpe[self.latest_dt] rval['sortino'] = self.sortino[-1] rval['information'] = self.information[-1]