State of capital gains tracking in hledger

557 views
Skip to first unread message

ma.k...@gmail.com

unread,
Jun 6, 2015, 9:03:53 AM6/6/15
to hle...@googlegroups.com
Hi,
I'm trying to figure out whether I should use ledger, hledger or beancount. Hledger has a big head start for me because the binaries you provide make it so much easier to get started, especially on Windows systems.

Now I'm wondering what the state of capital gains tracking is in hledger.
Is it completely impossible with the lack of fluctuating prices?
Or can I make it work by only tracking differences in sales prices?

I'm looking at the following example from the ledger manual (see below)
Running hledger -f test.dat balance, I receive the error "unexpected {" referring to the { in the sale.
is there any way to make something like this work at the moment? What are the plans for this?

Best regards,
Matthias

2004/05/01 Stock purchase
    Assets:Broker                     50 AAPL @ $30.00
    Expenses:Broker:Commissions        $19.95
    Assets:Broker                  $-1,519.95

2005/08/01 Stock sale
    Assets:Broker                    -50 AAPL {$30.00} @ $50.00
    Expenses:Broker:Commissions        $19.95
    Income:Capital Gains           $-1,000.00
    Assets:Broker                   $2,480.05


Simon Michael

unread,
Jun 6, 2015, 9:25:48 AM6/6/15
to hle...@googlegroups.com
Welcome Matthias,

hledger doesn't currently parse Ledger's {...} prices (with no equals sign). I don't fully understand what those do. The Ledger manual implies they are equivalent to @ (a fluctuating unit price), but what does

 -50 AAPL {$30.00} @ $50.00 

mean ?

Currently in hledger all prices are fixed, like Ledger's {=...} prices (which we do parse). So I think there's no practical way to track fluctuating prices in hledger without implementing https://github.com/simonmichael/hledger/issues/131 (which should be not too hard).

Martin Blais

unread,
Jun 6, 2015, 1:07:30 PM6/6/15
to hle...@googlegroups.com
Ma,
I wrote a detailed comparison of these systems here:
http://furius.ca/beancount/doc/comparison
There first two sections of the document explicitly deals with the treatment of cost basis.


--
You received this message because you are subscribed to the Google Groups "hledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hledger+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin Blais

unread,
Jun 6, 2015, 1:17:08 PM6/6/15
to hle...@googlegroups.com
On Sat, Jun 6, 2015 at 9:25 AM, Simon Michael <si...@joyful.com> wrote:
Welcome Matthias,

hledger doesn't currently parse Ledger's {...} prices (with no equals sign). I don't fully understand what those do. The Ledger manual implies they are equivalent to @ (a fluctuating unit price), but what does

 -50 AAPL {$30.00} @ $50.00 

mean ?

Indeed.

In Beancount the semantics of this is that 50 units of "AAPL at $30 cost basis" are added to the inventory, and a price annotation of $50 is present on the posting. The cost basis is the value used in balancing the transaction. The price annotation is not entirely ignored however, it is used by plugins:

beancount.ops.implicit_prices: Automatically creates price directives corresponding to these annotations. This fills in values for the in-memory database of prices which is then used in reporting.

beancount.plugins.sellgains: Applies a secondary balance check that uses the price instead of the cost basis, and that also ignores any Income postings in the transaction. See the comment at the top of this file for a rationale and examples: https://bitbucket.org/blais/beancount/src/c8cedf46c100d0cb80553b091aff481243ade44d/src/python/beancount/plugins/sellgains.py?at=default
This additional check finds many errors in data entry.

Finally, when you write custom scripts against your data you can find the price annotations on the Python API and use them in creative ways if you want.



Currently in hledger all prices are fixed, like Ledger's {=...} prices (which we do parse). So I think there's no practical way to track fluctuating prices in hledger without implementing https://github.com/simonmichael/hledger/issues/131 (which should be not too hard).



On Jun 6, 2015, at 3:36 AM, ma.k...@gmail.com wrote:

Hi,
I'm trying to figure out whether I should use ledger, hledger or beancount. Hledger has a big head start for me because the binaries you provide make it so much easier to get started, especially on Windows systems.

Now I'm wondering what the state of capital gains tracking is in hledger.
Is it completely impossible with the lack of fluctuating prices?
Or can I make it work by only tracking differences in sales prices?

I'm looking at the following example from the ledger manual (see below)
Running hledger -f test.dat balance, I receive the error "unexpected {" referring to the { in the sale.
is there any way to make something like this work at the moment? What are the plans for this?

Best regards,
Matthias

2004/05/01 Stock purchase
    Assets:Broker                     50 AAPL @ $30.00
    Expenses:Broker:Commissions        $19.95
    Assets:Broker                  $-1,519.95

2005/08/01 Stock sale
    Assets:Broker                    -50 AAPL {$30.00} @ $50.00
    Expenses:Broker:Commissions        $19.95
    Income:Capital Gains           $-1,000.00
    Assets:Broker                   $2,480.05


Martin Blais

unread,
Jun 6, 2015, 1:29:09 PM6/6/15
to hle...@googlegroups.com
On Sat, Jun 6, 2015 at 1:17 PM, Martin Blais <bl...@furius.ca> wrote:
On Sat, Jun 6, 2015 at 9:25 AM, Simon Michael <si...@joyful.com> wrote:
Welcome Matthias,

hledger doesn't currently parse Ledger's {...} prices (with no equals sign). I don't fully understand what those do. The Ledger manual implies they are equivalent to @ (a fluctuating unit price), but what does

 -50 AAPL {$30.00} @ $50.00 

mean ?

Indeed.

In Beancount the semantics of this is that 50 units of "AAPL at $30 cost basis" are added to the inventory, and a price annotation of $50 is present on the posting. The cost basis is the value used in balancing the transaction. The price annotation is not entirely ignored however, it is used by plugins:

beancount.ops.implicit_prices: Automatically creates price directives corresponding to these annotations. This fills in values for the in-memory database of prices which is then used in reporting.

beancount.plugins.sellgains: Applies a secondary balance check that uses the price instead of the cost basis, and that also ignores any Income postings in the transaction. See the comment at the top of this file for a rationale and examples: https://bitbucket.org/blais/beancount/src/c8cedf46c100d0cb80553b091aff481243ade44d/src/python/beancount/plugins/sellgains.py?at=default
This additional check finds many errors in data entry.

I clarified the example in the sellgains plugin that explains how this works; here's an improved version:

ma.k...@gmail.com

unread,
Jun 7, 2015, 11:05:28 PM6/7/15
to hle...@googlegroups.com
Hi Simon, Hi Martin,
thank you very much for the explanations that you added!

Regarding beancount: I have read the comparison document you (Martin) had written. I like it very much and I agree with many of the steps you have taken there. At the same time, I am somewhat put off by the lengthy installation process that your documents lay out. In fact, I'm actually worried that it may not run on Windows at all. You mention wget and ncurses if I remember correctly. How necessary are those? You are developing on Linux or Mac, I suppose?

Regarding hledger:  
"-50 AAPL {$30.00} @ $50.00" should mean that I sell those AAPL that I bought at 30 for 50. 
I know that hledger doesn't have fluctuating prices and I am not asking for functionality where I can ask for a balance and have AAPL recorded at some intermediate price (e.g. 40, halfway). I would be satisfied to record a gain on the sale at same point. Something like the following may work initially as well.

2004/05/01 Stock purchase
    Assets:Broker                     50 AAPL
    Assets:Broker                  $-1500

2005/08/01 Stock sale
    Assets:Broker                    -50 AAPL
    Income:Capital Gains           $-1,000.00
    Assets:Broker                   $2500

Nobody here who tracks stock trades with hledger, is there? I'm open to other ways of entering this as well.

Regarding Issue 131: I am not sure I can fully understand the pains the folks are having with their abroad consumption. Creating additional account just so I can balance out the AAPL that I buy seems unnatural and cumbersome. In particular, I'd also expect the balance summary to show that I am in fact holding 50 AAPL instead of the zeroing out everything. As far as I am concerned, my example balances to zero. You consider AAPL at the purchase price and it zeros out. When you sell it, you add income to a capital gains account and it balances again. If you track prices along the way, you balance via an unrealized gains account.
I'm not sure -X would help with this. I don't want all my accounts to be summarized in AAPL currency. Would it?
What about the simpler trading scenario that I describe? Is is feasible? 

Best regards,
Matthias

Simon Michael

unread,
Jun 8, 2015, 12:32:42 PM6/8/15
to hle...@googlegroups.com, ledger-cli
Hi Matthias,

> On Jun 7, 2015, at 12:39 PM, ma.k...@gmail.com wrote:
> I know that hledger doesn't have fluctuating prices and I am not asking for functionality where I can ask for a balance and have AAPL recorded at some intermediate price (e.g. 40, halfway). I would be satisfied to record a gain on the sale at same point. Something like the following may work initially as well.
>
> 2004/05/01 Stock purchase
> Assets:Broker 50 AAPL
> Assets:Broker $-1500
>
> 2005/08/01 Stock sale
> Assets:Broker -50 AAPL
> Income:Capital Gains $-1,000.00
> Assets:Broker $2500
>
> Nobody here who tracks stock trades with hledger, is there? I'm open to other ways of entering this as well.

That seems quite reasonable, I don't know any reason why it couldn't be handled something like that, if you don't need to calculate values at other points in time.

> Regarding Issue 131: I am not sure I can fully understand the pains the folks are having with their abroad consumption. Creating additional account just so I can balance out the AAPL that I buy seems unnatural and cumbersome. In particular, I'd also expect the balance summary to show that I am in fact holding 50 AAPL instead of the zeroing out everything. As far as I am concerned, my example balances to zero. You consider AAPL at the purchase price and it zeros out. When you sell it, you add income to a capital gains account and it balances again. If you track prices along the way, you balance via an unrealized gains account.
> I'm not sure -X would help with this. I don't want all my accounts to be summarized in AAPL currency. Would it?
> What about the simpler trading scenario that I describe? Is is feasible?

In all honesty I'm not clear on this myself. I tend to get clear only when I try to model some real-world case in my own finances, or someone writes up a description of their situation that's really concrete and minimal. So discussions like this are quite helpful.

I think Ledger has these features interacting:

1. The -X flag converts all amounts to a chosen commodity (for which conversion prices are known as of the report date).

2. The -V flag is like -X but automatically selects the target commodity, in some magic hard-to-explain way according to the Ledger manual. I ignore it for now.

3. The P directive allows conversion prices to be recorded on additional dates, without needing transactions. This makes -X more useful when prices are fluctuating, because you can calculate commodity conversions more accurately on arbitrary dates.

4. If -X (or -V) is in effect, then whenever a price changes (due to a P directive, or being specified within a transaction), a "Commodities revalued" transaction is automatically generated on that date. These are displayed by the Ledger's register command, and also by print (but without the postings, which seems wrong).

5. The -B flag converts posting amounts which have a price attached, to that price's commodity. It isn't affected by P directives.

Did I get it right ?

Matthias Kauer

unread,
Jun 9, 2015, 3:36:44 AM6/9/15
to hle...@googlegroups.com, ledge...@googlegroups.com
Hi Michael,
you are correct. That example does indeed work and should be sufficient for tax-related tracking of stock trading. I'll only really know in practice, I suppose. Beancount obviously goes further with the specific matching of different lots you purchased, but I may not need that given my rather light trading.

I'd be more interested in getting a time evolution of my account now. I suppose I can call hledger balance and filter for a specific date. But doing that over and over for a plot would be tedious. 
Anyhow, without unrealized gains and thus fluctuating prices it may not make sense.

Regarding the flags you mention: 
-X and -P sound interesting. -B already does kind of the same thing, no? Is it like -X, but only for some main or base currency?
How difficult is the implementation of -P and some sort of time-based plotting, print or csv export? Where would you start?

Simon Michael

unread,
Jun 10, 2015, 6:55:18 PM6/10/15
to hle...@googlegroups.com
On Jun 9, 2015, at 12:36 AM, Matthias Kauer <ma.k...@gmail.com> wrote:
I'll only really know in practice, I suppose.

Yes that's always the real test. Let us know how it goes.

I'd be more interested in getting a time evolution of my account now. I suppose I can call hledger balance and filter for a specific date. But doing that over and over for a plot would be tedious. 

I'm not sure what you mean here. The register command (and hledger-web's balance chart) can show you a single account's balance over time, and http://hledger.org/manual.html#multicolumn-balance-reports can show multiple accounts' balances over time.

Regarding the flags you mention: 
-X and -P sound interesting. -B already does kind of the same thing, no? Is it like -X, but only for some main or base currency?

My description in this thread was terse. Check the hledger or Ledger manual, hopefully they make it clear.

How difficult is the implementation of -P and some sort of time-based plotting, print or csv export? Where would you start?

I think adding those things I mentioned to hledger is not difficult, just a matter of developer time and prioritising. I'd start by looking at how to implement the -X feature, using the current fixed prices.

The hledger manual talks about CSV output for the print, register and balance commands. For making plots, other than the one in hledger-web, I most recently used a multi-column balance report, saving the CSV output  and importing that into a spreadsheet for plotting. Eg something like hledger bal -M ^income ^expenses -o incexp.csv
Reply all
Reply to author
Forward
0 new messages