Current revision: 0.8

Last update: 2024-01-12 12:51:22 +0000 UTC

Thou hadst better eat salt with the Philosophers of Greece, than sugar with the Courtiers of Italy.

B. Franklin , Poor Richard's Almanac - 1758 A.D.

Fetching ledger exchange rates

Date: 01/11/2016, 16:41

Category: technology

Revision: 1

My tool of choice for personal and freelancing accounting is ledger-cli. Ledger is a simple, command line, double-entry accounting tool for geeks. All entries are written in a text file, in a clean, human readable way. Ledger, the binary, acts as a very powerful parser, consistent with the UNIX philosophy that every tool should do one job and do it well. I wouldn’t recommend ledger for accounting systems that have multiple users but as a personal accounting tool is extremely flexible.

I deal with three currencies on a monthly basis: USD, EUR and BGN. I like using signs instead of codes, so currencies in my ledger file appear as $, and лв. Ledger understands both notations and it doesn’t really care what kind of currency you are using as long as you let the system know the exchange rate.

Let’s assume we have the following entries:

; create sample assets accounts
account Equity:Opening Balances
account Assets:Business:Checking:BGN
account Assets:Business:Checking:EUR
account Assets:Business:Checking:USD

; create sample expenses accounts
account Expenses:Business:Services
account Expenses:Business:Hardware

2016/01/01 * Open accounts
  ; notes: First entry of personal assets
  Assets:Business:Checking:BGN  лв1500
  Assets:Business:Checking:EUR   €2750
  Assets:Business:Checking:USD  $10000
  Equity:Opening Balances

2016/01/13 * Officer HP Laser printer
  Expenses:Business:Hardware  лв480

2016/01/14 * X Monthly Subscription
  Expenses:Business:Services  $25

2016/01/15 * HP Proliant Server
  Expenses:Business:Hardware  €1080

Now a report, an assets balance will look like this:

$ ledger -f sample.ledger bal assets
               €1670  Assets:Business:Checking
              лв1020    BGN
               €1670    EUR
               $9975    USD

This is nice because I can see all the available assets, in multiple currencies in a clean, concise way.

Now, I want to know the value of my assets using today’s exchange rates. I can achieve this with ledger, using a specially formatted price database, which is again just a text file and looks like this:

P 2016/11/01 13:42:41 € $1.0946
P 2016/11/01 13:42:41 € лв1.9558

The format is pretty selfexplainatory, P stands for price followed by date, time assets, in my case the which I’m using a base currency and price of other assets, in my base currency.

Now ledger can deduce the exchange rate of your assets in BGN, USD and EUR. See the example bellow:

$ ledger -f sample.ledger --price-db ~/.prices.db -X € bal assets
              €11304  Assets:Business:Checking
                €522    BGN
               €1670    EUR
               €9113    USD

$ ledger -f sample.ledger --price-db ~/.prices.db -X $ bal assets
              $12374  Assets:Business:Checking
                $571    BGN
               $1828    EUR
               $9975    USD

$ ledger -f sample.ledger --price-db ~/.prices.db -X лв bal assets
             лв22109  Assets:Business:Checking
              лв1020    BGN
              лв3266    EUR
             лв17823    USD

I made a bash script to fetch exchange rates from fixer.io1:

#!/usr/bin/env bash

exchange_currencies=(USD BGN)

# Define exchange currency symbols
declare -A symbols
symbols[EUR]=# Fetch today's currency exchange rates from
fetch_data() {
  local -r base=$1 currency=$2
  curl -s\?base="$base" | jq .rates."$currency"

for i in "${exchange_currencies[@]}"; do
  date=$(date "+%Y/%m/%d %H:%M:%S")
  rate=$(fetch_data $base_currency "${i}")
  echo "P $date ${symbols[EUR]} ${symbols[$i]}${rate}" >> "$prices_db"

You can change the currencies and the location of prices.db file accordingly.

All my fleet consists of Macs, so I wrote a launchd script to run the script every day at 01:00 PM:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">

If you are a newcomer interested in ledger, check out the manual and the plaintextaccounting website.

  1. fetches the exchange rates from the ECB in XML and servers in nicely formatted JSON.