Dear Richar,
Thanks for this very useful example.
I don't use ledger's commodity conversion methods ({}, @, etc.). I instead
use the "trading accounts
<https://github.com/ledger/ledger/wiki/Multiple-currencies-with-currency-trading-accounts>"
method of recording commodity conversions. I wanted to test how that
method would perform with this example.
2018/12/31 Opening Balances
Assets:Stocks 2 AAPL
Trading:Stocks:USD -2 AAPL
Trading:USD:Stocks $1000.00
Equity:Opening Balances $-1000.00
P 2019/02/01 AAPL $540.00
2019/02/02 Sell share
Assets:Stocks -1 AAPL
Trading:Stocks:USD 1 AAPL
Trading:USD:Stocks $-500.00
Income:Capital Gain $-40.00
Assets:Checking $540.00
P 2019/02/01 AAPL $540.00
2019/02/10 Buy share
Assets:Stocks 1 AAPL
Trading:Stocks:USD -1 AAPL
Trading:USD:Stocks $490.00
Assets:Checking $-490.00
P 2019/03/01 AAPL $525
I query unrealized gains+losses using
$ ledger -f temp.ledg bal Trading -X '$'
$-60.00 Trading
$-1050.00 Stocks:USD
$990.00 USD:Stocks
--------------------
$-60.00
This shows the unrealized gains correctly at $-60.00 (it's unrealized
income, so the sign is negative). Similarly, if you record the lot price
as '@ 500.00', you can get similar results with your example:
2018/12/31 Opening Balances
Assets:Stocks 2 AAPL @ $500.00
Equity:Opening Balances
P 2019/02/01 AAPL $540.00
2019/02/02 Broker
Assets:Stocks -1 AAPL @ $500.00
Income:Capital Gains $-40.00
Assets:Checking $540.00
P 2019/02/10 AAPL $490.00
2019/02/10 Broker
Assets:Stocks 1 AAPL @ $490.00
Assets:Checking $-490.00
P 2019/03/01 AAPL $525.00
if you query for the unrealized gain now, you get:
$ ledger -f temp.ldg bal -X '$' --unrealized
$1100.00 Assets
$50.00 Checking
$1050.00 Stocks
$-1060.00 Equity
$-1000.00 Opening Balances
$-70.00 Unrealized Gains
$10.00 Unrealized Losses
$-40.00 Income:Capital Gains
--------------------
0
That shows the correct figure. However, if you follow the ledger syntax of
recording sales as "1 AAPL {$500.00} @ $540.00", it doesn't work.
Now for the additional bug:
As per the ledger manual
<https://www.ledger-cli.org/3.0/doc/ledger3.html#Fixing-Lot-Prices>, "1
AAPL {$500.00}" and "1 AAPL @ $500.00" are equivalent. However, I get
different results depending on how I record the initial transaction.
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 @ $500 ;@ $540.00
Income:Capital Gains -$40.00
Assets:Checking $540.00
P 2019/02/10 AAPL $490
2019/02/10 Broker
Assets:Stocks 1 AAPL @ $490
Assets:Checking $-490.00
P 2019/03/01 AAPL $525
-------------------
$ ledger -f temp.ldg bal -X '$' --unrealized
$1100.00 Assets
$50.00 Checking
$1050.00 Stocks
$-1060.00 Equity
$-1050.00 Opening Balances
$-10.00 Unrealized Gains
$-40.00 Income:Capital Gains
--------------------
0
Instead of "Unrealized Gains" being "$-70.00" and "Unrealized Losses" being
"$10.00", I get "Unrealized Gains" of "$-10.00", and "Opening Balances" of
"$-1050.00" (rather than "$-1000.00"). If I use the {} syntax instead of
the @ syntax, "Opening Balances" seems to use "AAPL" as its commodity,
rather than "$". Go figure.
Regards,
Pranesh
On Saturday, 9 March 2019 02:40:49 UTC+5:30, Richard Lawrence wrote:
>
> 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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ledger-cli/7e200b3f-a451-4e33-bd08-325c939da9f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.