Bug 502652 - Import of stock transactions with a separate fee results in a calculation error
Summary: Import of stock transactions with a separate fee results in a calculation error
Status: REPORTED
Alias: None
Product: kmymoney
Classification: Applications
Component: importer (show other bugs)
Version: 5.1.3
Platform: Microsoft Windows Microsoft Windows
: NOR normal
Target Milestone: ---
Assignee: KMyMoney Devel Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-04-10 20:15 UTC by k6lw
Modified: 2025-04-13 09:11 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
Sample CSV import File (1.69 KB, text/csv)
2025-04-13 00:18 UTC, k6lw
Details
Analysis of the problem (259.12 KB, application/pdf)
2025-04-13 07:42 UTC, Thomas Baumgart
Details
Analysis of the problem (261.59 KB, application/pdf)
2025-04-13 09:11 UTC, Thomas Baumgart
Details

Note You need to log in before you can comment on or make changes to this bug.
Description k6lw 2025-04-10 20:15:51 UTC
SUMMARY
I imported a bunch of stock transactions but noticed the brokerage account total was not correct. For any transaction where there was a fee associated with the buy or sell the amount shown was incorrect. HOWEVER if I click on Edit for the record, the amount changes to the correct amount.  In other cases sometimes I need to subtract the fee from the total to calculate the share price to four digits to arrive at the correct amount. In other cases I need to add the fee to the total and divide by the number of shares to reach the correct amount.

STEPS TO REPRODUCE
1. Import stock trades from a CSV file
2. Compare any transactions with a fee in addition to the trade
3. Notice the amount shown in the ledger is wrong. Click on edit the record and the amount changes to the correct value.

OBSERVED RESULT
Record imports but total amount is wrong


EXPECTED RESULT
Import transactions and have totals be correct

SOFTWARE/OS VERSION
Windows: Windows 10 (I've also seen the same issue when running Kubuntu)
macOS: 
(available in the Info Center app, or by running `kinfo` in a terminal window)
Linux/KDE Plasma: 
KDE Plasma Version: 5+
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Some stocks have a tax in addition to a fee. Haven't seen any way to have this captured during import so I manually add those records.
Comment 1 Jack 2025-04-10 20:50:31 UTC
What version are you running?  What type of import are you using: ofx, qif, csv?  If you are running 5.1.3, can you try a development version, just to be sure it hasn't been fixed in the meantime?
I'm not aware of any recent changes in the import code, but it's certainly possible fees are processed in the wrong order to get the right price per share if you are starting with a total price for the transaction.
Can you provide a kmy and import file which reproduce the issue?  Note that if the amount looks wrong, but gets corrected when you accept the transaction, that's probably OK, but if you have to edit and save it, there may be something to fix.
Comment 2 k6lw 2025-04-13 00:18:00 UTC
Created attachment 180216 [details]
Sample CSV import File

Here is a sample stock transaction file containing both sells and buys, with and without "fees".
When importing the file any of the transactions with a fee will appear in the ledger with an incorrect amount. If you click on edit SOMETIMES the amount changes to the correct value. Otherwise I either need to subtract the fee from the total and divide the result by the number of shares to determine the share price to 4 digits and then the correct value is obtained. In other cases I need to add the fee to the total then divide by the number of shares to obtain the correct value.
Comment 3 k6lw 2025-04-13 00:31:03 UTC
Answer to questions. File import of CSV. Sample file submitted

I confirmed the error appears for both 5.1.3-eef04f1 on windows and in 5.1.3 in Linux.

Complete rundown:
OS Windows 10 Pro 22H2 64 bit Version 19045.4842 
KMyMoney version 5.1.3-eef04f1

On Import I used the following settings:
Date column 1, Action column 2, Fee column 8, Price column 6, Quantity column 5, Amount column 9 price faction=1, Symbol column 3
Compare ledger entries with amount shown on the spreadsheet. e.g. Stock SPHQ's amount shown in the ledger should be $4589.19.
The amount displayed will be different. Click on EDIT in the ledger and the total amount will change to the correct value. There are 9 other rows where you can see the differences.

FYI, It didn't do it today while creating this test file, but yesterday when importing the file I told KMyMoney that row 1 was headers. When it came time to import the file it complained about an incorrect date format. The program was trying to import the header row. I backed out and just told the importer to start on row 2 and then the import worked with the exception of these errors which I can reproduce at will.

For Kubuntu:
Version 24.0.4
Plasma 5.27.12
KDE Framework 5.115
Kernal: 6.8.0-57 64 bit
KMyMoney 5.1.3
All other settings identical. Same results
Comment 4 Thomas Baumgart 2025-04-13 07:42:19 UTC
Created attachment 180220 [details]
Analysis of the problem

I wrote up what I have found with details and screenshots and attached it as a PDF document.
Comment 5 Thomas Baumgart 2025-04-13 09:10:45 UTC
Git commit de1965c949066b51e61a419099b9c9bbc5637341 by Thomas Baumgart.
Committed on 13/04/2025 at 09:10.
Pushed by tbaumgart into branch 'master'.

Transport more investment attributes in statement file

The members of that object were correctly transported between
CSV importer and statement reader but the dump to the
statement file was missing them.

M  +8    -0    kmymoney/mymoney/mymoneystatement.cpp

https://invent.kde.org/office/kmymoney/-/commit/de1965c949066b51e61a419099b9c9bbc5637341
Comment 6 Thomas Baumgart 2025-04-13 09:11:38 UTC
Created attachment 180221 [details]
Analysis of the problem

Updated analysis after more values were written to the statement file.