Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

FIX holdings and exchange rate (#345)

soky srm0290ed636f59
diff --git a/app/models/exchange_rate/importer.rb b/app/models/exchange_rate/importer.rb
index 0975f2edfb..a077401b32 100644
--- a/app/models/exchange_rate/importer.rb
+++ b/app/models/exchange_rate/importer.rb
@@ -43,7 +43,8 @@ class ExchangeRate::Importer
end
# Gapfill with LOCF strategy (last observation carried forward)
- if chosen_rate.nil?
+ # Treat nil or zero rates as invalid and use previous rate
+ if chosen_rate.nil? || chosen_rate.to_f <= 0
chosen_rate = prev_rate_value
end
diff --git a/app/models/provider/twelve_data.rb b/app/models/provider/twelve_data.rb
index 5a7738bb4d..8f347d9fba 100644
--- a/app/models/provider/twelve_data.rb
+++ b/app/models/provider/twelve_data.rb
@@ -74,7 +74,7 @@ class Provider::TwelveData < Provider
data.map do |resp|
rate = resp.dig("close")
date = resp.dig("datetime")
- if rate.nil?
+ if rate.nil? || rate.to_f <= 0
Rails.logger.warn("#{self.class.name} returned invalid rate data for pair from: #{from} to: #{to} on: #{date}. Rate data: #{rate.inspect}")
next
end
@@ -178,7 +178,7 @@ class Provider::TwelveData < Provider
values.map do |resp|
price = resp.dig("close")
date = resp.dig("datetime")
- if price.nil?
+ if price.nil? || price.to_f <= 0
Rails.logger.warn("#{self.class.name} returned invalid price data for security #{symbol} on: #{date}. Price data: #{price.inspect}")
next
end
diff --git a/app/models/security/price/importer.rb b/app/models/security/price/importer.rb
index bcee3762dc..6fb064cbe6 100644
--- a/app/models/security/price/importer.rb
+++ b/app/models/security/price/importer.rb
@@ -51,7 +51,10 @@ class Security::Price::Importer
end
# Gap-fill using LOCF (last observation carried forward)
- chosen_price ||= prev_price_value
+ # Treat nil or zero prices as invalid and use previous price
+ if chosen_price.nil? || chosen_price.to_f <= 0
+ chosen_price = prev_price_value
+ end
prev_price_value = chosen_price
{
diff --git a/app/views/holdings/_holding.html.erb b/app/views/holdings/_holding.html.erb
index 7c03d86da5..c38fc1a067 100644
--- a/app/views/holdings/_holding.html.erb
+++ b/app/views/holdings/_holding.html.erb
@@ -45,7 +45,8 @@
</div>
<div class="col-span-2 text-right">
- <% if holding.trend %>
+ <%# Show Total Return (unrealized G/L) when cost basis exists %>
+ <% if holding.trades.any? && holding.trend %>
<%= tag.p format_money(holding.trend.value), style: "color: #{holding.trend.color};" %>
<%= tag.p "(#{number_to_percentage(holding.trend.percent, precision: 1)})", style: "color: #{holding.trend.color};" %>
<% else %>