Re: Total posting costs and precision issues?

2023-12-11 Thread Martin Michlmayr
* Tavis Ormandy  [2023-11-24 19:13]:
> 2023/11/02 * Sell Shares
> Assets:401K   (-partial_lot(5, "FSKAX", $734.43, 
> 6.456, "[2023/11/01]")) @ $120.00
> 
> ...ugly hack or elegant solution, you decide :)

I can't decide but it's certainly a very creative solution!

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

-- 

--- 
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 ledger-cli+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/20231212030354.GC214467%40jirafa.cyrius.com.


Re: Total posting costs and precision issues?

2023-11-24 Thread Tavis Ormandy
On 2023-11-22, Martin Michlmayr wrote:
> * Bob Wilson  [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 tav...@sdf.org
_\_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 ledger-cli+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/ujqsjs%24fbk%241%40ciao.gmane.io.


Re: Total posting costs and precision issues?

2023-11-21 Thread Martin Michlmayr
* Bob Wilson  [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/

-- 

--- 
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 ledger-cli+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/20231122000327.GA2667362%40jirafa.cyrius.com.


Total posting costs and precision issues?

2023-11-20 Thread Bob Wilson
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

-- 

--- 
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 ledger-cli+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/94432f4e-fa9d-42af-8b76-3f2924c12b3bn%40googlegroups.com.