Compute unrealized PnL day-by-day

75 views
Skip to first unread message

Zhuoyun Wei

unread,
Dec 5, 2017, 11:15:39 AM12/5/17
to bean...@googlegroups.com
Hi,

I've been using Beancount to track trading activities. With
"beancount.plugins.unrealized" plugin enabled, I can see a few special
transactions inserted for unrealized PnL. The special transactions are
always inserted as the last transactions in the journal.

I am wondering if there is a BQL or something to compute the unrealized
PnL day-by-day. Id est, if I close the position on Nov 25, the PnL is X
USD; if I close the position on Nov 26, the PnL is Y USD.

I have tried:

select date, account, position, balance from close on 2017-XX-XX clear where account ~ 'Unrealized';

but it does not work as expected. Is that because the plugin runs before
the "CLOSE" syntax takes effect?

Currently the only way I can think of, is to comment out / remove latest
"price" entries from price database to force Beancount to compute
unrealized PnL for a different date.

--
Zhuoyun Wei
signature.asc

Martin Blais

unread,
Dec 9, 2017, 10:40:20 PM12/9/17
to Beancount
There's no way to do this right now, unless you resort to some Python (but it's doable with Python for sure, as long as you have frequent enough price directives, you have all the required data already). See the net-worth-over-time.py script, for example.

I've written about this previously on the mailing-list (see archive), but I see two ways to report unrealized profit / market value:
(1) Implement the feature in all reporting routines, recognizing its first-class need and accept the extra complexity, or
(2) Automatically insert the unrealized gains as transactions and avoid the need to talk about it in reporting.
What you have in this plugin is (2). 
The elegance is that reports don't know about unrealized profits.
It turns out it's not that great (it was a bit of an exploration anyhow).

I think the better way is for all the reporting routines to support valuation at market value and if needed, cost basis reporting as well. I don't think creating fake realization entries like this is viable long-term. Creating more frequent realizing entries also causes other problems.




Zhuoyun Wei

unread,
Dec 10, 2017, 4:14:18 AM12/10/17
to bean...@googlegroups.com
2017-12-09 22:39:56 Martin Blais <bl...@furius.ca>:
> There's no way to do this right now, unless you resort to some Python (but it's doable with Python for sure, as long as
> you have frequent enough price directives, you have all the required data already).

Ah I see. I have enough price directives.

> See the net-worth-over-time.py script, for example.
>

Thanks for pointing out a way. I looked into the script, along with the
beancount.plugins.unrealized pluin, and wrote a quick script to compute
the unrealized PnL day by day. Here is the script for anyone with similar
needs:

https://gist.github.com/wzyboy/427808e3fab4e75eb4b007c530884132

The script is very crude: it does not consider your actual holdings on a
certain date (because I do not know how to get the balance of a commodity on a
certain date, efficiently). Instead, it just compares your current holdings
with the market value of your holdings on a certain date.


--
Zhuoyun Wei
signature.asc
Reply all
Reply to author
Forward
0 new messages