I believe ledger doesn't handle capital gains and losses correctly.
Thierry and I independently ran into this problem in 2010 and tried to
explain why ledger's behaviour is wrong.  John disagreed with us back
then but since I still believe that ledger's behaviour is wrong, I'd
like to open up this debate again (see [1] and [2] for the original
discussion).

Let's start with an example.  We start with 10.00 GBP in cash.
Naturally, since this is our only asset and there are no
liabilities, our equity is -10.00 GBP.

2011-01-01 * Opening balance
    Assets:Cash                               10.00 GBP
    Equity:Opening balance                   -10.00 GBP

This balances just fine since assets equals equity:

$ ledger -f test.ledger bal
           10.00 GBP  Assets:Cash
          -10.00 GBP  Equity:Opening balance
--------------------
                   0

Now we'll buy 1 AAA share for 10.00 GBP:

2011-02-01 * Buy 1 AAA for 10.00 GBP
    Assets:Investments                 1 AAA {10.00 GBP}
    Assets:Cash                              -10.00 GBP

We now have 1 AAA share worth 10.00 GBP and no liabilities,
so our equity is still -10.00 GBP.

Now let's sell our AAA share for 12.00 GBP, making a profit of
2.00 GBP.  Let's start with this transaction:

2011-12-07 * Sell AAA with a gain
    Assets:Cash                               12.00 GBP
    Assets:Investments                -1 AAA {10.00 GBP} @@ 12.00 GBP

ledger will now produce the following error:

While balancing transaction from "test.ledger", lines 5-7:
> 2011-12-07 * Sell AAA with a gain
>     Assets:Cash                               12.00 GBP
>     Assets:Investments  -1 AAA {10.00 GBP} @@ 12.00 GBP
Unbalanced remainder is:
            2.00 GBP
Amount to balance against:
           14.00 GBP

The unbalanced remainder is 2.00 GBP.  Why?  Because we're selling
10.00 GBP worth of AAA but get 12.00 GBP for it... so we have to
account for the gain of 2.00 GBP somehow.  So let's fix up the
transaction by making the gain clear:

2011-12-07 * Sell AAA with a gain
    Assets:Cash                               12.00 GBP
    Assets:Investments  -1 AAA {10.00 GBP} @@ 12.00 GBP
    Income:Capital gains                      -2.00 GBP

So now we're selling 1 AAA, originally worth 10.00 GBP, for 12.00
GBP.  We get 12.00 GBP in cash and make a capital gain of 2.00 GBP.

Now the transaction is accepted by ledger and according to my own
calculation it should balance.  But if you look at ledger bal you'll
see that it does not balance:

$ ledger -f test.ledger bal
           12.00 GBP  Assets:Cash
           -8.00 GBP  Equity
            2.00 GBP    Capital Gains
          -10.00 GBP    Opening balance
           -2.00 GBP  Income:Capital gains
--------------------
            2.00 GBP

So what do we have here?  We have 12.00 GBP in Assets:Cash, which
is correct (we originally had 10.00 GBP in cash, bought 1 AAA for
that money, and then sold 1 AAA for 12.00 GBP in cash).  We also
have an Equity:Opening balance of -10.00 GBP, which is correct
since our opening equity only changes at the end of the year.
And we have -2.00 GBP if Income:Capital gains to account for our
gain (i.e. how we ended up going from 10.00 GBP at the beginning
of the year to 12.00 GBP now).

But where does 2.00 GBP in Equity:Capital Gains come from and why
does it no longer balance?

This is automatically generated by ledger and I think that's wrong.
John explained in [1] that this exists "because according to the
rules of double-entry accounting, money cannot "come from nowhere"."

Of course it has to come from somewhere.  But that's why we added
the Income:Capital gains  -2.00 GBP entry.  This is where our gain
comes from.  We've accounted for it already, and now ledger tries
to account for it again by adding something to Equity:Capital Gains.
I see two problems with this:

1) We've already accounted for the gain by adding
     Income:Capital gains    -2.00 GBP
   Without this entry, ledger complained about a balancing
   problem.  So when working out what the balance is, ledger
   obviously takes that gain/loss into account already.  ledger
   should either not add the gain/loss to the balance and
   generate the automatic posting, or should add the gain/loss to
   the balance (i.e. requiring the user to account for the gain/loss
   in the transaction) and not generate something automatically.
   Both would lead to something which balances.  But currently
   ledger asks the user to account for the gain/loss in the
   transaction and _also_ adds an automatic posting.
2) Why is Equity:Capital Gains  +2.00 GBP and not -2.00 GBP
   -12.00 GBP in Equity is more than -10.00 GBP in Equity, so
   if we make a gain -2.00 GBP should be added to equity, and
   +2.00 GBP should be added if we make a loss.  ledger does
   the reverse.

To sum up the problem:

 - ledger generates an entry to Equity that is reversed.
 - ledger shouldn't generate this entry in the first place since
   the user accounted for the gain/loss already.

I hope this explanation is clearer than the one I offered in 2010.

By the way, I think we also need a discussion at some point
about how these capital gains/losses should be treated with -B
or -V.  But my example didn't use these flags at all, and we
should discuss the simple case without these flags first.

Martin

[1] http://www.mail-archive.com/[email protected]/msg01158.html
[2] http://www.mail-archive.com/[email protected]/msg01272.html

P.S. I've included a longer explanation below using the accounting
equation, but hopefully my example above is clear enough.  The text
below is only optional reading.

Let's take a look at the accounting equation.  The simply form
is:

  Assets = Liabilities + Equity

We can extend this to add Income and Expenses:

  Assets = Liabilities + Equity + Income - Expenses

Income and Expense accounts are used during the year.  At the
end of the year, when closing the books, all Income and Expense
accounts are added up and the result (either a profit or a loss)
is added to Equity.  Equity is not touched during the year - only
when closing the book at the end of the year.

We can reorganize the accounting equation as follows:

  Assets - Liabilities - Equity = 0
  Assets - Liabilities - Equity - Income + Expenses = 0

In ledger, a negative equity (-10.00 GBP) is more than a positive
equity (10.00 GBP).  I think the best way to explain this
is to reorganize the accounting equation as follows:

  Assets + (-Liabilities) + (-Equity) = 0
  Assets + (-Liabilities) + (-Equity) + (-Income) + Expenses = 0

and to observe that the minus symbol is part of the values
we enter into ledger (i.e. this is the reason we're writing
-10.00 GBP for an equitiy which is actually positive).

So let's get back to our example.  We had 10.00 GBP in cash
and an equity of -10.00 GBP.  If we fill in the equation, we
get the following:

  10.00 + -0 + -10.00 + 0 + 0 = 0

When we buy 1 AAA, our accounting equation doesn't change
because we still have 10.00 in assets.  Then we sell 1 AAA
for 12.00 GBP:

  12.00 + -0 -10.00 + -2.00 + 0 = 0

The accounting equation is still valid: we now have 12.00 in
assets, we have an opening balance of -10.00 and we made a gain
of -2.00.  At the end of the year, when closing our books,
we'd add up income and expenses and see that we made a profit
of -2.00.  (Obviously a profit of plus 2.00 in normal language,
but -2.00 in ledger-speak).  So now equity would change to -12.00
and we'd have:

  12.00 + -0 -12.00 = 0

This is what should happen.  What happens in reality is that
we account for the gain in Income but ledger also add something
to Equity, so we end up with:

 12.00 + -0 + (-10.00 + 2.00) + -2.00 + 0 = 2.0

and this is not valid because it should equal 0.

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

Reply via email to