balance of virtual transactions depends on amount syntax

64 views
Skip to first unread message

thierry

unread,
Mar 13, 2010, 1:10:31 PM3/13/10
to Ledger
Hello,

I've found that results of 'balance' command are different, depending
on the syntax of the amount.
Actually, I was expecting same result, whatever the way the amount is
spelled.

Thierry

$ ledger -f test8.ledger bal
$270 Assets:Broker
$30 Expense:Broker:Commissions
--------------------
$300


$ ledger -f test9.ledger bal
$270 Assets:Broker
$30 Expense:Broker:Commissions
$-300 Income:Capital Gains
--------------------
0


$ diff test8.ledger test9.ledger
10,11c10,11
< [Income:Capital Gains] $300 ; buy price 10*30
< [Income:Capital Gains] $-600 ; sell price 10*60
---
> [Income:Capital Gains] -10 AAPL @ $30 ; buy price 10*30
> [Income:Capital Gains] 10 AAPL @ $60 ; sell price 10*60


$ cat test8.ledger
1998/05/05 Buy AAPL
Assets:Broker 10 AAPL @ $30
Expense:Broker:Commissions $15
Assets:Broker

1999/08/16 Sell AAPL
Assets:Broker -10 AAPL @ $60
Expense:Broker:Commissions $15
Assets:Broker $585
[Income:Capital Gains] $300 ; buy price 10*30
[Income:Capital Gains] $-600 ; sell price 10*60
[Income:Capital Gains]


$ cat test9.ledger
1998/05/05 Buy AAPL
Assets:Broker 10 AAPL @ $30
Expense:Broker:Commissions $15
Assets:Broker

1999/08/16 Sell AAPL
Assets:Broker -10 AAPL @ $60
Expense:Broker:Commissions $15
Assets:Broker $585
[Income:Capital Gains] -10 AAPL @ $30 ; buy price 10*30
[Income:Capital Gains] 10 AAPL @ $60 ; sell price 10*60
[Income:Capital Gains]


$ ledger -v
Ledger master-0-g5471370, the command-line accounting tool

John Wiegley

unread,
Mar 13, 2010, 1:14:29 PM3/13/10
to ledge...@googlegroups.com
On Mar 13, 2010, at 1:10 PM, thierry wrote:

> I've found that results of 'balance' command are different, depending
> on the syntax of the amount.
> Actually, I was expecting same result, whatever the way the amount is
> spelled.

That's a great bug, Thierry. Thank you!

John

thierry

unread,
Mar 13, 2010, 3:03:12 PM3/13/10
to Ledger


Repost of test8.ledger, there is a typo about the signed amount.
This has no impact on the bug, but this has impact on realisticness of
the transaction.


$ cat test8.ledger
1998/05/05 Buy AAPL
Assets:Broker 10 AAPL @ $30
Expense:Broker:Commissions $15
Assets:Broker

1999/08/16 Sell AAPL
Assets:Broker -10 AAPL @ $60
Expense:Broker:Commissions $15
Assets:Broker $585

[Income:Capital Gains] $-300 ; buy price 10*30
[Income:Capital Gains] $600 ; sell price 10*60

John Wiegley

unread,
May 22, 2010, 8:52:01 PM5/22/10
to ledge...@googlegroups.com
On Mar 13, 2010, at 1:10 PM, thierry wrote:

> I've found that results of 'balance' command are different, depending
> on the syntax of the amount.
> Actually, I was expecting same result, whatever the way the amount is
> spelled.
>
> $ ledger -f test8.ledger bal
> $270 Assets:Broker
> $30 Expense:Broker:Commissions
> --------------------
> $300
>
> $ ledger -f test9.ledger bal
> $270 Assets:Broker
> $30 Expense:Broker:Commissions
> $-300 Income:Capital Gains
> --------------------
> 0

Actually, this behavior is correct. I think what you may be unaware of is that Ledger automatically adds the capital gains amount to the transaction. If we look at the register report, things become clearer:

~/src/ledger $ ledger -f test8.dat --columns=70 reg
98-May-05 Buy AAPL Assets:Broker 10 AAPL 10 AAPL
Ex:Br:Commissions $15 $15
10 AAPL
Assets:Broker $-315 $-300
10 AAPL
99-Aug-16 Sell AAPL Assets:Broker -10 AAPL $-300
Ex:Br:Commissions $15 $-285
Assets:Broker $585 $300
[I:Capital Gains] $-300 0
[I:Capital Gains] $600 $600
[I:Capital Gains] $-300 $300

Here, before your virtual postings, the amount to balance is $-300 (the capital gains income). This amount goes into catch-all posting that had no amount. Then you added -300 + 600, or $300, to the transaction, and this became the final balance.

~/src/ledger $ ledger -f test9.dat --columns=70 reg
98-May-05 Buy AAPL Assets:Broker 10 AAPL 10 AAPL
Ex:Br:Commissions $15 $15
10 AAPL
Assets:Broker $-315 $-300
10 AAPL
99-Aug-16 Sell AAPL Assets:Broker -10 AAPL $-300
Ex:Br:Commissions $15 $-285
Assets:Broker $585 $300
[I:Capital Gains] -10 AAPL $300
-10 AAPL
[I:Capital Gains] 10 AAPL $300
[I:Capital Gains] $-300 0

Here the situation is the same, there is $300 that must be balanced, which Ledger put into the catch-all posting for you. But then you added -10 AAPL + 10 AAPL to the transaction, or 0. So the result is zero.

There is a better way to handle these transactions, however, which looks like this:

1998/05/05 Buy AAPL
Assets:Broker 10 AAPL @ $30
Expense:Broker:Commissions $15
Assets:Broker

1999/08/16 Sell AAPL
Assets:Broker $585
Expense:Broker:Commissions $15
Assets:Broker -10 AAPL {$30} @ $60
Income:Capital Gains

Income:Capital Gains is automatically balanced into Equity:Capital Gains for you when this style of transaction is encountered.

John

Thierry Daucourt

unread,
May 23, 2010, 3:45:34 PM5/23/10
to ledge...@googlegroups.com
I understood the answer up to the last line. But not the last line.

Income:Capital Gains is automatically balanced into Equity:Capital Gains for you when this style of transaction is encountered.

At the moment, I am confused. I have not yet caught the point.

$ cat test91.ledger

1998/05/05 Buy AAPL
  Assets:Broker               10 AAPL @ $30
  Expense:Broker:Commissions      $15
  Assets:Broker

1999/08/16 Sell AAPL
  Assets:Broker                  $585
  Expense:Broker:Commissions      $15
  Assets:Broker              -10 AAPL {$30} @ $60
  Income:Capital Gains

$ ~/ledger/ledger -f test91.ledger bal
                $270  Assets:Broker
                $300  Equity:Capital Gains

                 $30  Expense:Broker:Commissions
               $-300  Income:Capital Gains
--------------------
                $300

$ diff test91.ledger test92.ledger
0a1,4
> 1998/01/01 Account opening
>   Assets:Broker                  $350
>   Equity:Opening Balances
>

$ cat test92.ledger
1998/01/01 Account opening
  Assets:Broker                  $350
  Equity:Opening Balances


1998/05/05 Buy AAPL
  Assets:Broker               10 AAPL @ $30
  Expense:Broker:Commissions      $15
  Assets:Broker

1999/08/16 Sell AAPL
  Assets:Broker                  $585
  Expense:Broker:Commissions      $15
  Assets:Broker              -10 AAPL {$30} @ $60
  Income:Capital Gains

$ ~/ledger/ledger -f test92.ledger bal
                $620  Assets:Broker
                $-50  Equity
                $300    Capital Gains
               $-350    Opening Balances

                 $30  Expense:Broker:Commissions
               $-300  Income:Capital Gains
--------------------
                $300


The balance does not balance to zero, and I do not understand why. I can not get the point of adding the "Equity:Capital Gains", on top of "Income:Capital Gains". Why both exists?
I have read again chapter "3.5 Understanding Equity" in ledger v2.6 documentation, but I can not figure out why in the example of test92.ledger my Equity should be $-50.

Another point about non english speaking users: I've translated the test91.ledger into French. Actually "Equity:Capital Gain" does not sound French and translation should be "Capitaux propres:Bénéfice". I would propose to add an option to .ledgerrc, something like "Equity:Capital gain=Capitaux propres:Bénéfice"

$ cat test93.ledger
1998/05/05 Achat AAPL
  Actif:Courtier               10 AAPL @ 30 €
  Dépense:Courtier:Commissions      15 €
  Actif:Courtier

1999/08/16 Vente AAPL
  Actif:Courtier                  585 €
  Dépense:Courtier:Commissions      15 €
  Actif:Courtier              -10 AAPL {30 €} @ 60 €
  Revenu:Gain en capital

$ ~/ledger/ledger -f test93.ledger bal
               270 €  Actif:Courtier
                30 €  Dépense:Courtier:Commissions
               300 €  Equity:Capital Gains
              -300 €  Revenu:Gain en capital
--------------------
               300 €

Martin Michlmayr

unread,
Jun 4, 2010, 8:24:44 AM6/4/10
to ledge...@googlegroups.com
* Thierry Daucourt <thierry....@free.fr> [2010-05-23 21:45]:

> The balance does not balance to zero, and I do not understand why. I can not
> get the point of adding the "Equity:Capital Gains", on top of
> "Income:Capital Gains". Why both exists?

I believe this is a bug in ledger. I independently ran into this the
other day: http://newartisans.com/bugzilla/show_bug.cgi?id=213
It has some explanation of what I believe is going on.

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

John Wiegley

unread,
Jun 4, 2010, 4:07:56 PM6/4/10
to ledge...@googlegroups.com
On May 23, 2010, at 3:45 PM, Thierry Daucourt wrote:

> The balance does not balance to zero, and I do not understand why. I can not get the point of adding the "Equity:Capital Gains", on top of "Income:Capital Gains". Why both exists?

It exists because according to the rules of double-entry accounting, money
cannot "come from nowhere". You just made $300 on a stock transaction; this
$300 has to be balanced with an identical amount affecting another account.

I use Equity for this purpose since it reflects the fact that the value came
from your equity: or assets which you already owned whose value had not been
realized as a gain.

> I have read again chapter "3.5 Understanding Equity" in ledger v2.6 documentation, but I can not figure out why in the example of test92.ledger my Equity should be $-50.

It is important to note that the only report which is guaranteed to balance
to zero in all cases is the -B report:

$ ledger -B bal
$320 Assets:Broker


$-50 Equity
$300 Capital Gains
$-350 Opening Balances
$30 Expense:Broker:Commissions
$-300 Income:Capital Gains
--------------------

0

It makes more sense to see what's going on in the register report:

$ ledger -B --columns=70 reg
98-Jan-01 Account opening Assets:Broker $350 $350
..pening Balances $-350 0
98-May-05 Buy AAPL Assets:Broker $300 $300
Ex:Br:Commissions $15 $315
Assets:Broker $-315 0
99-Aug-16 Sell AAPL Assets:Broker $585 $585
Ex:Br:Commissions $15 $600
Assets:Broker $-600 0
In:Capital Gains $-300 $-300
Eq:Capital Gains $300 0

If Income:Capital Gains were not balanced into Equity:Capital Gains, then
*this* report would not balance to zero, which is _always must_.

> Another point about non english speaking users: I've translated the test91.ledger into French. Actually "Equity:Capital Gain" does not sound French and translation should be "Capitaux propres:Bénéfice". I would propose to add an option to .ledgerrc, something like "Equity:Capital gain=Capitaux propres:Bénéfice"

Ledger uses gettext, which allows for all English text within it to be
translated for a specific locale. When I get closer to final release,
would you be willing to translate a PO file into French for me? It will
include strings such as this one.

John

John Wiegley

unread,
Jun 4, 2010, 4:11:17 PM6/4/10
to ledge...@googlegroups.com
On Jun 4, 2010, at 8:24 AM, Martin Michlmayr wrote:

> I believe this is a bug in ledger. I independently ran into this the
> other day: http://newartisans.com/bugzilla/show_bug.cgi?id=213
> It has some explanation of what I believe is going on.

In your case, too, Martin, you need to use the -B flag for things to
balance to zero:

$ ledger -B --columns=70 reg

10-Jan-01 Opening balance Assets:Cash 10.00 EUR 10.00 EUR
..Opening balance -10.00 EUR 0
10-Jan-20 Buy GBP Assets:Cash 7.00 EUR 7.00 EUR
Assets:Cash -7.00 EUR 0
10-Jan-20 Sell GBP Assets:Cash -7.50 EUR -7.50 EUR
Assets:Cash 7.50 EUR 0
Income:Gain -0.50 EUR -0.50 EUR
Eq:Capital Gains 0.50 EUR 0

Without -B the report reflects the commodities involved, and thus includes
any gains:

$ ledger --columns=70 reg
10-Jan-01 Opening balance Assets:Cash 10.00 EUR 10.00 EUR
..Opening balance -10.00 EUR 0
10-Jan-20 Buy GBP Assets:Cash 5.00 GBP 5.00 GBP
Assets:Cash -7.00 EUR -7.00 EUR
5.00 GBP
10-Jan-20 Sell GBP Assets:Cash -5.00 GBP -7.00 EUR
Assets:Cash 7.50 EUR 0.50 EUR
Income:Gain -0.50 EUR 0
Eq:Capital Gains 0.50 EUR 0.50 EUR

The part of this bug about {=} not auto-balancing is a separate issue.

John

Felipe Magno de Almeida

unread,
Jun 4, 2010, 7:16:53 PM6/4/10
to ledge...@googlegroups.com
On Fri, Jun 4, 2010 at 5:07 PM, John Wiegley <jwie...@gmail.com> wrote:
> On May 23, 2010, at 3:45 PM, Thierry Daucourt wrote:
>

[snip]

>> Another point about non english speaking users: I've translated the test91.ledger into French. Actually "Equity:Capital Gain" does not sound French and translation should be "Capitaux propres:Bénéfice". I would propose to add an option to .ledgerrc, something like "Equity:Capital gain=Capitaux propres:Bénéfice"
>
> Ledger uses gettext, which allows for all English text within it to be
> translated for a specific locale.  When I get closer to final release,
> would you be willing to translate a PO file into French for me?  It will
> include strings such as this one.

I would like to translate to Brazilian Portuguese as well.

> John

Regards,
--
Felipe Magno de Almeida

John Wiegley

unread,
Jun 5, 2010, 3:59:46 AM6/5/10
to ledge...@googlegroups.com
On Jun 4, 2010, at 7:16 PM, Felipe Magno de Almeida wrote:

> I would like to translate to Brazilian Portuguese as well.

Thank you so much, Felipe. I've added you to the list and will contact you
once things are finalized.

John

Martin Michlmayr

unread,
Jun 20, 2010, 8:54:03 AM6/20/10
to ledge...@googlegroups.com
* John Wiegley <jwie...@gmail.com> [2010-06-04 16:07]:

> > The balance does not balance to zero, and I do not understand why.
> > I can not get the point of adding the "Equity:Capital Gains", on
> > top of "Income:Capital Gains". Why both exists?

> It exists because according to the rules of double-entry accounting, money
> cannot "come from nowhere". You just made $300 on a stock transaction; this
> $300 has to be balanced with an identical amount affecting another account.
>
> I use Equity for this purpose since it reflects the fact that the value came

I think we all agree that it has to come from somewhere but what
Thierry and I don't understand why it has to be accounted for _twice_
(once in the manual "Income:Capital Gains" in Thierry's case and once
with the automatic "Equity:Capital Gains" posting).

Before I go any further, I should mention that I'm very new to
accounting so I'd like to get input from folks who are more
experienced. But I believe I've come up with a solution that I like
more than the current one.

Let's take my simple example: I own 10.00 EUR, I then buy 5.00 GBP for
7.00 EUR and then I sell those 5.00 GBP for 7.50 EUR, making a 0.50
EUR gain (ledger file below).

ledger -B will show:

10.00 EUR Assets:Cash
-9.50 EUR Equity
0.50 EUR Capital Gains
-10.00 EUR Opening balances
-0.50 EUR Income:Gain

The manual Income:Gain is to account for the gain and the
Equity:Capital Gains is an automatic posting from ledger.

But I wonder if this output makes sense. With -B, ledger says that I
have 10.00 EUR instead of 10.50 EUR. So imho if it shows this basis
cost, it should _not_ show the Income:Gain. After all, -B doesn't
consider the gain I made.

ledger without -B shows:

10.50 EUR Assets:Cash
-9.50 EUR Equity
0.50 EUR Capital Gains
-10.00 EUR Opening balances
-0.50 EUR Income:Gain
--------------------
0.50 EUR

So now I indeed I have 10.50 EUR and the 0.50 EUR gain is accounted
for by Income:Gain, but then we also have this automatic
Equity:Capital Gains posting and in the end it doesn't balance.

So imho there are three things wrong:
- in the first example (with -B), Equity:Capital Gains and Income:Gain
should not be there because we're talking about the basis cost.
- Also, the second example should imho balance because we account for
the gain.
- Finally, shouldn't Equity:Capital Gains be -0.50 EUR rather than
+0.50 EUR, leading to a total -10.50 equity (so that Assets == Equity)?

So how about this solution: at the moment, ledger generates an
automatic posting (in Equity) but you still need to manually account
for the gain/loss too. How about making these changes:
- don't require the manual account to account for the gain/loss since
ledger generates an automatic posting for this anyway.
- change the direction of the posting (i.e. a gain would be negative,
a loss positive)
- do not generate this automatic posting when -B is used.

This would mean that we end up with something that imho makes more
sense.

ledger -B would simply show:
10.00 EUR Assets:Cash
-10.00 EUR Equity:Opening balances
--------------------
0
i.e. we never made a gain/loss, we still have what we had.

whereas ledger without -B would show:

10.50 EUR Assets:Cash
-10.50 EUR Equity
-0.50 EUR Capital Gains
-10.00 EUR Opening balances
--------------------
0.00 EUR

i.e. we made a 0.50 EUR gain, now have 10.50 EUR in assets and
everything balances.

Finally, we could allow some way to change the name of the
automatic posting depending on the posting, e.g.:

Assets:Cash -5.00 GBP {=1.4 EUR} @ 1.5 EUR => Income:Gain

So I would end up with:

10.50 EUR Assets:Cash
-10.00 EUR Equity
-10.00 EUR Opening balances
-0.50 EUR Income:Gain

which I like more than the Equity:Capital Gains account.

Maybe this post shows my misunderstanding of ledger and accounting,
but it describes how I would expect ledger to work with this example.
Any comments?


Example file:

2007-01-01 Opening balances
Assets:Cash 10.00 EUR
Equity:Opening balances

2008-01-01 Buy 5.00 GBP for 7.00 EUR
Assets:Cash 5.00 GBP @ 1.4 EUR
Assets:Cash

2009-01-01 Sell 5.00 GBP for 7.50 EUR that I bought for 7.00 EUR
Assets:Cash -5.00 GBP {=1.4 EUR} @ 1.5 EUR
Assets:Cash 7.50 EUR
Income:Gain

Reply all
Reply to author
Forward
0 new messages