Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Total posting costs and precision issues?

57 views
Skip to first unread message

Bob Wilson

unread,
Nov 21, 2023, 12:05:18 AM11/21/23
to Ledger
Hi folks,
   I have some stock purchases that I have specified using total costs. Because of not-round-numbers, I'm having trouble specifying the lot so I can sell it cleanly.

Here's an example:

2023/11/01 * Buy Shares
    Assets:401K                             6.456 FSKAX @@ $734.43
    Income:Opening Balances              -$734.43

2023/11/02 * Sell Shares
    Assets:401K                            -6.456 FSKAX {$113.7592936803} [2023/11/01] @ $120.00
    Income:Capital Gains                  -$40.29
    Assets:401K                           $774.72

The first transaction uses the total posting cost. To record the second transaction, I ran, `ledger --lots balance Assets:401K`. I copied that $113 number verbatim as printed.

So I've sold all the shares, but now when I run `ledger --lots balance Assets:401K`, the result is:

             $774.72
6.456 FSKAX {$113.7592936803} [2023/11/01]
-6.456 FSKAX {$113.7592936803} [2023/11/01]  Assets:401K

So it looks like the lot I sold doesn't quite match what ledger thinks I owned. I anticipate the issue is that the precision used in printing is different than the precision used to store lots internally.

I tried selling using the total posting cost:

2023/11/02 * Sell Shares
    Assets:401K                            -6.456 FSKAX {{$734.43}} [2023/11/01] @ $120.00
    Income:Capital Gains                  -$40.29
    Assets:401K                           $774.72

That works great as long as I sell whole lots. But as soon as I try to sell a partial lot, it doesn't work at all. (The capital gains are not calculated correctly.)

The best idea I have right now is to just not use total posting costs, and always use the per-share price. But I find total posting costs to be convenient for many use cases.

Any suggestions?

Thanks folks!
Bob Wilson

Martin Michlmayr

unread,
Nov 21, 2023, 7:03:50 PM11/21/23
to ledge...@googlegroups.com
* Bob Wilson <mills...@gmail.com> [2023-11-20 21:05]:
> So it looks like the lot I sold doesn't quite match what ledger thinks I
> owned. I anticipate the issue is that the precision used in printing is
> different than the precision used to store lots internally.

The only solution I'm aware of is:

2023/11/01 * Buy Shares
Assets:401K 6.456 FSKAX {$113.7592936803} [2023/11/01] @@ $734.43
Income:Opening Balances -$734.43

2023/11/02 * Sell Shares
Assets:401K -6.456 FSKAX {$113.7592936803} [2023/11/01] @ $120.00
Income:Capital Gains -$40.29
Assets:401K $774.72

I don't know if there's a better one.

--
Martin Michlmayr
https://www.cyrius.com/

Tavis Ormandy

unread,
Nov 24, 2023, 2:13:13 PM11/24/23
to ledge...@googlegroups.com
I have a solution... but, I guess it's subjective if it's better :)

The problem, as Bob said, is that there is no way of specifying enough
precision in the transaction syntax, except {{}} which doesn't work for
partial lots. You can use amounts in expressions though, so you could
make a function to do it...

It's a bit tricky, because you have to be careful to never convert
anything or it will get truncated, and then you're back at square one.

How about this, add this to to the top if your ledger:

define partial_lot(count, commodity, lotprice, lotsize, lotdate) = ((to_amount(to_string(lotsize) + " " + commodity + "{{" + lotprice + "}}" + lotdate) / lotsize ) * count)

Now you can do this, to sell 5 shares from a lot of 6.456:

2023/11/02 * Sell Shares
Assets:401K (-partial_lot(5, "FSKAX", $734.43, 6.456, "[2023/11/01]")) @ $120.00
Income:Capital Gains -$40.29
Assets:401K $774.72

The idea is that rather than converting anything into a string
({$113.123123123...}) you leave it as an amount. If you divide that
amount by the lot size, you get the value of 1 FSKAX, then you can
multipy by the desired partial lot size.

...ugly hack or elegant solution, you decide :)

Tavis.

--
_o) $ lynx lock.cmpxchg8b.com
/\\ _o) _o) $ finger tav...@sdf.org
_\_V _( ) _( ) @taviso

Reply all
Reply to author
Forward
0 new messages