From 101baad0f891b6b152228d17b6c8b0bf532bffbc Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Mon, 14 Apr 2014 18:45:33 -0400 Subject: [PATCH] BUG: Fix change of type of period.sharpe The factoring out of the Sharpe calculation changed behavior so that both period and cumulative return nans when there is no volatility; however before that change period returned 0.0. This breaks existing consumers which expected a non-nan value for period results. Smooth out that change by checking the value after the sharpe has been calculated and reset nan's to 0.0 --- zipline/finance/risk/period.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/zipline/finance/risk/period.py b/zipline/finance/risk/period.py index b8127a31..b0aa35d4 100644 --- a/zipline/finance/risk/period.py +++ b/zipline/finance/risk/period.py @@ -112,6 +112,15 @@ class RiskMetricsPeriod(object): self.end_date ) self.sharpe = self.calculate_sharpe() + # The consumer currently expects a 0.0 value for sharpe in period, + # this differs from cumulative which was np.nan. + # When factoring out the sharpe_ratio, the different return types + # were collapsed into `np.nan`. + # TODO: Either fix consumer to accept `np.nan` or make the + # `sharpe_ratio` return type configurable. + # In the meantime, convert nan values to 0.0 + if pd.isnull(self.sharpe): + self.sharpe = 0.0 self.sortino = self.calculate_sortino() self.information = self.calculate_information() self.beta, self.algorithm_covariance, self.benchmark_variance, \