From 8037e309b298970c014d953a4226b03b6284005a Mon Sep 17 00:00:00 2001 From: Andrew Yang Date: Sat, 17 Sep 2016 15:24:51 -0400 Subject: [PATCH 1/2] ez --- your_return_calculator.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/your_return_calculator.rb b/your_return_calculator.rb index 3b43174..5a90726 100644 --- a/your_return_calculator.rb +++ b/your_return_calculator.rb @@ -9,7 +9,15 @@ def calculate! # snapshot.date # snapshot.cash_flow # snapshot.market_value - - BigDecimal.new(0) + previous_snapshot = nil + total_return = 1 + for snapshot in snapshots do + if previous_snapshot != nil then + period_return = (snapshot.market_value - snapshot.cash_flow) / previous_snapshot.market_value + total_return = total_return * period_return + end + previous_snapshot = snapshot + end + return total_return - 1 end end From 1c23137daa30328c575a4f30e7d31cb49186372b Mon Sep 17 00:00:00 2001 From: Andrew Yang Date: Sat, 17 Sep 2016 15:32:43 -0400 Subject: [PATCH 2/2] Make BigDecimal --- your_return_calculator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/your_return_calculator.rb b/your_return_calculator.rb index 5a90726..6cd4aac 100644 --- a/your_return_calculator.rb +++ b/your_return_calculator.rb @@ -10,7 +10,7 @@ def calculate! # snapshot.cash_flow # snapshot.market_value previous_snapshot = nil - total_return = 1 + total_return = BigDecimal.new(1) for snapshot in snapshots do if previous_snapshot != nil then period_return = (snapshot.market_value - snapshot.cash_flow) / previous_snapshot.market_value