Hi all,
In case there are ledger-mode users here who might find this useful, I
wanted to share a snippet of code I wrote as I was doing my end-of-year
journal file tidying. It scrapes the USD-EUR exchange rate from
exchange-rates.org for the date of the transaction at point, then enters
it as a price declaration for the same date on the line before the
transaction.
Thus it transforms something like
2020/12/29 * Salary
assets:Checking €10
revenues:Income
into
P 2020/12/29 € $1.2255
2020/12/29 * Salary
assets:Checking €10
revenues:Income
Useful if, like me, you have to report overseas earnings on your US
taxes!
I have ledger-insert-exchange-rate bound to C-c X in ledger-mode.
The currency names and symbols are hardcoded but they'd be easy to adapt
to your own needs. Improvements welcome. Happy New Year!
Best,
Richard
(defun parse-EUR-USD-exchange-rate ()
"Find and parse the exchange rate out of a buffer containing the HTML
from exchange-rates.org"
(goto-char (point-min))
(if (re-search-forward
"1 Euro in US Dollars is \\([[:digit:]]+\\.[[:digit:]]+\\)"
(point-max) t)
(match-string 1)
(error "Exchange rate not found in HTML")))
(defun fetch-exchange-rate-on (month day year)
"Return the exchange rate from exchange-rates.org on a given date"
(let* ((url (format "https://www.exchange-rates.org/Rate/EUR/USD/%s-%s-%s"
month day year)))
(with-current-buffer (url-retrieve-synchronously url t t)
(parse-EUR-USD-exchange-rate))))
(defun ledger-insert-exchange-rate ()
"Add the exchange rate on a certain date as a price declaration to the line
before point"
(interactive)
(save-excursion
(goto-char (car (ledger-navigate-find-xact-extents (point))))
(when (looking-at ledger-iso-date-regexp)
(let* ((year (match-string 2))
(month (match-string 3))
(day (match-string 4))
(date-str (match-string 0))
(ex-rate (fetch-exchange-rate-on month day year))
(price-str (format "P %s € $%s\n" date-str ex-rate)))
(insert price-str)))))
--
---
You received this message because you are subscribed to the Google Groups
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ledger-cli/87wnwycapa.fsf%40aquinas.