Hi everyone,
In trying to answer Chary's question, I have realized I do not have a
very good grip on how to think about "unrealized" gains and losses, and
how ledger calculates them. So I wrote a few test cases to help explain
it to myself. (Can you tell I'm procrastinating a little bit? ;) I am
not sure, but I *may* have discovered a bug: see Example 3. Can someone
explain this to me?
Example 1: you hold a certain quantity of stock throughout the
entire period, during which its value fluctuates. Say you hold 2
shares of AAPL. At the beginning of the period, each is worth $500.
At the end, each is worth $525.
2018/12/31 Opening Balances
Assets:Stocks 2 AAPL @ $500
Equity:Opening Balances
P 2019/01/01 AAPL $500
P 2019/02/01 AAPL $515
P 2019/03/01 AAPL $525
The unrealized gain is then:
(2 AAPL * $525) - (2 AAPL * $500) = $1050 - $1000 = $50
i.e.,
(end quantity * end price) - (beginning quantity * beginning price)
That is exactly what ledger reports, with --unrealized:
ledger -f test.journal bal Assets:Stocks -X '$' --unrealized
$1050 Assets:Stocks
$-50 Equity:Unrealized Gains
--------------------
$1000
======================================================================
Example 2: you hold a certain quantity of stock throughout the
entire period, but you also acquire more at some point in the
middle. Say you hold 2 AAPL at the beginning of the period, each
worth $500. At some later point, when AAPL is up to $510, you buy
one more share. At the end of the period, all three shares are
worth $525.
2018/12/31 Opening Balances
Assets:Stocks 2 AAPL @ $500
Assets:Checking $1000.00
Equity:Opening Balances
P 2019/01/01 AAPL $500
P 2019/02/01 AAPL $510
2019/02/02 Broker
Assets:Stocks 1 AAPL @ $510.00
Assets:Checking
P 2019/03/01 AAPL $525
I would then expect the unrealized gain to be:
(3 AAPL * $525) - (2 AAPL * $500 + 1 AAPL * $510) = $65
Again, this is just what ledger says:
ledger -f test.journal bal Assets:Stocks -X '$' --unrealized
$1575.00 Assets:Stocks
$-65.00 Equity:Unrealized Gains
--------------------
$1510.00
======================================================================
Now for the problematic example:
Example 3: you sell some stock at one point when the price
rises, and reacquire it when the price drops. Say you start holding
2 shares of AAPL, each worth $500. Then, when the price is up to
$540, you sell one share, resulting in a realized gain of $40.
Later, the price drops to $490 and you buy another share. At the
end of the period, the price is $525, and you also have $50 in the bank.
2018/12/31 Opening Balances
Assets:Stocks 2 AAPL @ $500
Equity:Opening Balances
P 2019/02/01 AAPL $540
2019/02/02 Broker
Assets:Stocks -1 AAPL @ $540.00
Assets:Checking
P 2019/02/10 AAPL $490
2019/02/10 Broker
Assets:Stocks 1 AAPL @ $490
Assets:Checking
P 2019/03/01 AAPL $525
Now, what is the unrealized gain here? First of all, it's not clear how
this should be calculated. I thought of two reasonable ways to
calculate it, which give different results. From one perspective, this
example is the same as Example 1: you started and ended the period with
2 shares of AAPL, worth $500 each at the beginning and $525 at the end,
for an unrealized gain of $50. But following the logic of Example 2,
counting unrealized gains only from the first time the share is
acquired, we'd get:
$525 - $500 = $25 unrealized gain for Share #1 (held the whole period)
$525 - $490 = $35 unrealized gain for Share #3 (bought on 2019/02/10)
leading to a total of $60 of unrealized gain.
Ledger actually does it differently. It says we have $150 in
unrealized gains, and $50 in unrealized losses:
ledger -f test.journal bal Assets:Stocks -X '$' --unrealized
$1050.00 Assets:Stocks
$-100.00 Equity
$-150.00 Unrealized Gains
$50.00 Unrealized Losses
--------------------
$950.00
Huh? Here's what I think is going on:
Consider Share #1, held for the whole period.
It first gained $40 in value, lost $50, and then gained $35.
So, $75 in unrealized gains, $50 in unrealized losses.
Share #3 was bought at $490 and then moved to $525 at the end of the period.
So, that's another $35 in unrealized gains.
Together with Share #1's unrealized gains, that's a total of $110.
Share #2 was sold at the first price move, on 2019/02/02. As far as I
can see, it should have *no* unrealized gains or losses. (It has,
instead, a *realized* gain of $40.)
Ledger reports $150 in unrealized gains rather than $110, so it must be
including that $40 from Share #2 as unrealized gains. That seems wrong
to me, since that gain was realized, not unrealized. Is this a bug? Or
am I missing something?
By the way, the results are the same even if I annotate the sale of
Share #2 with the lot price, like:
2019/02/02 Broker
Assets:Stocks -1 AAPL {$500} @ $540.00
Assets:Checking
They're also the same if I delete the redundant price declaration on
2019/02/01. So these can't be what's going wrong -- if something is
wrong! Again, I'm not sure I understand this. Can someone explain it?
Thanks for reading!
--
Best,
Richard
--
---
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].
For more options, visit https://groups.google.com/d/optout.