Bug 420056

Summary: BUY/SELL information ignored when importing OFX investment transactions
Product: [Applications] kmymoney Reporter: Dawid Wróbel <me>
Component: importerAssignee: KMyMoney Devel Mailing List <kmymoney-devel>
Status: RESOLVED FIXED    
Severity: major    
Priority: NOR    
Version: git (master)   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In: 5.1.0
Sentry Crash Report:
Attachments: Sample OFX file

Description Dawid Wróbel 2020-04-14 03:34:57 UTC
Created attachment 127525 [details]
Sample OFX file

SUMMARY
I noticed that BUY/SELL information is completely ignored and KMyMoney defaults to just assuming each transaction is a "BUY" one when importing investment transactions using OFX.


STEPS TO REPRODUCE
1. Create an investment and a corresponding brokerage account
2. Import the OFX file attached,
3. Assign accounts accordingly

OBSERVED RESULT
1. Notice that all investment transactions are of Sell type
2. Notice that brokerage account transactions are all on the same side of ledger. In particular notice 

EXPECTED RESULT
3. Investment transactions should be of both Sell and Buy type, matching the OFX file
4. Brokerage account transactions should be on both sides of the ledger. In particular notice that the EXCHANGE IN/OUT transactions from 4/08 do not zero out.


I debugged it briefly and can see that OFXImporter::ofxTransactionCallback() assigns t.m_eAction = eMyMoney::Transaction::Action::Sell/Buy flags correctly, which would indicate that the issue is downstream. However, I also tested with identical information using QIF statement and could not reproduce the issue, so it seems to be OFX specific after all.
Comment 1 Jack 2020-04-14 13:28:09 UTC
I've been importing with OFX direct for years, from at least two institutions, and I have not seen this.  (Merrill Lynch has big problems with divident reinvestments, but that's a separate issue.)  My guess is it is some oddity in how your brokerage prepares the OFX.  They probably have both buy and sell transactions, but both using positive amounts for the funds.  I can't speak easily to the internals, but as I remember QIF import is done entirely within KMM, but the OFX is done using libOFX.  As one test, you might configure the aqbanking ofx importer and see if it does the same thing as libOFX.  If it does, then the problem is probably within the OFX you are importing.  You can also just look in the OFX statement itself.  Unfortunately, if my guess is correct, I'm not sure to deal with it, other than perhaps making the assumption that all buy transactions should use a negative cash amount (assuming I've got it right and it's not the other way around.)
Comment 2 Dawid Wróbel 2020-04-14 18:33:30 UTC
Jack, look like you're right. Per OFX 2.2 spec (https://www.ofx.net/downloads/OFX%202.2.pdf), section 13.3.3:

"In a transaction, the signage of UNITS and TOTALs is determined by the transaction's effect on the underlying cash balance (an investment sell would contain a positive TOTAL and negative UNITS whereas an investment buy would contain a negative TOTAL and positive UNITS)."

All of the UNITs and TOTALs in the file provided by my Fidelity 401k brokerage account are positive.

However, I did check with Moneydance and they import these transactions properly.

I'll check with aqbanking and report.
Comment 3 Dawid Wróbel 2020-04-15 22:54:57 UTC
Git commit 8bd68cd8a2e0ebadedc04ff665d6704d6395d8a0 by Dawid Wróbel.
Committed on 15/04/2020 at 22:54.
Pushed by wrobelda into branch 'bug-420056'.

Add option to enable fixing of the buy/sell signs

Some providers do not follow the OFX spec and do not assign the correct
positive/negative signage to amount (TOTALs) and quantity (UNITs)
values assosciated with BUY/SELL investment transactions (see OFX2.2,
section 13.3.3). This change adds option to OFX import dialog and
account's online settings to force the signage to follow the the spec.
GUI:

M  +1    -0    kmymoney/plugins/ofx/import/dialogs/konlinebankingstatus.cpp
M  +19   -2    kmymoney/plugins/ofx/import/dialogs/konlinebankingstatusdecl.ui
M  +18   -1    kmymoney/plugins/ofx/import/importoption.ui
M  +96   -74   kmymoney/plugins/ofx/import/ofximporter.cpp

https://commits.kde.org/kmymoney/8bd68cd8a2e0ebadedc04ff665d6704d6395d8a0
Comment 4 Dawid Wróbel 2020-04-17 00:01:32 UTC
Git commit 50da0c1ac7d911c024e8d4b1b9cd0a925aba858f by Dawid Wróbel.
Committed on 17/04/2020 at 00:00.
Pushed by wrobelda into branch 'master'.

Add option to enable fixing of the buy/sell signs

Some providers do not follow the OFX spec and do not assign the correct
positive/negative signage to amount (TOTALs) and quantity (UNITs)
values assosciated with BUY/SELL investment transactions (see OFX2.2,
section 13.3.3). This change adds option to OFX import dialog and
account's online settings to force the signage to follow the the spec.
GUI:

M  +1    -0    kmymoney/plugins/ofx/import/dialogs/konlinebankingstatus.cpp
M  +19   -2    kmymoney/plugins/ofx/import/dialogs/konlinebankingstatusdecl.ui
M  +18   -1    kmymoney/plugins/ofx/import/importoption.ui
M  +96   -74   kmymoney/plugins/ofx/import/ofximporter.cpp

https://commits.kde.org/kmymoney/50da0c1ac7d911c024e8d4b1b9cd0a925aba858f