1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 13:35:21 +02:00

First pass at security price reference (#1388)
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

* First pass at security price reference

* Data cleanup

* Synth security fetching does better with a mic_code

* Update test suite

😭

* Update schema.rb

* Update generator.rb
This commit is contained in:
Josh Pigford 2024-10-29 15:37:59 -04:00 committed by GitHub
parent bf695972e4
commit 490f44589e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 155 additions and 88 deletions

View file

@ -1,33 +1,31 @@
class Security::Price < ApplicationRecord
include Provided
before_save :upcase_ticker
validates :ticker, presence: true, uniqueness: { scope: :date, case_sensitive: false }
belongs_to :security
class << self
def find_price(ticker:, date:, cache: true)
result = find_by(ticker:, date:)
def find_price(security:, date:, cache: true)
result = find_by(security:, date:)
result || fetch_price_from_provider(ticker:, date:, cache:)
result || fetch_price_from_provider(security:, date:, cache:)
end
def find_prices(ticker:, start_date:, end_date: Date.current, cache: true)
prices = where(ticker:, date: start_date..end_date).to_a
def find_prices(security:, start_date:, end_date: Date.current, cache: true)
prices = where(security_id: security.id, date: start_date..end_date).to_a
all_dates = (start_date..end_date).to_a.to_set
existing_dates = prices.map(&:date).to_set
missing_dates = (all_dates - existing_dates).sort
if missing_dates.any?
prices += fetch_prices_from_provider(ticker:, start_date: missing_dates.first, end_date: missing_dates.last, cache:)
prices += fetch_prices_from_provider(
security: security,
start_date: missing_dates.first,
end_date: missing_dates.last,
cache: cache
)
end
prices
end
end
private
def upcase_ticker
self.ticker = ticker.upcase
end
end

View file

@ -6,17 +6,18 @@ module Security::Price::Provided
class_methods do
private
def fetch_price_from_provider(ticker:, date:, cache: false)
def fetch_price_from_provider(security:, date:, cache: false)
return nil unless security_prices_provider.present?
response = security_prices_provider.fetch_security_prices \
ticker: ticker,
ticker: security.ticker,
mic_code: security.exchange_mic,
start_date: date,
end_date: date
if response.success? && response.prices.size > 0
price = Security::Price.new \
ticker: ticker,
security: security,
date: response.prices.first[:date],
price: response.prices.first[:price],
currency: response.prices.first[:currency]
@ -28,18 +29,20 @@ module Security::Price::Provided
end
end
def fetch_prices_from_provider(ticker:, start_date:, end_date:, cache: false)
def fetch_prices_from_provider(security:, start_date:, end_date:, cache: false)
return [] unless security_prices_provider.present?
return [] unless security
response = security_prices_provider.fetch_security_prices \
ticker: ticker,
ticker: security.ticker,
mic_code: security.exchange_mic,
start_date: start_date,
end_date: end_date
if response.success?
response.prices.map do |price|
new_price = Security::Price.find_or_initialize_by(
ticker: ticker,
security: security,
date: price[:date]
) do |p|
p.price = price[:price]