On 2023-11-22, Martin Michlmayr wrote:
> * Bob Wilson <[email protected]> [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.
>

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 [email protected]
_\_V _( ) _( )  @taviso

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/ujqsjs%24fbk%241%40ciao.gmane.io.

Reply via email to