Files
sure/app/models/security/price/importer.rb
soky srm 4dfd2913c7 Investment prices fixes (#559)
* Fix investments retrieval

     Problem Summary

     Stock prices for securities like European stocks become stale because:
     1. sync_all_accounts runs at 2:22 UTC (before European markets open)
     2. Provider doesn't have today's price yet, so importer gap-fills with LOCF (yesterday's price)
     3. Later import_market_data at 22:00 UTC sees all prices exist and skips fetching
     4. Real closing price is never retrieved

     Solution Overview

     Add a provisional boolean column to mark gap-filled prices that should be re-fetched.

* Update schema.rb

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-01-07 16:16:01 +01:00

209 lines
7.2 KiB
Ruby

class Security::Price::Importer
MissingSecurityPriceError = Class.new(StandardError)
MissingStartPriceError = Class.new(StandardError)
PROVISIONAL_LOOKBACK_DAYS = 3
def initialize(security:, security_provider:, start_date:, end_date:, clear_cache: false)
@security = security
@security_provider = security_provider
@start_date = start_date
@end_date = normalize_end_date(end_date)
@clear_cache = clear_cache
end
# Constructs a daily series of prices for a single security over the date range.
# Returns the number of rows upserted.
def import_provider_prices
if !clear_cache && all_prices_exist?
Rails.logger.info("No new prices to sync for #{security.ticker} between #{start_date} and #{end_date}, skipping")
return 0
end
if provider_prices.empty?
Rails.logger.warn("Could not fetch prices for #{security.ticker} between #{start_date} and #{end_date} because provider returned no prices")
return 0
end
prev_price_value = start_price_value
unless prev_price_value.present?
Rails.logger.error("Could not find a start price for #{security.ticker} on or before #{start_date}")
Sentry.capture_exception(MissingStartPriceError.new("Could not determine start price for ticker")) do |scope|
scope.set_tags(security_id: security.id)
scope.set_context("security", {
id: security.id,
start_date: start_date
})
end
return 0
end
gapfilled_prices = effective_start_date.upto(end_date).map do |date|
db_price = db_prices[date]
db_price_value = db_price&.price
provider_price = provider_prices[date]
provider_price_value = provider_price&.price
provider_currency = provider_price&.currency
has_provider_price = provider_price_value.present? && provider_price_value.to_f > 0
is_provisional = db_price&.provisional
chosen_price = if clear_cache || is_provisional
provider_price_value || db_price_value # overwrite when possible (or when provisional)
else
db_price_value || provider_price_value # fill gaps
end
# Gap-fill using LOCF (last observation carried forward)
# Treat nil or zero prices as invalid and use previous price
used_locf = false
if chosen_price.nil? || chosen_price.to_f <= 0
chosen_price = prev_price_value
used_locf = true
end
prev_price_value = chosen_price
provisional = determine_provisional_status(
date: date,
has_provider_price: has_provider_price,
used_locf: used_locf,
existing_provisional: db_price&.provisional
)
{
security_id: security.id,
date: date,
price: chosen_price,
currency: provider_currency || prev_price_currency || db_price_currency || "USD",
provisional: provisional
}
end
upsert_rows(gapfilled_prices)
end
private
attr_reader :security, :security_provider, :start_date, :end_date, :clear_cache
def provider_prices
@provider_prices ||= begin
provider_fetch_start_date = effective_start_date - 5.days
response = security_provider.fetch_security_prices(
symbol: security.ticker,
exchange_operating_mic: security.exchange_operating_mic,
start_date: provider_fetch_start_date,
end_date: end_date
)
if response.success?
response.data.index_by(&:date)
else
Rails.logger.warn("#{security_provider.class.name} could not fetch prices for #{security.ticker} between #{provider_fetch_start_date} and #{end_date}. Provider error: #{response.error.message}")
Sentry.capture_exception(MissingSecurityPriceError.new("Could not fetch prices for ticker"), level: :warning) do |scope|
scope.set_tags(security_id: security.id)
scope.set_context("security", { id: security.id, start_date: start_date, end_date: end_date })
end
{}
end
end
end
def db_prices
@db_prices ||= Security::Price.where(security_id: security.id, date: start_date..end_date)
.order(:date)
.to_a
.index_by(&:date)
end
def all_prices_exist?
return false if has_refetchable_provisional_prices?
db_prices.count == expected_count
end
def has_refetchable_provisional_prices?
Security::Price.where(security_id: security.id, date: start_date..end_date)
.refetchable_provisional(lookback_days: PROVISIONAL_LOOKBACK_DAYS)
.exists?
end
def expected_count
(start_date..end_date).count
end
# Skip over ranges that already exist unless clearing cache
# Also includes dates with refetchable provisional prices
def effective_start_date
return start_date if clear_cache
refetchable_dates = Security::Price.where(security_id: security.id, date: start_date..end_date)
.refetchable_provisional(lookback_days: PROVISIONAL_LOOKBACK_DAYS)
.pluck(:date)
.to_set
(start_date..end_date).detect do |d|
!db_prices.key?(d) || refetchable_dates.include?(d)
end || end_date
end
def start_price_value
provider_price_value = provider_prices.select { |date, _| date <= start_date }
.max_by { |date, _| date }
&.last&.price
db_price_value = db_prices[start_date]&.price
provider_price_value || db_price_value
end
def determine_provisional_status(date:, has_provider_price:, used_locf:, existing_provisional:)
# Provider returned real price => NOT provisional
return false if has_provider_price
# Gap-filled (LOCF) => provisional only if recent weekday
if used_locf
is_weekday = !date.saturday? && !date.sunday?
is_recent = date >= PROVISIONAL_LOOKBACK_DAYS.days.ago.to_date
return is_weekday && is_recent
end
# Otherwise preserve existing status
existing_provisional || false
end
def upsert_rows(rows)
batch_size = 200
total_upsert_count = 0
now = Time.current
rows_with_timestamps = rows.map { |row| row.merge(updated_at: now) }
rows_with_timestamps.each_slice(batch_size) do |batch|
ids = Security::Price.upsert_all(
batch,
unique_by: %i[security_id date currency],
returning: [ "id" ]
)
total_upsert_count += ids.count
end
total_upsert_count
end
def db_price_currency
db_prices.values.first&.currency
end
def prev_price_currency
@prev_price_currency ||= provider_prices.values.first&.currency
end
# Clamp to today (EST) so we never call our price API for a future date (our API is in EST/EDT timezone)
def normalize_end_date(requested_end_date)
today_est = Date.current.in_time_zone("America/New_York").to_date
[ requested_end_date, today_est ].min
end
end