Bug 396313

Summary: rounding errors in currency conversion of investment transactions
Product: [Applications] kmymoney Reporter: Al <software>
Component: generalAssignee: KMyMoney Devel Mailing List <kmymoney-devel>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: 5.1.0   
Target Milestone: ---   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed In: 5.1.2
Sentry Crash Report:
Attachments: Minimal data file
Screenshot of conversion Dialog
Minimal data file

Description Al 2018-07-08 14:47:40 UTC
When entering investment transactions with currency conversion and taxes (or fees), then the amount in the checking account is often incorrect (rounding error). I use KMyMoney 4.8.0 (on Ubuntu 16.04 LTS), and I confirmed that the same rounding error still exists in the latest development version from git://anongit.kde.org/kmymoney.git

*** Example ***
From the bank statement: dividend of 9000 JPY, taxes of 1378 JPY, exchange rate  0.8981 CHF for 100 JPY, amount received 68.45 CHF. This is correct: (9000 - 1378) * 0.8981 / 100 = 68.453, rounded to 68.45
In KMyMoney, the conversion dialog asks to convert 1378 JPY to CHF. When I enter the exchange rate (0.008981) then the final amount is 68.60 CHF (i.e., 0.15 CHF too high). When I instead enter the value (12.38 CHF for 1378 JPY) then the final amount is 68.48 CHF (i.e., 0.03 CHF too high).

*** Preferred solution ***
The conversion dialog asks for the final amount (in the above example 7622 JPY), which allows to enter the correct, final amount in CHF. The converted dividend and the converted tax is not showing up in any account statement (only in performance reports and the like), thus a small deviation by a few cents does not matter.

*** Possibly easier to implement solution ***
In case of split transactions, always have the user confirm the converted amount for each part of the split (although in theory the same rate is correct for all those splits as occuring on the same day).
Comment 1 Jack 2021-02-17 00:28:55 UTC
Does this problem still exist in a recent version, such as 5.1.1?  I don't know if this may have been addressed, but there were fixes related to some rounding issues.
Comment 2 Al 2021-02-20 10:28:56 UTC
Hi Jack
Thanks for looking into this. I tested on KMyMoney 5.1.0-1; this is the up-to-date version on the newest on Kubuntu 20.10. 
Outcome: the problem still persists. The deviation from the correct amount is slightly different, but still incorrect (the deviation is even larger then what I reported back in 2018).
Comment 3 Jack 2021-03-11 21:25:53 UTC
Can you provide a small kmy file which demonstrates the problem?  I'm unable to duplicate this, although I may be using different currency for each account and category, and different number of decimal places for each account.  Thanks.
Comment 4 Jack 2021-03-11 21:42:54 UTC
Please try entering the inverse conversion rate - instead of .008981 CHF per JPY try 111.35 JPY per CHF.  I think there is limited precision of the stored conversion rate, so .008981 is only being stored as .0089.  You can check that by looking at the Price Editor (menu item Tools/Prices) and looking at both JPY/CHF and CHF/JPY.
Comment 5 Al 2021-03-12 18:13:51 UTC
Created attachment 136622 [details]
Minimal data file

Thanks, Jack. Attached the requested minimal data file. I have entered the same transaction twice: once using the JPY -> CHF conversion *rate*, once entering the converted CHF *amount* (see comments to the two transactions). Both time the amount in the bank account is inaccurate: 108.00 CHF, should be 108.48 CHF.
Comment 6 Al 2021-03-12 18:17:53 UTC
Created attachment 136623 [details]
Screenshot of conversion Dialog

Workaround was suggested to enter the inverted conversion rate. However, the conversion dialog does not have an option to use the inverted rate, see screenshot ConversionDialog.png
Comment 7 Jack 2021-03-12 18:28:16 UTC
You have to use the Price Editor to enter the inverted conversion rate for that date, and do that before you enter the transaction.  That's why you need to look at both conversion rated in the Price Editor to be sure there isn't a conflicting one already entered for that date.
Comment 8 Al 2021-03-12 20:44:12 UTC
Created attachment 136629 [details]
Minimal data file

Attached the updated minimal data file. I have entered the same transaction three times: (1) using the pre-entered CHF -> JPY conversion rate; (2) using the JPY -> CHF conversion *rate*; (3) using the converted CHF *amount* (see comments to the three transactions). Each time the amount in the bank account is inaccurate: 108.00 CHF, should be 108.48 CHF.

Additionally, note that the conversion rate CHF -> JPY entered in the price editor (1/0.008539 = 117.109731819) is correct when looked up in the price editor (albeit rounded to 117.1097) but incorrect in the conversion dialog when entering a transaction: JPY -> CHF = 0.0085 and CHF -> JPY = 117.1341. The latter is 1/0.0085.
Comment 9 Thomas Baumgart 2021-03-14 09:05:48 UTC
Git commit e9786584d3f23e168ec52029437bfcd46ca5f072 by Thomas Baumgart.
Committed on 14/03/2021 at 09:02.
Pushed by tbaumgart into branch '5.1'.

Differentiate between trading currency and others

If the trading currency of a security has a smaller fraction than the
currencies of the other accounts in a transaction the values will be
truncated. This change fixes the problem by using the fraction
information from the account involved.
FIXED-IN: 5.1.2

M  +13   -9    kmymoney/dialogs/investtransactioneditor.cpp

https://invent.kde.org/office/kmymoney/commit/e9786584d3f23e168ec52029437bfcd46ca5f072