Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Last active September 8, 2021 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonmichael/28f94ce2bd73318e4fccbfa7c9fc7e87 to your computer and use it in GitHub Desktop.
Save simonmichael/28f94ce2bd73318e4fccbfa7c9fc7e87 to your computer and use it in GitHub Desktop.
Working out a reporting challenge from HN, part 2. See also part 1: https://gist.github.com/simonmichael/3bb42cd82b4afd437305d10ad914b9ec
; avodonosov problem, HN 2021-09-05
; part of https://gist.github.com/simonmichael/3bb42cd82b4afd437305d10ad914b9ec
; "For example
; Jan 1
; Received 1000 EUR for a contract
; The rate 1 EUR = 2 BYN
; Jan 2
; Exchanged 300 EUR to BYN
; The rate 1 EUR = 2.1 BYN
; Jan 3
; Received 2000 EUR for a contract
; The rate 1 EUR = 2.2 BYN
; Jan 5
; Exchanged 600 EUR to BYN
; The rate 1 EUR = 2.3 BYN
; Jan 30:
; The rate 1 EUR = 2.5 BYN
; I need reports net worth, total income for the month, capital gains.
; First generate the report in BYN, then in EUR, then in USD.
; Let's assume the commissions are zero.
; Net worth is easy - current sum in original currencies and then convert to the currency of the report using the current rate
; But what ledger was failing to do, for me, is to compute capital gains.
; And, as I remember, it was unable to even balance the ledger
; How would one describe this use case in ledger format?"
P 1/1 EUR 2 BYN
1/1 acquire euros when price is 2 BYN
revenues -2000 BYN ; 1000 EUR @ 2 BYN ; recognise the revenue in local currency immediately, it's more useful for reporting
assets:eur:2021-01-01 1000 EUR @ 2 BYN = 1000 EUR @ 2 BYN ; the money is not converted immediately, so we track it as a lot with a cost basis
1/2 convert some euros at 2.1 BYN
assets:eur:2021-01-01 -300 EUR @ 2 BYN = 700 EUR @ 2 BYN ; we're disposing of euros acquired with this cost
assets:byn 630 BYN ; selling them for this much (300 EUR @ 2.1 BYN)
revenues:forex gain -30 BYN ; resulting in this gain
P 1/3 EUR 2.2 BYN
1/3 acquire euros when price is 2.2 BYN
revenues -4400 BYN ; 2000 EUR @ 2.2 BYN
assets:eur:2021-01-03 2000 EUR @ 2.2 BYN = 2000 EUR @ 2.2 BYN ; new date and cost, new lot
1/5 convert some euros at 2.3 BYN
assets:eur:2021-01-01 -600 EUR @ 2 BYN = 100 EUR @ 2 BYN ; we assume FIFO rule, disposing from the oldest lot first
assets:byn 1380 BYN ; 600 EUR @ 2.3 BYN
revenues:forex gain -180 BYN
P 1/30 EUR 1.21 USD
P 1/30 EUR 2.5 BYN
; Now to see a zero balance in bse (confirming "balanced books"), there are two steps:
; 1. convert all balances to the base currency, at the reporting date.
; This also involves recognising forex gain/loss as usual.
; 2. merge revenues/expenses to equity.
;comment
1/30 temporary for reporting: convert all to base currency, at current rate (2.5)
assets:eur:2021-01-01 -100 EUR @ 2 BYN = 0 EUR @ 2 BYN
assets:eur:2021-01-01:value on 1/30 250 BYN ; 100 EUR @ 2.5 BYN
revenues:forex gain:unrealised -50 BYN
assets:eur:2021-01-03 -2000 EUR @ 2.2 BYN = 0 EUR @ 2.2 BYN
assets:eur:2021-01-03:value on 1/30 5000 BYN ; 2000 EUR @ 2.5 BYN
revenues:forex gain:unrealised -600 BYN
;comment
1/30 temporary for reporting: retain earnings
revenues 6400 BYN == 0
revenues:forex gain 210 BYN == 0
revenues:forex gain:unrealised 650 BYN == 0
equity:retained earnings
comment
$ hledger -f avodonosov.journal bse
Balance Sheet With Equity 2021-01-30
|| 2021-01-30
=====================================++============
Assets ||
-------------------------------------++------------
assets:byn || 2010.0 BYN
assets:eur:2021-01-01:value on 1/30 || 250.0 BYN
assets:eur:2021-01-03:value on 1/30 || 5000.0 BYN
-------------------------------------++------------
|| 7260.0 BYN
=====================================++============
Liabilities ||
-------------------------------------++------------
-------------------------------------++------------
||
=====================================++============
Equity ||
-------------------------------------++------------
equity:retained earnings || 7260.0 BYN
-------------------------------------++------------
|| 7260.0 BYN
=====================================++============
Net: || 0
Questions:
1. What are the ways to report unrealised capital/forex gains/losses ?
1.1. Add a "convert all to base currency" txn working out the gain
As above.
1.2. Run cost and value reports and compare the difference
$ hledger bal assets:eur -B
200.0 BYN assets:eur:2021-01-01
4400.0 BYN assets:eur:2021-01-03
--------------------
4600.0 BYN
$ hledger bal assets:eur -V
250.0 BYN assets:eur:2021-01-01
5000.0 BYN assets:eur:2021-01-03
--------------------
5250.0 BYN
1.3. Use bal --gain in hledger master
$ hledger bal assets:eur --gain
50.0 BYN assets:eur:2021-01-01
600.0 BYN assets:eur:2021-01-03
--------------------
650.0 BYN
1.4. use roi ?
I don't know how; something like
$ hledger roi --inv assets:eur --pnl 'revenues:forex gain'
2. In the "convert some euros" txns, how to use trading accounts
(equity:conversion postings) instead of @ notation, while also
respecting cost basis ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment