Tracking a Portfolio

99 views
Skip to first unread message

Brandon Olivares

unread,
Mar 10, 2020, 10:20:57 PM3/10/20
to hle...@googlegroups.com
I want to use hledger to track the performance of an investment portfolio. Would this be possible?

My main question is, how would I track trades that result in a net credit? For example, when I short a stock, I technically sell it first, receiving the current price for it, then hopefully buy it back at a lower price, keeping the difference. How would I mark something like this in hledger?

Thanks,
Brandon

Brandon Olivares

unread,
Mar 11, 2020, 12:36:28 AM3/11/20
to hledger
Sorry but a bit of a followup to this.

I put something quick together just to see how it might work. But how would I handle commodity names that are complex with numbers in them? It seems to trip up the parser, unless I surround it with quotes. Is this appropriate and expected?

Anything wrong with this format below? Here I'm demonstrating an option put credit spread with buying a put at $261 and selling a put at $264.

; journal created 2020-03-11 by hledger

P 2020/03/10 "SPY200311P00261000" $10.00
P 2020/03/10 "SPY200311P00264000" $18.50

2020/03/08 Deposit
    assets:portfolio:cash          $500
    assets:client

2020/03/09 Deposit
    assets:portfolio:cash          $500
    assets:client

2020/03/10
    assets:portfolio:SPY:200311P00261000  4 "SPY200311P00261000" @ $113.00
    assets:portfolio:cash                                         $-452.00

2020/03/10
    assets:portfolio:SPY:200311P00264000  -4 "SPY200311P00264000" @ $193.00
    assets:portfolio:cash                                           $772.00

Simon Michael

unread,
Mar 11, 2020, 3:04:35 PM3/11/20
to hledger
Hi Brandon,

yes it must be possible to some extent, but we are short of realistic examples and how-tos. Yours is a great start and I'll be studying it, unfortunately I don't have much more to add yet. Hopefully you'll get more input, or try also #plaintextaccounting chat and https://plaintextaccounting.org .

Yes, it's normal to need quotes around commodity symbols containing numbers (it helps separate them from the quantity).


-- 
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/387a6f89-4754-4ff2-94e6-b2a8eace452e%40googlegroups.com.

Simon Michael

unread,
Mar 11, 2020, 3:11:47 PM3/11/20
to hledger
PS: this isn't something we've used much, but it wouldn't hurt to also ask on https://money.stackexchange.com . 

There aren't many PTA users there (past questions: https://plaintextaccounting.org/#stack-exchange), but there are a lot of financial experts. I think this is a bookkeeping question, and therefore sufficiently on-topic.

(I'm hoping to get enough activity and points to create a [hledger] topic there.)



Brandon Olivares

unread,
Mar 11, 2020, 3:13:59 PM3/11/20
to hle...@googlegroups.com
Thanks for your response. I’m figuring a lot of it out. Just one thing I could really use your help with if possible:

In options contracts it’s common for a contract to become worthless. Basically say a contract is worth $200 one day, the next day it could be worth nothing and should simply be discarded.

How could I signify this in Hledger?

So in simpler terms, I sell contract A for $200. Ideally I want it to become worthless so I don’t have to buy it back. I can do the transaction just fine where I sell the transaction and receive $200. But then the asset account lists 1 contract worth $200. How can I then say that this is now worth nothing?

Simon Michael

unread,
Mar 11, 2020, 3:16:23 PM3/11/20
to hledger
Would it be considered a loss ? That would make it an expense I think. But I'm not experienced with options or their bookkeeping entries, so take with a grain of salt.


Simon Michael

unread,
Mar 11, 2020, 3:21:23 PM3/11/20
to hledger
PS I find pages like this one really hard to follow, but - any use ?



Klauss Hass

unread,
Mar 11, 2020, 3:26:15 PM3/11/20
to hle...@googlegroups.com
I use ledger but hledger probably works the same.

If your put is worthless by 2020-03-01 you’d have to mark it down on the expiration date. Here is how I’d do it

2020/03/10
    assets:portfolio:SPY:200311P00261000  4 "SPY200311P00261000" @ $113.00
    assets:portfolio:cash                                         $-452.00

2020-03-11 * Total Loss
assets:portfolio:SPY:200311P00261000  -4 "SPY200311P00261000" @ $113.00
Expenses:Market:Options

This way the position would leave your portfolio and the loss of $452 will appear in a P&L report

The other way around would be:

2020-03-11 * Total Profit
assets:portfolio:SPY:200311P00261000  -4 "SPY200311P00261000" @ $115.00
Assets:Cash $ 452
Income:Market:Options

On the profit side, hledger would balance the income at $8


Brandon Olivares

unread,
Mar 11, 2020, 3:29:13 PM3/11/20
to hle...@googlegroups.com
This is very helpful, thanks! And the article looks nice too.

kla...@hass.net.br

unread,
Mar 11, 2020, 4:55:55 PM3/11/20
to hle...@googlegroups.com

My profit example is wrong

Assets:cash should be $ 460 instead of $452. This way profit correctly balances to $8.

Brandon Olivares

unread,
Mar 11, 2020, 5:16:14 PM3/11/20
to hle...@googlegroups.com
OK what would you think of this? Let me know if you can follow it. It’s a real-world example and this is how I closed it out. Would this be correct?

; Buy 4 contracts for $113 each
2020/03/10
    Assets:Portfolio:SPY:200311P00261000  4 "SPY200311P00261000" @ $113.00
    Expenses:Fees:Brokerage                                        $2.60
    Expenses:Fees:Regulatory                                       $0.06
    Assets:Portfolio:Cash                                         $-454.66

; Sell 4 contracts for $193 each (not the same contracts)
2020-03-10
    Assets:Portfolio:SPY:200311P00264000    -4 "SPY200311P00264000" @ $193.00
    Expenses:Fees:Brokerage                                             $2.60
    Expenses:Fees:Regulatory                                            $0.09
    Assets:Portfolio:Cash                                             $769.31

; $261 contracts expire worthless
; Technically this would be a loss?
; But it’s really the same transaction as the one below so it’s a net profit.
2020/03/11 SPY Expire
    Assets:Portfolio:SPY:200311P00261000  -4 "SPY200311P00261000" @ $113.00
    Income:Capital Gains                                         $452.00

; $264 contracts expire worthless,
; resulting in net profit
2020/03/11 SPY Expire
    Assets:Portfolio:SPY:200311P00264000  4 "SPY200311P00264000" @ $193.00
    Income:Capital Gains  $-772.00



kla...@hass.net.br

unread,
Mar 11, 2020, 5:55:18 PM3/11/20
to hle...@googlegroups.com

These transactions seem to be off.

 

You paid $ 454.66 for the 4 contracts on the first leg, but you’re not adjusting you loss for the brokerage fees your total loss will be $ 454.66 instead of $452 on the third leg.

What I recommend you to do is let hledger calculate the amount paid for each contract ($454.66/4 = $ 113,665) and don’t hard set the asset price on the first transaction.

You can do that by removing the price paid – everything after @ - and use the -V flag  when running the reports and you get the total amount paid on the transaction, which is what matters in the end. Again I used to use ledger but I’m sure hledger works the same.

 

On the other leg brokerage fees weren’t calculated either, your profit before taxes should be $769.31 instead of 772

 

When you balance the transactions you should get a total profit of $314.65 if I’m not mistaken

Brandon Olivares

unread,
Mar 11, 2020, 6:12:07 PM3/11/20
to hle...@googlegroups.com
There are no brokerage fees on the final legs because they aren’t transactions, the options just expire worthless.

Also the $113 is the actual price of each contract so shouldn’t be anything to calculate there.

So to simplify this let’s say this happens:

1. I sell 4 contracts for $193 each (going to ignore the other leg for now). This is the real per-contract price my brokerage lists.
2. I receive $772 in cash
3. I pay fees of $2.60 to the broker and $0.09 regulatory fee
4. I’m left with $769.31
5. In one day those 4 contracts become worthless and I still keep all the cash I already had. There are no further transactions because those contracts no longer exist.
6. However I need an offsetting transaction to remove the contracts from the portfolio account, so I offset it to Income:Capital Gains
7. Income has to be $772.00 because that’s what I received for the contracts in total.
8. Income:Capital Gains will be $-772.00, and Expenses will be $2.69; I think this should offset one another right?
9. If I run hleddger incomestatement it seems to give me the correct net value of $769.31.

Am I incorrect somewhere?

Brandon Olivares

unread,
Mar 12, 2020, 9:24:52 AM3/12/20
to hle...@googlegroups.com
Sorry just one last question.

What’s the proper way to deal with unrealized gains? ledger actually has an —unrealized flag, but I don’t see that option on hledger.

Simon Michael

unread,
Mar 12, 2020, 10:52:40 AM3/12/20
to hledger
Manually, for now. We don't have any special support for tracking lots, so I would give each one its own subaccount. And then compare cost (-B) and value (-V) reports.


the.so...@gmail.com

unread,
Jan 24, 2021, 10:54:18 AM1/24/21
to hledger
Dear Brandon,
Another way to track unrealized gains is to record transactions using currency/commodity trading accounts:
https://github.com/ledger/ledger/wiki/Multiple-currencies-with-currency-trading-accounts

For instance, if invest in shares, I would use trading account pairs with names such as `Equity:Trading:Shares:INR:AAPL` and `Equity:Trading:Shares:AAPL:INR` (where AAPL is the ticker symbol for the share).

When I wish to find out the unrealized gain/loss, I would simply use the command:

`hledger bal equity:trading:shares -V`

That will tell you  the gain / loss.  (The gain will be indicated by a negative number since it is unrealized income, and a loss by a positive number since it is unrealized expense.)

Or you could instead use:

`hledger bal equity:trading:shares -V --invert`

That will show the gain with a positive sign and a loss with a negative sign.

You can simulate lots using sub-accounts with dates (and a transaction number within that date, if you make multiple purchases and sales within a single day):

`Assets:Shares:AAPL:20210124_1`
`Equity:Trading:Shares:INR:AAPL:20210124_1`
`Equity:Trading:Shares:AAPL:INR:20210124_1`

... and so on.

Regards,
Pranesh

Simon Michael

unread,
Jan 24, 2021, 11:04:14 AM1/24/21
to hle...@googlegroups.com
That's a great how-to, and an elegant method! Could we add it to the cookbook, possibly with a complete mini example added ?

On Jan 24, 2021, at 07:54, the.so...@gmail.com <the.so...@gmail.com> wrote:

Dear Brandon,
Reply all
Reply to author
Forward
0 new messages