mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 13:35:21 +02:00
Market data sync refinements (#2252)
* Exchange rate syncer implementation * Security price syncer * Fix issues with provider API * Add back prod schedule * Add back price and exchange rate syncs to account syncs * Remove unused stock_exchanges table
This commit is contained in:
parent
6917cecf33
commit
6dc1d22672
38 changed files with 1206 additions and 1615 deletions
|
@ -1,27 +0,0 @@
|
|||
module Account::Convertible
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def sync_required_exchange_rates
|
||||
unless requires_exchange_rates?
|
||||
Rails.logger.info("No exchange rate sync needed for account #{id}")
|
||||
return
|
||||
end
|
||||
|
||||
affected_row_count = ExchangeRate.sync_provider_rates(
|
||||
from: currency,
|
||||
to: target_currency,
|
||||
start_date: start_date,
|
||||
)
|
||||
|
||||
Rails.logger.info("Synced #{affected_row_count} exchange rates for account #{id}")
|
||||
end
|
||||
|
||||
private
|
||||
def target_currency
|
||||
family.currency
|
||||
end
|
||||
|
||||
def requires_exchange_rates?
|
||||
currency != target_currency
|
||||
end
|
||||
end
|
82
app/models/account/market_data_syncer.rb
Normal file
82
app/models/account/market_data_syncer.rb
Normal file
|
@ -0,0 +1,82 @@
|
|||
class Account::MarketDataSyncer
|
||||
attr_reader :account
|
||||
|
||||
def initialize(account)
|
||||
@account = account
|
||||
end
|
||||
|
||||
def sync_market_data
|
||||
sync_exchange_rates
|
||||
sync_security_prices
|
||||
end
|
||||
|
||||
private
|
||||
def sync_exchange_rates
|
||||
return unless needs_exchange_rates?
|
||||
return unless ExchangeRate.provider
|
||||
|
||||
pair_dates = {}
|
||||
|
||||
# 1. ENTRY-BASED PAIRS – currencies that differ from the account currency
|
||||
account.entries
|
||||
.where.not(currency: account.currency)
|
||||
.group(:currency)
|
||||
.minimum(:date)
|
||||
.each do |source_currency, date|
|
||||
key = [ source_currency, account.currency ]
|
||||
pair_dates[key] = [ pair_dates[key], date ].compact.min
|
||||
end
|
||||
|
||||
# 2. ACCOUNT-BASED PAIR – convert the account currency to the family currency (if different)
|
||||
if foreign_account?
|
||||
key = [ account.currency, account.family.currency ]
|
||||
pair_dates[key] = [ pair_dates[key], account.start_date ].compact.min
|
||||
end
|
||||
|
||||
pair_dates.each do |(source, target), start_date|
|
||||
ExchangeRate.sync_provider_rates(
|
||||
from: source,
|
||||
to: target,
|
||||
start_date: start_date,
|
||||
end_date: Date.current
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def sync_security_prices
|
||||
return unless Security.provider
|
||||
|
||||
account_securities = account.trades.map(&:security).uniq
|
||||
|
||||
return if account_securities.empty?
|
||||
|
||||
account_securities.each do |security|
|
||||
security.sync_provider_prices(
|
||||
start_date: first_required_price_date(security),
|
||||
end_date: Date.current
|
||||
)
|
||||
|
||||
security.sync_provider_details
|
||||
end
|
||||
end
|
||||
|
||||
# Calculates the first date we require a price for the given security scoped to this account
|
||||
def first_required_price_date(security)
|
||||
account.trades.with_entry
|
||||
.where(security: security)
|
||||
.where(entries: { account_id: account.id })
|
||||
.minimum("entries.date")
|
||||
end
|
||||
|
||||
def needs_exchange_rates?
|
||||
has_multi_currency_entries? || foreign_account?
|
||||
end
|
||||
|
||||
def has_multi_currency_entries?
|
||||
account.entries.where.not(currency: account.currency).exists?
|
||||
end
|
||||
|
||||
def foreign_account?
|
||||
account.currency != account.family.currency
|
||||
end
|
||||
end
|
|
@ -7,6 +7,7 @@ class Account::Syncer
|
|||
|
||||
def perform_sync(sync)
|
||||
Rails.logger.info("Processing balances (#{account.linked? ? 'reverse' : 'forward'})")
|
||||
sync_market_data
|
||||
sync_balances
|
||||
end
|
||||
|
||||
|
@ -19,4 +20,18 @@ class Account::Syncer
|
|||
strategy = account.linked? ? :reverse : :forward
|
||||
Balance::Syncer.new(account, strategy: strategy).sync_balances
|
||||
end
|
||||
|
||||
# Syncs all the exchange rates + security prices this account needs to display historical chart data
|
||||
#
|
||||
# This is a *supplemental* sync. The daily market data sync should have already populated
|
||||
# a majority or all of this data, so this is often a no-op.
|
||||
#
|
||||
# We rescue errors here because if this operation fails, we don't want to fail the entire sync since
|
||||
# we have reasonable fallbacks for missing market data.
|
||||
def sync_market_data
|
||||
Account::MarketDataSyncer.new(account).sync_market_data
|
||||
rescue => e
|
||||
Rails.logger.error("Error syncing market data for account #{account.id}: #{e.message}")
|
||||
Sentry.capture_exception(e)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue