Finance Articles

Program your Finances: Command-line Accounting

Note: you can find much more information about ledger on ledger-cli.org, including links to official documentation and other implementations

About three years ago I was in some serious financial straits. I had just started my first job out of college that I had moved across the country for and had to bootstrap almost my whole life. This meant buying furniture, buying a car, outfitting a kitchen, etc. Every two weeks I would get a salary deposit, and within two weeks it would be almost completely gone from my checking account. I actually bounced a rent check or two in there. After the second time that happened I vowed it wouldn't happen again and started keeping track of every penny that I spent using a program called ledger. This was, in hindsight, exactly what I needed to get myself back on track. Actually seeing money moving in and out of my accounts forced me to modify my behavior. At the time, Mint wasn't around, but I don't think it would have helped nearly as much. Forcing myself to actually type out the transactions was the key to changing behavior.

Ledger is almost the most boring, austere accounting program you could think of. There's no pretty graphs, no online interaction, no GUI of any sort. It's basically a command-line driven calculator with a lot of specializations that make it ideal for tracking finances, which is what makes it so ideal for someone who spends a lot of time inside a text editor. It's very easy to script around and it has a very rich query language that lets you get at the data that you want with a minimum of fuss. It's very much the inspiration for Calorific.


Program your Finances: Reporting for Fun and Profit

Note: you can find much more information about ledger on ledger-cli.org, including links to official documentation and other implementations

Another note: I've written a new version of this that is much more dynamic and flexible named Ledger Web.

Last year I wrote what ended up being the most popular article on this blog ever, Program Your Finances: Command-line Accounting. That post went over how I track and report on my finances using a program called Ledger along with a few helper scripts. Recently I expanded that toolset quite a bit and wanted to show how keeping meticulous track of your finances can give you superpowers. Read on for the gory details.


Program Your Finances: Vacation Tracking

Note: you can find much more information about ledger on ledger-cli.org, including links to official documentation and other implementations

Recently my girlfriend and I visited the wonderful city of Vancouver, Canada. While out of country we tend to use my Schwab Investor Checking account because it carries no fees whatsoever, including currency conversions, and it refunds all ATM fees. Last year when we went to Ireland we just kept all of the receipts and figured it out when we got back, which was excrutiatingly painful. Lost receipts, invisible cash transactions, ugh. It hurts to even think about it. This year, I decided to cobble together a simple system so we could track on the fly. Read on to see how it came together.


Program Your Finances: Automated Transactions

Note: you can find much more information about ledger on ledger-cli.org, including links to official documentation and other implementations

I've been using Ledger for almost five years now to keep track of my personal finances. For three of those years I've lived with a roommate of one form or another. Part of living with a roommate is splitting up bills. Some people decide to do this by dividing the bills up between roommates. For example, Pete pays the electric and gas bills and Andrew pays the water and the cable. Other roommates decide to nominate one person to have all of the bills in their name and post the amounts due every month for everyone to see. This is what my girlfriend and have been doing and it's been working great. All of the bills are in my name and I give her a summary every month and she hands me a check. Easy peasy.

Of course, being a complete and utter nerd means that I have to make this more complicated than it needs to be in the name of reducing the amount of work I have to do.


A Robust Reporting System for Ledger

Note: you can find much more information about ledger on ledger-cli.org, including links to official documentation and other implementations

For the last five years I've kept my personal finances in order using the ledger system, a sophisticated command line program that consumes a lightly formatted text file. It's helped me repay debts and get everything in order, helping me financially absorb an injury last month that would have been extremely detrimental just a few years prior.

The stock ledger program is exclusively command-line oriented. For quick checks and greping over output, this is fine. For some time, though, I've wanted a more graphical, more robust way of looking at my finances. I've also wanted a more familiar query language, since version 2.0's queries were someone limited and version 3.0's query syntax is not very well documented yet. Last year I wrote a simple system that pushed monthly reports out to static HTML files, which got me part of the way there but I really wanted something more flexible. Something where I can just write an arbitrary query and have the output dumped to HTML.

Thus, I present Ledger Web. In Ledger Web, your ledger is kept in a text file, just the same as always, reports are ERB files, and queries are SQL. Ledger Web watches your ledger file and whenever it changes dumps it into a PostgreSQL database table. It's also quite customizable, letting you set hooks both before and after row insertion and before and after ledger file load.


Program Your Finances: Envelope Budgeting

Note: you can find much more information about ledger on ledger-cli.org, including links to official documentation and other implementations. Also, check out my intro to accounting with Ledger.

A few years ago I heard about YNAB, or You Need A Budget. YNAB is a set of rules and associated software that help people to dig themselves out of financial holes and prosper with a budget. The rules are:

  1. Give Every Dollar a Job
  2. Save for a Rainy Day
  3. Roll With the Punches
  4. Live on Last Month's Income

YNAB embraces both traditional budgeting, where you have a fixed amount of money every month for a category, as well as "envelope budgeting", where you put a fixed amount every month into a category, but if you don't spend all of that it rolls to the next month.

In this blog post I'm going to talk about how to smoothly implement envelope budgeting in Ledger land.

Envelope Budgeting: A Primer

Envelope budgeting is a pretty simple concept. When you receive a paycheck, you separate out a certain amount of money for each category and put it in an envelope. When the money in the envelope is gone, you can't spend any more for that category. Some financial systems actually have you draw out your entire paycheck in cash and put it into physical envelopes, but we're not going to go that far.

Chart of Accounts

If you've ever taken an accounting class you're probably familiar with the concept of a "chart of accounts". In an accounting system, your accounts make a tree, starting from five root accounts:

  • Assets
  • Liabilities
  • Income
  • Expenses
  • Equity

For example, if you have a checking account, that's an asset, like this: Assets:Checking. A credit card would be a liability: Liabilities:Credit Card. Your paycheck would be income: Income:Salary, and getting groceries would be an expense: Expenses:Food:Groceries. Equity is out of scope for this discussion, but in a personal finance system it's typically used when you're declaring opening balances in accounts.

Parallel Accounts

The best way to implement envelope budgeting in Ledger is using a parallel chart of accounts. That is to say, a set of accounts that's outside of your normal real-money assets, income, expenses, or liabilities. I've chosen to use Assets:Funds and Liabilities:Funds ("fund" as in "slush fund") in the examples that follow, but you can use whatever you want as long as it doesn't mix with your real money accounts.

Let's say our water bill comes every other month and averages $100. In a traditional monthly budgeting system this would be hard to account for, since some months will be zero and some will have a charge. With our parallel accounts, though, this is easy:

2015/04/02 * Salary
    Assets:Checking              $1,000.00
    Income:Salary

2015/04/02 * Water Bill Accrual
    Assets:Funds:Water              $50.00
    Liabilities:Funds:Water

2015/05/02 * Salary
    Assets:Checking              $1,000.00
    Income:Salary

2015/05/02 * Water Bill Accrual
    Assets:Funds:Water              $50.00
    Liabilities:Funds:Water

At the beginning of April and May, we receive our salary deposit and set aside $50 each time for your water bill. Notice how, in the accrual account, we're depositing into our Assets:Funds:Water account and balancing it out from a companion liability. This reflects the fact that in double entry accounting every transaction has to balance, and dedicated balancing liabilities make things easier later on. Here are our balances:

           $2,100.00  Assets
           $2,000.00    Checking
             $100.00    Funds:Water
          $-2,000.00  Income:Salary
            $-100.00  Liabilities:Funds:Water
--------------------
                   0

Now let's look at what happens when our water bill comes due:

2015/05/03 * Water Bill
    Expenses:Water                  $95.00
    Assets:Checking                $-95.00
    Liabilities:Funds:Water         $95.00
    Assets:Funds:Water             $-95.00

Notice how we pull $95 out of our checking account and also pull $95 out of our Liabilities:Funds:Water account.

Here's what the balances look like now:

           $1,910.00  Assets
           $1,905.00    Checking
               $5.00    Funds:Water
              $95.00  Expenses:Water
          $-2,000.00  Income:Salary
              $-5.00  Liabilities:Funds:Water
--------------------
                   0

$95 went from the checking account into the water expense and the water fund still has $5 in it.

Automated Envelopes

This system would be a pain in the butt if we had to manually track it for every transaction. Thankfully, Ledger has us covered with automated transactions.

An automated transaction looks a lot like a normal transaction, except it starts with an = and has an expression instead of a payee and date. Let's see what our water accrual rule looks like:

= /Income:Salary/
    * Assets:Funds:Water         $50.00
    * Liabilities:Funds:Water   $-50.00

In this example the expression is a regular expression surrounded by /s. /Income:Salary/ will match any posting with that as the account name.

After the expression we have two lines. They start with a * to indicate that they're cleared transactions. Next is the account name and an amount, just like in a normal ledger transaction.

Now, let's set up a matching rule for spending out of the envelope:

= /Expenses:Water/
    * Liabilities:Funds:Water      1.0
    * Assets:Funds:Water          -1.0

This one is very similar to the first, except for those amounts. Notice how they don't have a commodity attached to them? In automated transactions, ledger will treat an amount without a commodity as a percentage, where 1.0 = 100%. This rule means that we want to match every water expense and pull 100% of it out of our water envelope.

Putting it all together, here's what the automatic version looks like:

= /Income:Salary/
    * Assets:Funds:Water            $50.00
    * Liabilities:Funds:Water      $-50.00

= /Expenses:Water/
    * Liabilities:Funds:Water          1.0
    * Assets:Funds:Water              -1.0

2015/04/02 * Salary
    Assets:Checking              $1,000.00
    Income:Salary

2015/05/02 * Salary
    Assets:Checking              $1,000.00
    Income:Salary

2015/05/03 * Water Bill
    Expenses:Water                  $95.00
    Assets:Checking                $-95.00

Here's the resulting register report:

15-Apr-02 Salary     Assets:Checking          $1,000.00 $1,000.00
                     Income:Salary           $-1,000.00         0
                     Assets:Funds:Water          $50.00    $50.00
                     Liabilities:Funds:Water    $-50.00         0
15-May-02 Salary     Assets:Checking          $1,000.00 $1,000.00
                     Income:Salary           $-1,000.00         0
                     Assets:Funds:Water          $50.00    $50.00
                     Liabilities:Funds:Water    $-50.00         0
15-May-03 Water Bill Expenses:Water              $95.00    $95.00
                     Assets:Checking            $-95.00         0
                     Liabilities:Funds:Water     $95.00    $95.00
                     Assets:Funds:Water         $-95.00         0

For every paycheck, $50 went into our fund. When we paid the water bill, $95 came out of the fund. To set this up for more envelopes, just create a corresponding pair of rules for each one.

One last thing. What if we want to change how much we're setting aside in the water envelope? Let's say our rates go up and we now need to save $55 from each paycheck instead of $50. Here's how we do that:

= /Income:Salary/ and expr date >= [2015/04/01] && date < [2015/06/01]
    * Assets:Funds:Water         $50.00
    * Liabilities:Funds:Water   $-50.00

= /Income:Salary/ and expr date >= [2015/06/01]
    * Assets:Funds:Water         $55.00
    * Liabilities:Funds:Water   $-55.00

We can't just delete the old rule because then the transactions from before would be off. Instead, we add date expressions to our rules. Ledger's expression grammar is pretty complicated and not very well documented, but this should be sufficient for the rules you'll be writing for automatic envelopes. Ledger's manual has more documentation on automatic transactions.

I put the examples in this gist if you'd like to play with them. You'll need Ledger 3 installed.


Program Your Finances: Algorithmic Savings

When I started my first full time job in 2007 I started putting away a little bit of my paycheck every two weeks into savings. For the past two years I haven't been doing that manually. Instead, I've been using Ledger's fantastic automated transactions to put money away without having to think about it, both for long term goals and envelope budgeting.

Automated saving transactions have been great, except that they never really captured the whole picture, nor did they fit a few constraints I wanted:

  • When a fund is below a minimum threshold it should get priority
  • When a fund is above a maximum it should not receive any more savings
  • I don't want to save any more than I actually have available in a given month

For example, I keep an emergency fund that I keep at about $15k. If it falls below, say, $13k, I want to boost it up as fast as possible. But, if I only have $10 left at the end of the month I don't want to try to save more than that.

Algorithms

Ledger's automated transactions can't reach that kind of flexibility because they don't have access to arbitrary account balances (at least as far as I can tell). Also, because they're evaluated at parse time, the first 300 lines of my ledger file are automated transaction rules.

Instead of using automated transactions, I wrote a little program that generates a transaction to be pasted into my ledger. It takes the three constraints above and turns the cash left over at the end of the month into savings without me having to put numbers into a spreadsheet and manually construct the Ledger transaction.

The algorithm happens in two stages and acts on a set of rules, something like this:

RULES = [
  { 'Emergency'      => { min: 13000, max: 15000, weight: 10 } },
  { 'Medical'        => { min:  1500, max:  4000, weight:  8 } },
  { 'House'          => { min:  3000, max: 15000, weight:  8 } },
  { 'Furniture'      => { min:   200, max:  4000, weight:  4 } },
  { 'Travel'         => { min:  2000, max: 20000, weight:  4 } },
]

It also depends on having a few numbers available, namely the balance of each fund in the set of rules as well as how much excess cash there was at the end of the month.

The algorithm then takes two passes over the rules.

  1. Sum up the weights in all of the rules. If the account balance is greater than or equal to the max, set the weight to zero. If it's below the min, multiply the weight by 4. Keep track of the total weight in the set and the calculated weight for each rule.

  2. For each rule, calculate the percentage "share" by dividing the account weight by the total weight. Then calculate the amount of this share by multiplying it by the remaining income, up to the max for that fund. Subtract that amount from the remaining income, subtract that rule's weight from the total weight, and continue down the rules until you're out of money.

Each rule is evaluated in terms of two shrinking pies: the total weight and the remaining income. When no funds hit their max value this is strictly equivalent to a straight percentage savings, but elegantly deals with both the min and max situations.

Here's what that looks like in code:

account_weights = {}
total_weight = 0

RULES.each do |rule|
  account = rule.keys.first
  rules = rule.values.first
  weight = rules[:weight]

  if (fund_balances[account] || 0) < rules[:min]
    weight = weight * 4
  elsif fund_balances[account] >= rules[:max]
    weight = 0
  end

  total_weight += weight
  account_weights[account] = weight
end

xtns = {}
RULES.each do |rule|
  account = rule.keys.first
  rules = rule.values.first
  weight = account_weights[account]
  balance = fund_balances[account] || 0
  share = weight.to_f / total_weight.to_f

  deposit_amount = [
    remaining_income * share, 
    rules[:max] - balance
  ].min

  next if deposit_amount.round == 0

  total_weight -= weight
  remaining_income -= deposit_amount
  xtns[account] = deposit_amount
end

This algorithm has some great properties:

  • The priority of a fund is determined by it's placement in the rules. Earlier funds get funded before later funds.
  • The amount a fund gets is determined by it's weight. Higher weight gets a bigger share.
  • Funds below their minimum get plumped up with the weight multiplier, while full funds automatically drop out.

The only drawback is that I have to manually run this script every month, but I feel like that's a small price to pay for the flexibility this gives me. If you're interested in the gory details of the script I put the whole thing in a gist. I'd love to hear your thoughts, even if you just want to tell me I'm crazy.


Automatic Finances


My friend Amy Hoy recently tweeted about financial planning and personal finance. This particular tweet stuck out to me:

Last year I radically simplified my family's personal financial system and made everything as automatic as possible. Amy and Joel Hooks asked me to write up how it works, so this is the start of a series of short posts about how and why I set everything up.

A Little Bit of Why

I prefer to think of myself as a realist. Due to my health history my wife is likely going to be around longer than me. Her family has some very long lived female members as well. Her grandmother is 103 and her great aunt just passed in 2018 at the ripe old age of 95.

I want to make our finances as simple as possible so she doesn't have to worry about them when the inevitable happens.

In 2016 my wife and I welcomed our first child into the world and in late 2018 we welcomed our second. They are two more very important reasons why I want things to be simple. If something happens to both my wife and me, I want our intentions with regards to our finances as plain as possible.

This system got a trial run in late 2018. My wife was admitted to the hospital at 29 weeks pregnant for preeclampsia, a very dangerous condition that needs close monitoring. My daugther was born at 34 weeks and spent the next five weeks in NICU.

I didn't have to touch this system at all. Not once. I logged in a handful of times to check up on it, but everything just hummed along.


Automatic Cash Management

The Setup

How Money Flows

  • Payroll direct deposited into brokerage
  • All non-mortgage, non-Amazon expenses are paid with the Fidelity credit card
  • Bills autopay from brokerage (credit cards, insurance, billpay to household vendors)
  • Debit cards and live paper checks written against CMA

Why

We use a brokerage account because it lets us keep cash and investments in the same account. All of our cash, including working capital and reserves, sits in the brokerage's core position. Our core position is FZFXX, a Federal money market fund that pays ~2% interest.

We use the Fidelity credit card because it pays 2% cash back when it's set up to deposit rewards into a Fidelity account. It is also one of the only cards I've seen that can be set to automatically cash out deposits. Ours is set to deposit into the brokerage account.

We use the Amazon Prime credit card for all Amazon expenses. This pays 5% back on Amazon purchases which makes it worth it for us. Your milage may vary.

We have the CMA so we don't expose the brokerage account number every time we write a paper check. This is probably overly paranoid and is the only significant complication in the entire system.

The CMA can optionally have "self-funded overdraft protection" turned on, which would automatically transfer from the brokerage account into the CMA to fund checks and debit card transactions. We don't have this turned on, again mostly for paranoia. We make so few transactions like this that it's no problem to top up the account every few months.

The brokerage core position is not FDIC insured. I don't care about FDIC insurance. FZFXX is composed of ultra short term US Treasury bills. If Treasuries are suddenly not liquid enough to withdraw our money our society has much bigger problems.

The CMA's core position is FDIC insured, and the CMA is almost a full brokerage account, but we don't use it as the centerpiece account for two reasons. First, the CMA core position pays shit for interest. Second, the CMA cannot have margin turned on. We'll talk about why that's important in the next post.


Automatic Insurance

The Setup

  • $1.5 million of term life insurance on me
  • A smaller amount of term life insurance on my wife
  • A private disability insurance policy on me that pays $5,000/mo if I can't program computers anymore, for up to 2 years
  • Employer sponsored short and long term disability insurance
  • Employer sponsored health insurance
  • Auto, home, and umbrella insurance

Why

My future income is currently my family's largest asset.

Term life protects my family if I die before the kids go to college. Disability protects them if I'm incapacitated but don't die. Health protects our assets from medical bankruptcy. Auto, home and umbrella protect our assets from car crashes, fires, and slip and fall incidents.

We own term life insurance because it's the simplest, cheapest possible product. If you die they pay. If you die before 2 years have passed they'll dig into your application and make sure you didn't lie or omit anything, but that's the only wrinkle. Never buy whole, universal, or variable life insurance. It combines insurance with investing on very bad (for the customer) terms.

I have both employer sponsored and private policies because my health history makes it difficult and expensive to get the amount of insurance that we need. The combination, while a little more complicated, helps me sleep easier.


Automatic Investing

The Setup

How Money Flows

  • Every investable dollar is in FFNOX across all account types
  • Automatically purchase a fixed amount of FFNOX in the brokerage account every pay period
  • Automatically contribute to my employer's 401K plan every pay period
  • Once per year fund our Roth IRAs
  • Once per year fund our Solo 401K (if possible/advantageous)

Why

I don't want anyone to have to think about where to pull money from at any time. I want me or my wife to be able to login to Fidelity and sell enough to cover cash needs with a very small number of clicks.

FFNOX is a fund of funds consisting of four inexpensive Fidelity index funds. It invests in 60% US total stock market, 25% international developed total stock market, and 15% US total bond market. This fits our family's desired asset allocation.

The brokerage account has margin enabled. Margin allows you to borrow up to 50% of the value of your investable assets (everything but cash and CDs) from your broker for any purpose whatsoever. It kicks in while you run out of cash and will automatically pay itself back when you deposit cash in the account.

We have margin turned on so that we don't have to worry about selling investments to raise cash while something awful is happening. We can login to Fidelity and sell some FFNOX when it's convenient rather than having to do it one some kind of schedule.

We have automatic investing turned on so that I don't have to make a decision to purchase twice a month. I'm frequently tempted to mess with the program but my own lived experience and that of countless others suggests that the more hands off I can be the better off I'll end up.

Contributing to my employer's 401K plan is an automatic tax break and also means I get my employer's matching contribution. I have it set up to max out my contribution space ($19,000 for 2019) by dividing that by the number of pay checks in the year and setting a fixed dollar contribution.

We fund the IRAs and Solo 401Ks once a year just for practicality.

We're right on the bubble where sometimes we have to do what's called a "backdoor" Roth IRA contribution and that's complex enough that I only want to deal with it once a year.

The amount we can contribute to the 401K depends on how much self-employment income I raised during the year. It only makes sense to calculate at tax time.

We don't use robo-investors. FFNOX's total expenses are capped at 0.08%, or $80 per $100,000 annually. Wealthfront charges 0.25% (more than 3x) on top of the fees for the actual ETFs (typically ~0.1%). Robo-investors are never going to offset their fees when compared to FFNOX or similar funds (Target Retirement funds at Vanguard or Freedom Index funds at Fidelity).