mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 05:09:38 +02:00
Handle bad API data for trade quantity signage (#2416)
This commit is contained in:
parent
f3ab4a27ee
commit
e60b5df442
2 changed files with 59 additions and 3 deletions
|
@ -43,14 +43,14 @@ class PlaidAccount::Investments::TransactionsProcessor
|
|||
end
|
||||
|
||||
entry.assign_attributes(
|
||||
amount: transaction["quantity"] * transaction["price"],
|
||||
amount: derived_qty(transaction) * transaction["price"],
|
||||
currency: transaction["iso_currency_code"],
|
||||
date: transaction["date"]
|
||||
)
|
||||
|
||||
entry.trade.assign_attributes(
|
||||
security: resolved_security_result.security,
|
||||
qty: transaction["quantity"],
|
||||
qty: derived_qty(transaction),
|
||||
price: transaction["price"],
|
||||
currency: transaction["iso_currency_code"]
|
||||
)
|
||||
|
@ -87,4 +87,21 @@ class PlaidAccount::Investments::TransactionsProcessor
|
|||
def transactions
|
||||
plaid_account.raw_investments_payload["transactions"] || []
|
||||
end
|
||||
|
||||
# Plaid unfortunately returns incorrect signage on some `quantity` values. They claim all "sell" transactions
|
||||
# are negative signage, but we have found multiple instances of production data where this is not the case.
|
||||
#
|
||||
# This method attempts to use several Plaid data points to derive the true quantity with the correct signage.
|
||||
def derived_qty(transaction)
|
||||
reported_qty = transaction["quantity"]
|
||||
abs_qty = reported_qty.abs
|
||||
|
||||
if transaction["type"] == "sell" || transaction["amount"] < 0
|
||||
-abs_qty
|
||||
elsif transaction["type"] == "buy" || transaction["amount"] > 0
|
||||
abs_qty
|
||||
else
|
||||
reported_qty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue