roi with transaction costs

22 views
Skip to first unread message

Joel

unread,
Sep 1, 2021, 11:03:29 AMSep 1
to hledger
Hi all,

I am a long-time user of hledger, and recently have started looking into using the roi command, which looks promising. In particular, I would like to use it to track my share portfolio.

It costs me $10.00 to buy shares each time, and I would like to factor this in to the return. My share purchases look something like this:

2021/06/01 * Share purchase
        assets:shares                        100 STOCK @ $1.00
        expenses:brokerage                              $10.00
        assets:checking

However when I run hledger roi, I am unable to make hledger understand an initial cost of $110 (share value + brokerage) for a final value of $100 (share value only). Is this possible?

Regards,

Joel

Dmitry Astapov

unread,
Sep 1, 2021, 7:06:05 PMSep 1
to hle...@googlegroups.com
Hi,

So after this transaction, what do you consider to be the value of your investment : $100 or 110?

I think it is arguably 100 (and I would expect both balance and roi command to report it as such), but it sounds like you are prepared to argue that it is 110. Its this correct? 


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/hledger/03c9de76-33a2-4282-8420-06615ca87964n%40googlegroups.com.

Joel

unread,
Sep 2, 2021, 2:30:27 AMSep 2
to hledger
Hi,

I would say that the initial outlay (cost) is $110 but the value immediately after purchase is $100. So the IRR column would ideally (to me) show a loss immediately after purchase. I'm not sure if it's just that I am not writing up the transaction correctly, or that I need to invoke hledger roi in another way.

Dmitry Astapov

unread,
Sep 2, 2021, 1:44:53 PMSep 2
to hle...@googlegroups.com
Hi,

I would personally say that I would never model it this way as the value of investment was never 110, and extra expenses you incur are just that - extra expenses.

However, if you want to do it this way, then I guess you can try to have one transaction that puts 110 into your investment, and then another one that moves 10 from the investment into pnl account. This way cost of investment will be 110, you will have pnl of -10, and current value of investment equal to 100

Andreas Pauley

unread,
Sep 3, 2021, 5:07:52 AMSep 3
to Hledger Group
Hi,

I think it is important to account for fees and other costs when calculating ROI, although I'm also not sure about the best way to do it in hledger.

From reading what you both said, it sounds as if no one thinks the value was ever $110, so a transaction that puts $110 into the investment account wouldn't model this accurately.

Another way of looking at this could be to consider what would happen if I immediately sold the assets.
The value of the assets was always $100, but I would end up with just $90 in my pocket after accounting for fees - a $10 loss.

According to this Investopedia article, the formula does bring in cost:
ROI = (Final Value - Initial Value) / Cost (times 100 to express it as a percentage)

Kind regards,
Andreas

Dmitry Astapov

unread,
Sep 3, 2021, 7:36:37 AMSep 3
to hle...@googlegroups.com
Hi,

Going along with your proposal, we need a place for the 10 in the withdrawal transaction so that it will balance. Since you're are saying that you want to treat the 10 as the investment loss, it should go into profit and loss account (that roi could work with). 

So to me it looks like we are both suggesting the exact same thing. 


Joel

unread,
Sep 3, 2021, 12:06:25 PMSep 3
to hledger
I have produced a minimum working example that I believe achieves what I want:

$ cat test.ledger
P 2021/08/01 STOCK $1.00

2021/08/01 * Opening Balances
equity:opening balances                     -$1,000.00
assets:checking                              $1,000.00


2021/08/01 * Stock purchase ; shares:
assets:shares                        100 STOCK @ $1.00
assets:shares                                   $10.00
assets:checking

2021/08/02 * Fees
equity:pnl                                      $10.00
assets:shares

I can then run the following command and get a sensible-looking output, though I have not verified the IRR/TWR figures:

$ hledger roi -Y -f test.ledger -V --investment "assets:shares" --pnl "equity:pnl" --cashflow

IRR cash flow for 2021-01-01 - 2021-12-31
+------------++----------+
|            ||   Amount |
+============++==========+
| 2021-08-01 || $-110.00 |
| 2022-01-01 ||  $100.00 |
+------------++----------+


TWR cash flow for 2021-01-01 - 2021-12-31
+------------++-----------------+--------------++---------+----------+------------+-------++------------------+
|            || Portfolio value | Unit balance ||     Pnl | Cashflow | Unit price | Units || New Unit Balance |
+============++=================+==============++=========+==========+============+=======++==================+
| 2021-08-01 ||               0 |            0 ||       0 |  $110.00 |        100 |   1.1 ||              1.1 |
| 2021-08-02 ||          110.00 |          1.1 || $-10.00 |        0 |      90.91 |     0 ||              1.1 |
+------------++-----------------+--------------++---------+----------+------------+-------++------------------+
Final unit price: $100.00/1.1 units = 90.91
Total TWR: -9.09%.
Period: 1.00 years.
Annualized TWR: -9.09%

+---++------------+------------++---------------+----------+-------------+---------++---------+--------+
|   ||      Begin |        End || Value (begin) | Cashflow | Value (end) |     PnL ||     IRR |    TWR |
+===++============+============++===============+==========+=============+=========++=========+========+
| 1 || 2021-01-01 | 2021-12-31 ||             0 |  $110.00 |     $100.00 | $-10.00 || -20.34% | -9.09% |
+---++------------+------------++---------------+----------+-------------+---------++---------+--------+

I noticed that I have to ensure that the "Fees" line is not the same day as the share purchase. If they are coincident then hledger exits with an error, stating "hledger: Ratio has zero denominator". This feels like a slight hack, so I am quite open to suggestions on how to improve this structure.

Dmitry Astapov

unread,
Sep 3, 2021, 4:54:13 PMSep 3
to hle...@googlegroups.com
Thank you!

This is exactly the model that I've been trying (badly) to describe :)

The bug with "zero denominator" that you stumbled upon will be fixed via https://github.com/simonmichael/hledger/pull/1682



--
D. Astapov
Reply all
Reply to author
Forward
0 new messages