From 05bb179abab68e0f8bfc3e5954420380c809e277 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Wed, 17 Oct 2012 23:49:06 -0400 Subject: [PATCH] Accounts for negative values when creating compounded returns. Sets the value sent to log to a value that doesn't crash out because of negative value. Setting the value to 0 instead. --- zipline/finance/risk.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/zipline/finance/risk.py b/zipline/finance/risk.py index 35936110..759777ad 100644 --- a/zipline/finance/risk.py +++ b/zipline/finance/risk.py @@ -492,13 +492,20 @@ class RiskMetricsIterative(RiskMetricsBase): def update_compounded_log_returns(self): if len(self.algorithm_returns) == 0: return - elif len(self.compounded_log_returns) == 0: - self.compounded_log_returns.append( - math.log(1 + self.algorithm_returns[-1])) + + try: + compound = math.log(1 + self.algorithm_returns[-1]) + except ValueError: + compound = 0.0 + # BUG? Shouldn't this be set to log(1.0 + 0) ? + + if len(self.compounded_log_returns) == 0: + self.compounded_log_returns.append(compound) else: self.compounded_log_returns.append( self.compounded_log_returns[-1] + - math.log(1 + self.algorithm_returns[-1])) + compound + ) def calculate_period_returns(self, returns): period_returns = 1.0