Question about "4.5.2 Buying and Selling Stock"

28 views
Skip to first unread message

Tobias Pfeiffer

unread,
Aug 31, 2022, 7:39:26 PMAug 31
to ledge...@googlegroups.com
Hi,

I was about to ask about how people model their brokerage account and
keep track of gains etc., because I currently do it as simple as

2022-04-01 Buy Stocks
Assets:Brokerage 50 ETF
Assets:Savings -100 EUR

2022-05-01 Sell Stocks
Assets:Brokerage -10 ETF
Assets:Savings 30 EUR

and then I end up with the problem that I cannot track the capital
gains well. With that model I can see well enough where assets are and
in which form, and also I can see how the overall value developed, but
I cannot easily see, say, how much gains I had in a particular year.

Then I found section 4.5.2 "Buying and Selling Stock"
https://www.ledger-cli.org/3.0/doc/ledger3.html#index-buying-stock
in the ledger handbook which solves exactly that problem. However, I
don't understand the example.

2004/05/01 Stock purchase
Assets:Broker 50 AAPL @ $30.00
Expenses:Broker:Commissions $19.95
Assets:Broker $-1,519.95

2005/08/01 Stock sale
Assets:Broker -50 AAPL {$30.00} @ $50.00
Expenses:Broker:Commissions $19.95
Income:Capital Gains $-1,000.00
Assets:Broker $2,480.05

Clearly the second transaction does not balance if I leave out the lot
price "{$30.00}". What exactly is happening here with the lot price so
that this transaction balances?

Thanks
Tobias



signature.asc

Tobias Pfeiffer

unread,
Aug 31, 2022, 8:03:53 PMAug 31
to ledge...@googlegroups.com
Hi again,

Am Donnerstag, den 01.09.2022, 08:39 +0900 schrieb Tobias Pfeiffer:
> 2005/08/01 Stock sale
> Assets:Broker -50 AAPL {$30.00} @ $50.00
> Expenses:Broker:Commissions $19.95
> Income:Capital Gains $-1,000.00
> Assets:Broker $2,480.05

So this looks like it is basically equivalent to

2005/08/01 Stock sale
Assets:Broker -50 AAPL @ $30.00
Expenses:Broker:Commissions $19.95
Income:Capital Gains $-1,000.00
Assets:Savings $2,480.05

except that the former explicitly keeps the information about the sell
price, and also tracks it in the price history, is that right?

Thanks
Tobias

signature.asc

Martin Michlmayr

unread,
Aug 31, 2022, 10:07:30 PMAug 31
to ledge...@googlegroups.com
* Tobias Pfeiffer <tgpfe...@web.de> [2022-09-01 08:39]:
> However, I don't understand the example.
>
> 2004/05/01 Stock purchase
> Assets:Broker 50 AAPL @ $30.00
> Expenses:Broker:Commissions $19.95
> Assets:Broker $-1,519.95

This example is actually not ideal. It would be better to write:

2004/05/01 Stock purchase
Assets:Broker 50 AAPL {$30.00} @ $30.00

since that makes it clearer that you're creating what ledger calls a
"lot".

In order to understand what's going on with lots, it's very useful to
run "bal --lots". This is important because ledger allows you to
remove lots that do not exist, so it's a good idea to run "bal --lots"
periodically -- anything in red is a bed sign. In fact, the example
from the ledger tutorial is broken: the first transaction creates a
lot with a specific date, but the second transaction doesn't remove
that specific lot (since it doesn't specify the date).

For example the example you posted creates:

50 AAPL {$30.00} [2004-05-01] Assets:Broker

This is a lot. It was a cost ($30) and a date.

> 2005/08/01 Stock sale
> Assets:Broker -50 AAPL {$30.00} @ $50.00
> Expenses:Broker:Commissions $19.95
> Income:Capital Gains $-1,000.00
> Assets:Broker $2,480.05
>
> Clearly the second transaction does not balance if I leave out the lot
> price "{$30.00}". What exactly is happening here with the lot price so
> that this transaction balances?

You sell 50 AAPL for $50, so that's $2500. You also have a capital
gain of $20 per share ($50 - $30), so that's income of $1000; income
is negative in ledger, so that's -$1000.

* Tobias Pfeiffer <tgpfe...@web.de> [2022-09-01 09:03]:
> So this looks like it is basically equivalent to
>
> 2005/08/01 Stock sale
> Assets:Broker -50 AAPL @ $30.00
> Expenses:Broker:Commissions $19.95
> Income:Capital Gains $-1,000.00
> Assets:Savings $2,480.05
>
> except that the former explicitly keeps the information about the sell
> price, and also tracks it in the price history, is that right?

Well, apart from the fact that it's incorrect (*) because you're in fact
not selling at $30.

(*) Incorrect in the sense that it doesn't represent the true nature
of the transaction. It's not a faithful representation of what
happened.

The basic problem is that selling at $30 and selling something bought
at $30 at $50 leads to the same result:

50 * $30 = 1500

50 * $50 = 2500
50 * ($50-$30) = -1000 (capital gain)
----------------
Total 1500

So it's easy to get these sales wrong. Maybe ledger should post the
capital gains to some account automatically so reduce errors but the
problem is that there might not be a single capital gains account, so
you'd need special syntax to specify where the capital gains/loss
goes.

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

Tobias Pfeiffer

unread,
Sep 1, 2022, 8:29:04 AMSep 1
to ledge...@googlegroups.com
Martin,

thanks for your in-depth reply.

Am Donnerstag, den 01.09.2022, 10:07 +0800 schrieb Martin Michlmayr:
> > 2005/08/01 Stock sale
> > Assets:Broker -50 AAPL {$30.00} @ $50.00
> > Expenses:Broker:Commissions $19.95
> > Income:Capital Gains $-1,000.00
> > Assets:Broker $2,480.05
> >
> > Clearly the second transaction does not balance if I leave out the
> > lot price "{$30.00}". What exactly is happening here with the lot
> > price so that this transaction balances?
>
> You sell 50 AAPL for $50, so that's $2500. You also have a capital
> gain of $20 per share ($50 - $30), so that's income of $1000; income
> is negative in ledger, so that's -$1000.

I think I kind of understand your explanation, but I have trouble
understanding on a technical level how that transaction balances.
When you write "you *also* have a capital gain of $20 per share", can I
imagine that this lot notation means that there is an implicit "hidden
posting"
Assets:Broker (($50 - $30) * 50)
that is used to balance the "Income:Capital Gains" posting?

---

On a related note, today I found out that for the purposes of computing
capital gains, the way things are done in Japan is that on the point in
time where you sell shares of a particular company X, an average buy
price is computed for all the X shares you bought in the past, like

buy_price_avg = (total_price_1 + ... + total_price_n) /
(count_1 + ... + count_n)

and then the taxable capital gain is computed as

sell_count * (sell_price - buy_price_avg)

which means that you don't sell the shares from a particular lot, but
from some averaged lot. How would I model that? With your advice from
above, I have tried the following:

2004/05/01 Stock purchase
Assets:Broker 10 ETF @ 1,000 JPY
Assets:Savings -10,000 JPY

2004/06/01 Stock purchase
Assets:Broker 20 ETF @ 1,300 JPY
Assets:Savings -26,000 JPY

2004/07/01 Compute average
Assets:Broker -10 ETF @ 1,000 JPY
Assets:Broker -20 ETF @ 1,300 JPY
Assets:Broker 30 ETF @ 1,200 JPY ; weighted average

2004/07/01 Stock sell
Assets:Broker -20 ETF {1,200 JPY} @ 1,500 JPY
Income:Capital Gains -6,000 JPY
Assets:Savings 30,000 JPY

and that works well in the sense that both `ledger bal` and `ledger bal
--lot-prices` look good:

$ ledger -f example.ledger bal --lot-prices
10 ETF {1,200 JPY}
-6,000 JPY Assets
10 ETF {1,200 JPY} Broker
-6,000 JPY Savings
-6,000 JPY Income:Capital Gains
--------------------
10 ETF {1,200 JPY}
-12,000 JPY

However, `ledger bal --lots` still shows all of the lots that have ever
existed, it's a bit overwhelming:

$ ledger -f example2.ledger bal --lots
10 ETF {1,000 JPY} [04-May-01]
-10 ETF {1,000 JPY} [04-Jul-01]
-20 ETF {1,200 JPY}
30 ETF {1,200 JPY} [04-Jul-01]
20 ETF {1,300 JPY} [04-Jun-01]
-20 ETF {1,300 JPY} [04-Jul-01]
-6,000 JPY Assets
10 ETF {1,000 JPY} [04-May-01]
-10 ETF {1,000 JPY} [04-Jul-01]
-20 ETF {1,200 JPY}
30 ETF {1,200 JPY} [04-Jul-01]
20 ETF {1,300 JPY} [04-Jun-01]
-20 ETF {1,300 JPY} [04-Jul-01] Broker
-6,000 JPY Savings
-6,000 JPY Income:Capital Gains
--------------------
10 ETF {1,000 JPY} [04-May-01]
-10 ETF {1,000 JPY} [04-Jul-01]
-20 ETF {1,200 JPY}
30 ETF {1,200 JPY} [04-Jul-01]
20 ETF {1,300 JPY} [04-Jun-01]
-20 ETF {1,300 JPY} [04-Jul-01]
-12,000 JPY

Is that a problem? Should I try and get rid of these lots that I
thought should have disappeared in the "Compute average" transaction?
How would I do that?

Thank you,
Tobias


Tobias Pfeiffer

unread,
Sep 1, 2022, 7:25:03 PMSep 1
to ledge...@googlegroups.com
Hi again,

Am Donnerstag, den 01.09.2022, 21:28 +0900 schrieb Tobias Pfeiffer:
> On a related note, today I found out that for the purposes of
> computing capital gains, the way things are done in Japan is that on
> the point in time where you sell shares of a particular company X, an
> average buy price is computed for all the X shares you bought in the
> past, like
>
> buy_price_avg = (total_price_1 + ... + total_price_n) /
> (count_1 + ... + count_n)
>
> and then the taxable capital gain is computed as
>
> sell_count * (sell_price - buy_price_avg)
>
> which means that you don't sell the shares from a particular lot, but
> from some averaged lot. How would I model that? With your advice from
> above, I have tried the following: [...]

I should have tried just a bit harder before sending my message ;-)

The following works fine, it seems we need to be explicit about both
lot prices and dates when computing the average:

2004/05/01 Stock purchase
Assets:Broker 10 ETF @ 1,000 JPY
Assets:Savings -10,000 JPY

2004/06/01 Stock purchase
Assets:Broker 20 ETF @ 1,300 JPY
Assets:Savings -26,000 JPY

2004/07/01 Re-evaluate
Assets:Broker -10 ETF {1,000 JPY} [2004/05/01] @ 1,000 JPY
Assets:Broker -20 ETF {1,300 JPY} [2004/06/01] @ 1,300 JPY
Assets:Broker 30 ETF @ 1,200 JPY ; weighted average

2004/07/01 Stock sell
Assets:Broker -20 ETF {1,200 JPY} [2004/07/01] @ 1,500 JPY
Income:Capital Gains -6,000 JPY
Assets:Savings 30,000 JPY

This completely eliminates the original lots in the "Re-evaluate"
transaction, so that only the ones with the average prices remain:

$ ledger -f example.ledger bal --lots
10 ETF {1,200 JPY} [04-Jul-01]
-6,000 JPY Assets
10 ETF {1,200 JPY} [04-Jul-01] Broker
-6,000 JPY Savings
-6,000 JPY Income:Capital Gains
--------------------
10 ETF {1,200 JPY} [04-Jul-01]
-12,000 JPY

Not sure if I'm going to rewrite my transaction history like that, but
good to know how it works ;-)

Thanks
Tobias


Martin Michlmayr

unread,
Sep 2, 2022, 3:17:15 AMSep 2
to ledge...@googlegroups.com
* Tobias Pfeiffer <tgpfe...@web.de> [2022-09-02 08:24]:
> The following works fine, it seems we need to be explicit about both
> lot prices and dates when computing the average:

Yes, you always have to use the same details.

If a lot date is specified, you have to specify the lot date.
If a lot note is specified, you have to specify the lot note.

As I mentioned before, ledger allows you to remove lots that don't
exist, so it's easy to get this wrong. "bal --lots" is your friend.
You've figured this out in the meantime.

> 2004/05/01 Stock purchase
> Assets:Broker 10 ETF @ 1,000 JPY
> Assets:Savings -10,000 JPY
>
> 2004/06/01 Stock purchase
> Assets:Broker 20 ETF @ 1,300 JPY
> Assets:Savings -26,000 JPY
>
> 2004/07/01 Re-evaluate
> Assets:Broker -10 ETF {1,000 JPY} [2004/05/01] @ 1,000 JPY
> Assets:Broker -20 ETF {1,300 JPY} [2004/06/01] @ 1,300 JPY

Yes, unfortunately you have to specify it manually. But this is
correct.

Martin Michlmayr

unread,
Sep 2, 2022, 3:26:26 AMSep 2
to ledge...@googlegroups.com
* Tobias Pfeiffer <tgpfe...@web.de> [2022-09-01 21:28]:
> > > 2005/08/01 Stock sale
> > > Assets:Broker -50 AAPL {$30.00} @ $50.00
> > > Expenses:Broker:Commissions $19.95
> > > Income:Capital Gains $-1,000.00
> > > Assets:Broker $2,480.05

> I think I kind of understand your explanation, but I have trouble
> understanding on a technical level how that transaction balances.
> When you write "you *also* have a capital gain of $20 per share", can I
...

Let me try to explain it the way I see it.

Assets:Broker contains 50 AAPL {$30.00}. We agree on that. This is
$1500. Now we can argue what it's really worth right now because the
value of AAPL goes up and down but we know that Assets:Broker contains
50 AAPL with a *book value* of $30 each and that's a total of $1500.
You can verify this with "bal Assets:Broker -B"

So now when you do:

Assets:Broker -50 AAPL {$30.00} @ $50.00

{$30.00} is the cost. @ $50.00 is the price. You're selling
something with a cost of $30.00 (the book value) for a price of $50.

So you're basically saying: hey, this APPL I own with a total book
value of $1500... you know what, I'm selling it for $2500

So Assets:Broker goes down by $1500 (remember, the book value!) and
you get $2500 (what you sold it for). That doesn't quite balance,
does it? So where does the difference come from? You made a gain.

So basically we have:

Assets:Broker -$1500 of APPL [book value]
Assets:Broker $2500 of cash
Income:Capital gains -$1000 (the balancing figure)

> imagine that this lot notation means that there is an implicit "hidden
> posting"
> Assets:Broker (($50 - $30) * 50)
> that is used to balance the "Income:Capital Gains" posting?

I don't think that's correct. Like I showed, Assets:Broker is just
$1500 (the book value).

At least this is how I understand it. Maybe I'm wrong, but my
explanation appears to make sense. ;)
Reply all
Reply to author
Forward
0 new messages