mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Match Plaid holding values on current day (#2212)
* Match Plaid holding values on current day * Fix chart timezone issue * Add timezone tests for syncs * Hide sidebars on trades test
This commit is contained in:
parent
470b753833
commit
2000f05453
16 changed files with 242 additions and 21 deletions
|
@ -7,6 +7,7 @@ class AccountableSparklinesController < ApplicationController
|
|||
.where(accountable_type: @accountable.name)
|
||||
.balance_series(
|
||||
currency: family.currency,
|
||||
timezone: family.timezone,
|
||||
favorable_direction: @accountable.favorable_direction
|
||||
)
|
||||
end
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import { Controller } from "@hotwired/stimulus";
|
||||
import * as d3 from "d3";
|
||||
|
||||
const parseLocalDate = d3.timeParse("%Y-%m-%d");
|
||||
|
||||
export default class extends Controller {
|
||||
static values = {
|
||||
data: Object,
|
||||
|
@ -51,10 +53,12 @@ export default class extends Controller {
|
|||
|
||||
_normalizeDataPoints() {
|
||||
this._normalDataPoints = (this.dataValue.values || []).map((d) => ({
|
||||
date: new Date(`${d.date}T00:00:00Z`),
|
||||
date: parseLocalDate(d.date),
|
||||
date_formatted: d.date_formatted,
|
||||
trend: d.trend,
|
||||
}));
|
||||
|
||||
console.log(this._normalDataPoints);
|
||||
}
|
||||
|
||||
_rememberInitialContainerSize() {
|
||||
|
@ -185,7 +189,7 @@ export default class extends Controller {
|
|||
this._normalDataPoints[this._normalDataPoints.length - 1].date,
|
||||
])
|
||||
.tickSize(0)
|
||||
.tickFormat(d3.utcFormat("%b %d, %Y")),
|
||||
.tickFormat(d3.timeFormat("%b %d, %Y")),
|
||||
)
|
||||
.select(".domain")
|
||||
.remove();
|
||||
|
|
|
@ -2,7 +2,7 @@ module Account::Chartable
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def balance_series(currency:, period: Period.last_30_days, favorable_direction: "up", view: :balance, interval: nil)
|
||||
def balance_series(currency:, period: Period.last_30_days, favorable_direction: "up", view: :balance, interval: nil, timezone: nil)
|
||||
raise ArgumentError, "Invalid view type" unless [ :balance, :cash_balance, :holdings_balance ].include?(view.to_sym)
|
||||
|
||||
series_interval = interval || period.interval
|
||||
|
@ -51,7 +51,7 @@ module Account::Chartable
|
|||
WITH dates as (
|
||||
SELECT generate_series(DATE :start_date, DATE :end_date, :interval::interval)::date as date
|
||||
UNION DISTINCT
|
||||
SELECT CURRENT_DATE -- Ensures we always end on current date, regardless of interval
|
||||
SELECT :end_date::date AS date -- Ensures we always end on user's "today" date, regardless of interval
|
||||
)
|
||||
SELECT
|
||||
d.date,
|
||||
|
@ -132,7 +132,8 @@ module Account::Chartable
|
|||
period: period,
|
||||
view: view,
|
||||
interval: interval,
|
||||
favorable_direction: favorable_direction
|
||||
favorable_direction: favorable_direction,
|
||||
timezone: family.timezone
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ class Assistant::Function::GetAccounts < Assistant::Function
|
|||
def historical_balances(account)
|
||||
start_date = [ account.start_date, 5.years.ago.to_date ].max
|
||||
period = Period.custom(start_date: start_date, end_date: Date.current)
|
||||
balance_series = account.balance_series(period: period, interval: "1 month")
|
||||
balance_series = account.balance_series(period: period, interval: "1 month", timezone: family.timezone)
|
||||
|
||||
to_ai_time_series(balance_series)
|
||||
end
|
||||
|
|
|
@ -55,6 +55,7 @@ class Assistant::Function::GetBalanceSheet < Assistant::Function
|
|||
period: period,
|
||||
interval: "1 month",
|
||||
favorable_direction: "up",
|
||||
timezone: family.timezone
|
||||
)
|
||||
|
||||
to_ai_time_series(balance_series)
|
||||
|
|
|
@ -69,7 +69,7 @@ class BalanceSheet
|
|||
end
|
||||
|
||||
def net_worth_series(period: Period.last_30_days)
|
||||
active_accounts.balance_series(currency: currency, period: period, favorable_direction: "up")
|
||||
active_accounts.balance_series(currency: currency, period: period, favorable_direction: "up", timezone: family.timezone)
|
||||
end
|
||||
|
||||
def currency
|
||||
|
|
|
@ -40,12 +40,11 @@ class Holding::BaseCalculator
|
|||
new_quantities
|
||||
end
|
||||
|
||||
def build_holdings(portfolio, date)
|
||||
def build_holdings(portfolio, date, price_source: nil)
|
||||
portfolio.map do |security_id, qty|
|
||||
price = portfolio_cache.get_price(security_id, date)
|
||||
price = portfolio_cache.get_price(security_id, date, source: price_source)
|
||||
|
||||
if price.nil?
|
||||
Rails.logger.warn "No price found for security #{security_id} on #{date}"
|
||||
next
|
||||
end
|
||||
|
||||
|
|
|
@ -21,11 +21,15 @@ class Holding::PortfolioCache
|
|||
end
|
||||
end
|
||||
|
||||
def get_price(security_id, date)
|
||||
def get_price(security_id, date, source: nil)
|
||||
security = @security_cache[security_id]
|
||||
raise SecurityNotFound.new(security_id, account.id) unless security
|
||||
|
||||
price = security[:prices].select { |p| p.price.date == date }.min_by(&:priority)&.price
|
||||
if source.present?
|
||||
price = security[:prices].select { |p| p.price.date == date && p.source == source }.min_by(&:priority)&.price
|
||||
else
|
||||
price = security[:prices].select { |p| p.price.date == date }.min_by(&:priority)&.price
|
||||
end
|
||||
|
||||
return nil unless price
|
||||
|
||||
|
@ -46,7 +50,7 @@ class Holding::PortfolioCache
|
|||
end
|
||||
|
||||
private
|
||||
PriceWithPriority = Data.define(:price, :priority)
|
||||
PriceWithPriority = Data.define(:price, :priority, :source)
|
||||
|
||||
def trades
|
||||
@trades ||= account.entries.includes(entryable: :security).trades.chronological.to_a
|
||||
|
@ -86,7 +90,8 @@ class Holding::PortfolioCache
|
|||
db_prices = security.prices.where(date: account.start_date..Date.current).map do |price|
|
||||
PriceWithPriority.new(
|
||||
price: price,
|
||||
priority: 1
|
||||
priority: 1,
|
||||
source: "db"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -101,7 +106,8 @@ class Holding::PortfolioCache
|
|||
currency: trade.entryable.currency,
|
||||
date: trade.date
|
||||
),
|
||||
priority: 2
|
||||
priority: 2,
|
||||
source: "trade"
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -115,7 +121,8 @@ class Holding::PortfolioCache
|
|||
currency: holding.currency,
|
||||
date: holding.date
|
||||
),
|
||||
priority: 3
|
||||
priority: 3,
|
||||
source: "holding"
|
||||
)
|
||||
end
|
||||
else
|
||||
|
|
|
@ -16,7 +16,9 @@ class Holding::ReverseCalculator < Holding::BaseCalculator
|
|||
Date.current.downto(account.start_date).each do |date|
|
||||
today_trades = portfolio_cache.get_trades(date: date)
|
||||
previous_portfolio = transform_portfolio(current_portfolio, today_trades, direction: :reverse)
|
||||
holdings += build_holdings(current_portfolio, date)
|
||||
|
||||
# If current day, always use holding prices (since that's what Plaid gives us). For historical values, use market data (since Plaid doesn't supply historical prices)
|
||||
holdings += build_holdings(current_portfolio, date, price_source: date == Date.current ? "holding" : nil)
|
||||
current_portfolio = previous_portfolio
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<%= content_for :prev_nav do %>
|
||||
<%= content_for :prev_nav do %>
|
||||
<%= image_tag "logomark-color.svg", class: "w-10 h-10" %>
|
||||
<% end %>
|
||||
|
||||
|
@ -38,8 +38,7 @@
|
|||
<%= family_form.select :country,
|
||||
country_options,
|
||||
{ label: "Country" },
|
||||
required: true
|
||||
%>
|
||||
required: true %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue