A User's view of Hledger

Learning about hledger

Emacs Lisp code for mutual fund transaction

Last week I posted a simple way to speed entry of transactions for mutual fund dividends. The basic idea was to paste a skeleton of the transaction, by which I mean all the information that stays the same, such as the name of the mutual fund and the categories. Once you have copied that into your file, all you need to do is to fill in the missing information, such as date, dollar amount, and number of shares.

Below is another option, written in Emacs Lisp. This differs from the above in that it prompts you to enter the key information: date, dollar amount, and number of shares. Once you have provided that information, the transactions are inserted into your text at the point of the cursor.

This second approach has two advantages. One is that it avoids repetitive typing. For example, rather than typing in the amount in dollars in three places in your transactions, you type it once, and the amount appears in the three places courtesy of Emacs Lisp. The second advantage is that you are prompted for each necessary part of the transaction. This way you don’t forget to supply a necessary part of the transaction.

To give credit where credit is due, the idea for this and some of the code came from my Emacs mentor Protesilaos Stavrou. Any mistakes in the implementation are entirely mine.

Here is the Emacs Lisp code:

(defun insert-Acme-dividend ()
  "Insert text for Acme Bond dividend."
  (interactive)
  (let ((transaction-date (org-read-date))
        (amount-string (read-string "Enter dollar amount, no dollar sign: "))
        (shares (read-string "Enter number of shares: ")))
    (insert
     (concat
      "\n"
      (format "%s Dividend Received | Acme Bonds" transaction-date)
      "\n"
      (format "     income:Acme:ACMET                  $-%s" amount-string)
      "\n"
      (format "     assets:Acme:cash                    $%s" amount-string)
      "\n"
      "\n"
      (format "%s Dividend Reinvestment | Acme Bonds" transaction-date)
      "\n"
      (format "     assets:Acme:cash                   $-%s" amount-string)
      "\n"
      (format "     assets:Acme:ACMET                    %s ACMET"  shares)
      "\n"))))