Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Last active September 8, 2021 22:13
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/3bb42cd82b4afd437305d10ad914b9ec to your computer and use it in GitHub Desktop.
Save simonmichael/3bb42cd82b4afd437305d10ad914b9ec to your computer and use it in GitHub Desktop.
Working out a reporting challenge from HN, part 1. See also part 2 (foreign exchange gains): https://gist.github.com/simonmichael/28f94ce2bd73318e4fccbfa7c9fc7e87
; avodonosov problem, from https://news.ycombinator.com/item?id=28424432
; 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?
1/1
revenues -1000 EUR
assets:eur 1000 EUR
P 1/1 EUR 2 BYN
1/2
assets:eur -300 EUR @ 2.1 BYN
assets:byn 630 BYN
1/3
revenues -2000 EUR
assets:eur 2000 EUR
P 1/3 EUR 2.2 BYN
1/5
assets:eur -600 EUR @ 2.3 BYN
assets:byn 1380 BYN
P 1/30 EUR 2.5 BYN
P 1/30 EUR 1.21 USD
#!/usr/bin/env bash
export LEDGER_FILE=avodonosov-example.journal
echo "First generate the report in BYN, then in EUR, then in USD."
echo "Net worth, valued at month end"
echo
hledger bs; echo
hledger bs -X BYN; echo
hledger bs -X EUR; echo
hledger bs -X USD; echo
echo
echo
echo "Total income for the month, valued when received"
echo
hledger is -M; echo
hledger is -M --value=then,BYN; echo
hledger is -M --value=then,EUR; echo
hledger is -M --value=then,USD; echo
First generate the report in BYN, then in EUR, then in USD.
Net worth, valued at month end
Balance Sheet 2021-01-05
|| 2021-01-05
=============++======================
Assets ||
-------------++----------------------
assets:byn || 2010.0 BYN
assets:eur || 2100 EUR
-------------++----------------------
|| 2010.0 BYN, 2100 EUR
=============++======================
Liabilities ||
-------------++----------------------
-------------++----------------------
||
=============++======================
Net: || 2010.0 BYN, 2100 EUR
Balance Sheet 2021-01-30, valued at period ends
|| 2021-01-30
=============++============
Assets ||
-------------++------------
assets:byn || 2010.0 BYN
assets:eur || 5250.0 BYN
-------------++------------
|| 7260.0 BYN
=============++============
Liabilities ||
-------------++------------
-------------++------------
||
=============++============
Net: || 7260.0 BYN
Balance Sheet 2021-01-30, valued at period ends
|| 2021-01-30
=============++============
Assets ||
-------------++------------
assets:byn || 804 EUR
assets:eur || 2100 EUR
-------------++------------
|| 2904 EUR
=============++============
Liabilities ||
-------------++------------
-------------++------------
||
=============++============
Net: || 2904 EUR
Balance Sheet 2021-01-30, valued at period ends
|| 2021-01-30
=============++=============
Assets ||
-------------++-------------
assets:byn || 972.84 USD
assets:eur || 2541.00 USD
-------------++-------------
|| 3513.84 USD
=============++=============
Liabilities ||
-------------++-------------
-------------++-------------
||
=============++=============
Net: || 3513.84 USD
Total income for the month, valued when received
Income Statement 2021-01
|| Jan
==========++==========
Revenues ||
----------++----------
revenues || 3000 EUR
----------++----------
|| 3000 EUR
==========++==========
Expenses ||
----------++----------
----------++----------
||
==========++==========
Net: || 3000 EUR
Income Statement 2021-01, valued at posting date
|| Jan
==========++============
Revenues ||
----------++------------
revenues || 6400.0 BYN
----------++------------
|| 6400.0 BYN
==========++============
Expenses ||
----------++------------
----------++------------
||
==========++============
Net: || 6400.0 BYN
Income Statement 2021-01, valued at posting date
|| Jan
==========++==========
Revenues ||
----------++----------
revenues || 3000 EUR
----------++----------
|| 3000 EUR
==========++==========
Expenses ||
----------++----------
----------++----------
||
==========++==========
Net: || 3000 EUR
Income Statement 2021-01, valued at posting date
|| Jan
==========++==========
Revenues ||
----------++----------
revenues || 3000 EUR
----------++----------
|| 3000 EUR
==========++==========
Expenses ||
----------++----------
----------++----------
||
==========++==========
Net: || 3000 EUR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment