Bug 372163

Summary: Investment sells giving the wrong result
Product: [Applications] kmymoney Reporter: Jose Arthur Benetasso Villanova <jose.arthur>
Component: generalAssignee: KMyMoney Devel Mailing List <kmymoney-devel>
Status: VERIFIED FIXED    
Severity: normal CC: lukasz.wojnilowicz, ostroffjh
Priority: NOR    
Version: 4.7.2   
Target Milestone: ---   
Platform: Fedora RPMs   
OS: Linux   
Latest Commit: Version Fixed In: 5.0
Sentry Crash Report:
Attachments: kmymoney file
test kmy
quote script

Description Jose Arthur Benetasso Villanova 2016-11-06 22:48:54 UTC
Created attachment 102081 [details]
kmymoney file

The test account in the investment should have balance 0.00, but I got 0.02
Comment 1 Jack 2016-11-07 00:15:34 UTC
Normally I would ask you to provide more details, such as exactly what you did.  However, this looks like a rounding issue.  You bought two shares at a price of 1. You then twice sold one share, each at a price of 1.009.  There are various places in the program where you can set price precision, and I suspect you have a precision somewhere higher than two decimal places.  Neither of the sell transactions alone triggers a visible issue, but the combination does.  This also seems confusing because all three transactions are on the same date, but the sort differently in the investment and checking accounts.  When I moved the two sell transactions to the following day, each showed an amount of 1.01.
Comment 2 allan 2016-11-07 00:26:15 UTC
See Bug 345655 - Rounding problems between checking and investment account.
This was fixed in 4.8.0, so it might be worthwhile upgrading to see if it is your problem.
Comment 3 Jose Arthur Benetasso Villanova 2016-12-04 10:51:18 UTC
The problem that I've simulate in this .kmy file is really fixed, but even if 4.8.0 I still have rounding problems.

I'm trying to extract just the relevant parts of my .kmy file since I'm not willing to share my account data.
Comment 4 wojnilowicz 2017-04-01 06:19:12 UTC
Git commit c6e31c96a03ad5a7ec5e3695bbfa8336181ab81c by Łukasz Wojniłowicz.
Committed on 01/04/2017 at 06:16.
Pushed by wojnilowicz into branch 'master'.

Fix rounding problem with investments

Patch introduces rounding rules per security for fixing bug #372163. It
seems that this broker always rounds amounts down while my broker rounds
amounts depending on the outlying digit, so it couldn't work for both of
us without rules.

Rounding is done in InvestTransactionEditor because it has all needed
informations at hand.

No rounding of shares is done in InvestTransactionEditor::setupPrice.
Transaction from bug #372163 looks as follows:
brokerage:
shares = 1,009 ; value = 1,009
investment:
shares = -1 ; value = 1,009

InvestTransactionEditor::setupPrice causes brokerage to look as follows:
shares = 1,01 ; value = 1,009
As we can see shares and value diverge, which is unacceptable here.

Patch makes assumption that transaction has only single split of
stock/mutual fund/bond.
Related: bug 345655, bug 357784, bug 365177
FIXED-IN:5.0

Differential Revision: https://phabricator.kde.org/D5187

Signed-off-by: Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>

M  +32   -15   kmymoney/dialogs/investtransactioneditor.cpp
M  +31   -0    kmymoney/kmymoneyutils.cpp
M  +22   -0    kmymoney/kmymoneyutils.h
M  +39   -0    kmymoney/mymoney/mymoneysecurity.cpp
M  +27   -8    kmymoney/mymoney/mymoneysecurity.h
M  +1    -0    kmymoney/mymoney/storage/mymoneydbdef.cpp
M  +3    -0    kmymoney/mymoney/storage/mymoneystoragesql.cpp
M  +4    -3    kmymoney/widgets/transaction.cpp
M  +11   -0    kmymoney/wizards/newinvestmentwizard/kinvestmentdetailswizardpage.cpp
M  +104  -90   kmymoney/wizards/newinvestmentwizard/kinvestmentdetailswizardpagedecl.ui
M  +2    -0    kmymoney/wizards/newinvestmentwizard/knewinvestmentwizard.cpp

https://commits.kde.org/kmymoney/c6e31c96a03ad5a7ec5e3695bbfa8336181ab81c
Comment 5 Jose Arthur Benetasso Villanova 2017-04-05 10:50:57 UTC
Unfortunately I found a side efect of this fix.

When I "update stock and currency prices" using "online price update", the price is rounded in 2 decimals, even if my price precision is 8 decimals.

Manual updates work fine.
Comment 6 wojnilowicz 2017-04-05 14:03:50 UTC
That's strange because I didn't fiddle with "online price update".

If you can provide test .kmy file to reproduce the problem then it would help and hasten solving it.

(In reply to Jose Arthur Benetasso Villanova from comment #5)
> Unfortunately I found a side efect of this fix.
> 
> When I "update stock and currency prices" using "online price update", the
> price is rounded in 2 decimals, even if my price precision is 8 decimals.
> 
> Manual updates work fine.
Comment 7 Jose Arthur Benetasso Villanova 2017-04-07 10:51:17 UTC
Created attachment 104913 [details]
test kmy
Comment 8 Jose Arthur Benetasso Villanova 2017-04-07 10:51:52 UTC
Created attachment 104914 [details]
quote script
Comment 9 Jose Arthur Benetasso Villanova 2017-04-07 10:56:49 UTC
Download test.kmy and fakequote.sh.

Configure kmymoney online quote in "Settings" > "Configure KMymoney..." > "Online Quotes"

Create a new Online quote as:
Name: TEST
URL file:///home/ze/bin/fakequote.sh %1
Symbol: '([^"]*)',
Price: '[^"]*','[^"]*','([^"]*)'
Date: '[^"]*','([^"]*)',
Date Format: %y-%m-%d

Check if the investment is pointing to the corret online source.

In investments, right button, "online price update".

Result:
Executing /home/ze/bin/fakequote.sh TEST...
Symbol found: TEST','2017-55-07
Price found: 12.34567890 (12.3457)
Price for TEST updated (id E000001)

But in investment: 12.35000000
Comment 10 wojnilowicz 2017-04-07 16:46:51 UTC
(In reply to Jose Arthur Benetasso Villanova from comment #9)
> Download test.kmy and fakequote.sh.
> 
> Configure kmymoney online quote in "Settings" > "Configure KMymoney..." >
> "Online Quotes"
> 
> Create a new Online quote as:
> Name: TEST
> URL file:///home/ze/bin/fakequote.sh %1
> Symbol: '([^"]*)',
> Price: '[^"]*','[^"]*','([^"]*)'
> Date: '[^"]*','([^"]*)',
> Date Format: %y-%m-%d
> 
> Check if the investment is pointing to the corret online source.
> 
> In investments, right button, "online price update".
> 
> Result:
> Executing /home/ze/bin/fakequote.sh TEST...
> Symbol found: TEST','2017-55-07
> Price found: 12.34567890 (12.3457)
> Price for TEST updated (id E000001)
> 
> But in investment: 12.35000000

The problem is fixed now.

BTW. Your problem wasn't caused by my fix to this problem and you had that problem before, so please don't lie.