1
0
Fork 0
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)
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

This commit is contained in:
Zach Gollwitzer 2025-06-26 09:54:25 -04:00 committed by GitHub
parent f3ab4a27ee
commit e60b5df442
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 3 deletions

View file

@ -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