Transaction balances as unbalanced virtual but doesn't when real

25 views
Skip to first unread message

Muke Tever

unread,
Jan 3, 2022, 1:06:19 PMJan 3
to Ledger
Howdy!

I'm trying to automate a transaction.  The below gives the expected outcome, and it appears to balance as expected:

= %/inventory/
    (Equity:$account)                         (-amount)
    (Equity:USD)                              (market(amount))

2022/01/02 Interest
    Assets:OBJ                                 1 OBJ @ $5.00 ; :inventory:
    Income:OBJs                              $-5.00


               1 OBJ  Assets:OBJ
              -1 OBJ  Equity:Assets:OBJ
               $5.00  Equity:USD
              $-5.00  Income:OBJs
--------------------
                   0


But I need this to be a real transaction.  However if I make it real, or even if I use [balanced virtual accounts], I get an error that it doesn't balance anymore:

= %/inventory/
    Equity:$account                         (-amount)
    Equity:USD                              (market(amount))

2022/01/02 Interest
    Assets:OBJ                                 1 OBJ @ $5.00 ; :inventory:
    Income:OBJs                              $-5.00

While parsing file "/Users/[...]/Documents/2022/Ledger/testfile.ledger", line 7:
While balancing transaction from "/Users/[...]/Documents/2022/Ledger/testfile.ledger", lines 5-7:
> 2022/01/02 Interest
>     Assets:OBJ                                 1 OBJ @ $5.00 ; :inventory:
>     Income:OBJs                              $-5.00
Unbalanced remainder is:
               $5.00
-1 OBJ {$5.00} [2022/01/02]
Amount to balance against:
              $10.00
While applying automated transaction from "/Users/[...]/Documents/2022/Ledger/testfile.ledger", lines 1-3:
> = %/inventory/
>     Equity:$account                         (-amount)
>     Equity:USD                              (market(amount))
While extending transaction from "/Users/[...]/Documents/2022/Ledger/testfile.ledger", lines 5-7:
> 2022/01/02 Interest
>     Assets:OBJ                                 1 OBJ @ $5.00 ; :inventory:
>     Income:OBJs                              $-5.00
Error: Transaction does not balance


This looks like a bug, but it's also possible I'm doing something broken that shouldn't work.

Any suggestions on how this should be done? 


Martin Michlmayr

unread,
Jan 3, 2022, 6:12:39 PMJan 3
to ledge...@googlegroups.com
* Muke Tever <koh...@gmail.com> [2022-01-02 21:45]:
> = %/inventory/
> Equity:$account (-amount)
> Equity:USD (market(amount))

I initially thought it's a bug but after looking at it some more I
think it's not a bug.

amount and market(amount) are not the same. The amount is $5.00.
market(amount) is 1 OBJ {$5.00} [2022-01-02].

So I think you need to use either:

= %/inventory/
Equity:$account (-amount)
Equity:USD (amount)

or:

= %/inventory/
Equity:$account (market(-amount))
Equity:USD (market(amount))

You can run this command to see the difference:
ledger -f d bal --lots equity

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

Muke Tever

unread,
Jan 3, 2022, 9:54:05 PMJan 3
to Ledger
On Monday, January 3, 2022 at 3:12:39 PM UTC-8 Martin Michlmayr wrote:
* Muke Tever  [2022-01-02 21:45]:
> = %/inventory/
> Equity:$account (-amount)
> Equity:USD (market(amount))

I initially thought it's a bug but after looking at it some more I
think it's not a bug.

amount and market(amount) are not the same. The amount is $5.00.
market(amount) is 1 OBJ {$5.00} [2022-01-02].

Oh, I see the problem, and I guess it's a little different than I thought.  So amount and market(amount) doesn't 'balance' to zero, but they do 'balance' in the sense that ledger doesn't complain about it being unbalanced, if they exist as a regular transaction:

2022/01/02 Interest
    Assets:OBJ                                 1 OBJ {$5.00} [2022/01/02]
    Income:OBJs                               $-5.00
   
1 OBJ {$5.00} [2022/01/02]  Assets:OBJ
              $-5.00  Income:OBJs
--------------------
              $-5.00
1 OBJ {$5.00} [2022/01/02]

So the interesting thing is that it complains when it's an automated transaction, even when it both would balance as a normal transaction and would balance the original transaction to zero.  

= %/inventory/
    Equity:$account                         -1 OBJ {$5.00} [2022/01/02]
    Equity:USD                              $5.00

2022/01/02 Interest
    Assets:OBJ                                 1 OBJ {$5.00} [2022/01/02] ; :inventory:
    Income:OBJs                               $-5.00
 
While parsing file "/Users/[...]/Documents/2022/Ledger/testfile.ledger", line 7:
While balancing transaction from "/Users/[...]/Documents/2022/Ledger/testfile.ledger", lines 5-7:
> 2022/01/02 Interest
>     Assets:OBJ                                 1 OBJ {$5.00} [2022/01/02] ; :inventory:

>     Income:OBJs                               $-5.00
Unbalanced remainder is:
               $5.00
-1 OBJ {$5.00} [2022/01/02]
Amount to balance against:
              $10.00
While applying automated transaction from "/Users/[...]/Documents/2022/Ledger/testfile.ledger", lines 1-3:
> = %/inventory/
>     Equity:$account                         -1 OBJ {$5.00} [2022/01/02]
>     Equity:USD                              $5.00

While extending transaction from "/Users/[...]/Documents/2022/Ledger/testfile.ledger", lines 5-7:
> 2022/01/02 Interest
>     Assets:OBJ                                 1 OBJ {$5.00} [2022/01/02] ; :inventory:

>     Income:OBJs                               $-5.00
Error: Transaction does not balance

So I think you need to use either:

= %/inventory/
Equity:$account (-amount)
Equity:USD (amount)

or:

= %/inventory/
Equity:$account (market(-amount))
Equity:USD (market(amount))

You can run this command to see the difference:
ledger -f d bal --lots equity

Indeed these do not throw errors, but they don't get me balanced to zero as I'm intending.
Thank you for your time and response.

Martin Michlmayr

unread,
Jan 4, 2022, 5:32:20 AMJan 4
to ledge...@googlegroups.com
* Muke Tever <koh...@gmail.com> [2022-01-03 18:54]:
> Oh, I see the problem, and I guess it's a little different than I
> thought. So amount and market(amount) doesn't 'balance' to zero,
> but they do 'balance' in the sense that ledger doesn't complain
> about it being unbalanced, if they exist as a regular transaction:

> 2022/01/02 Interest
> Assets:OBJ 1 OBJ {$5.00} [2022/01/02]
> Income:OBJs $-5.00

Yes and no.

The example you give only works because ledger assumes that a
transaction with two commodities balances. (It does an implicit
conversion. There was some discussion about this recently, in which I
argued that ledger should not do this.)

If you introduce a third commodity in your example it will not
balance:

2022/01/02 Interest
Assets:OBJ 1 OBJ {$5.00} [2022/01/02]
Income:OBJs $-5.00
A -10 EUR
B 10 EUR

You need the @ on the first posting.

BTW, given 1 OBJ {$5.00} I once argued that the cost should set the
price (i.e. @ $5.00 should be implicit if no other @ is given).
I still think that makes sense but it's not how ledger works.
I think it's this one: https://github.com/ledger/ledger/issues/630

Muke Tever

unread,
Jan 4, 2022, 6:26:49 AMJan 4
to Ledger
On Tuesday, January 4, 2022 at 2:32:20 AM UTC-8 Martin Michlmayr wrote:
* Muke Tever  [2022-01-03 18:54]:
> Oh, I see the problem, and I guess it's a little different than I
> thought. So amount and market(amount) doesn't 'balance' to zero,
> but they do 'balance' in the sense that ledger doesn't complain
> about it being unbalanced, if they exist as a regular transaction:

> 2022/01/02 Interest
> Assets:OBJ 1 OBJ {$5.00} [2022/01/02]
> Income:OBJs $-5.00

Yes and no.

The example you give only works because ledger assumes that a
transaction with two commodities balances. (It does an implicit
conversion. There was some discussion about this recently, in which I
argued that ledger should not do this.)

If you introduce a third commodity in your example it will not
balance:

2022/01/02 Interest
Assets:OBJ 1 OBJ {$5.00} [2022/01/02]
Income:OBJs $-5.00
A -10 EUR
B 10 EUR

You need the @ on the first posting.

Indeed— I constructed it with @ in my initial failed example. 

But this helps me out.  My goal was to fix the redundancy of having four lines in the transaction when two were predictable.  If it's a matter of commodities, I can just put a different line in the automated transaction so it's only working with the one commodity.  

The below builds on your first suggestion to me and produces the intended result, balancing without errors (though I'll certainly rename the tag to something more applicable):

= %/inventory/
    Income:OBJs                  (market(-amount))
    Equity:USD                    (market(amount))

2022/01/02 Interest
    Assets:OBJ                       1 OBJ [2022/01/02] @ $5.00 ; :inventory:
    Equity:OBJ                      -1 OBJ [2022/01/02] @ $5.00


1 OBJ {$5} [2022/01/02]  Assets:OBJ
-1 OBJ {$5} [2022/01/02]  Equity:OBJ
                  $5  Equity:USD
                 $-5  Income:OBJs
--------------------
                   0


 Thank you!
 
Reply all
Reply to author
Forward
0 new messages