I'm a software engineer with a day job, and on the side I build FinSight, a budgeting app that connects to your bank through Plaid with read only access. One of the screens I care most about is the balance forecast: a day by day line for the next 90 days that shows where your checking and savings are heading, which bills land on which day, and the first date the balance would go below zero if nothing changes.
The first version of that line looked reasonable and was wrong. Not wrong in a way that crashes, wrong in the way that is worse: it produced confident numbers that leaned in one direction. This post is about the two bugs, the fix, and what I took from it.
How the forecast works
The model is simple on purpose. There are two ingredients.
The first is recurring cash flow. A detector groups transactions by normalized merchant name, direction and account, takes the median gap between dates, and snaps it to weekly, biweekly, monthly, quarterly or yearly. Paychecks, rent, phone bills and subscriptions all come out of that, and each one gets projected forward on its own schedule.
The second is what I call drift: the everyday spending that does not repeat on a schedule. Groceries, gas, a pizza on a Friday. Recurring bills alone make the future look far too rosy, because most of what people spend is not a bill. So the forecast estimates an average daily amount and subtracts it every day.
Then it walks forward one day at a time from today's liquid balance (checking, savings and cash accounts only, no investments):
let running = startingBalance; for (let day = 0; day <= days; day++) { if (day > 0) { running += netByDay.get(day) ?? 0; // scheduled income and bills running -= dailyDrift; // everyday spending } points.push({ day, balance: Math.round(running * 100) / 100, /* ... */ }); } From that series it pulls the lowest point and the first negative day, which is what the screen actually leads with. A date is more useful than a warning.
Everything interesting is inside dailyDrift.
Bug one: not every outgoing dollar is spending
Drift was computed as total expenses in the history window, minus the part already explained by detected recurring bills, divided by the number of days.
The problem is the word "expenses". From the bank's point of view, paying your credit card bill is money going out. So is moving money from checking to savings. So is a loan payment. If the detector had not yet picked one of those up as its own recurring series, it landed in the discretionary pile and got spread across every single day of the forecast as if it were coffee and groceries.
Worse, when the card is connected too, the payment is double counting. The purchases already show up as spending on the card account, so counting the payment again makes the person look like they spend far more than they do.
The fix was not new logic. The reports page already had a classifier that finds credit card payments, transfers between your own accounts, and loan payments. The forecast just was not using it. Now the query builds one exclusion set and hands it in:
const excludedFromDrift = new Set([ ...ccPaymentIds, ...internalTransferIds, ...loanPaymentIds, ]); and drift only sums what is left:
const totalExpenses = window .filter((t) => t.amount > 0 && !excludedIds.has(t.id)) .reduce((s, t) => s + t.amount, 0); Loan and mortgage payments are only removed from drift, not from detection. They are real bills, so when they repeat they still get their own marker on the timeline. They just should not be smeared across every day as everyday spending.
Bug two: the denominator was a constant
The history window is the previous three full months plus the current month to date. The old code divided by the length of that window, always.
That is fine for someone with months of history. For someone who connected a bank last week, it is badly wrong. Ten days of real spending divided by roughly ninety days says they spend about a ninth of what they actually do. The forecast for a brand new account came out flattering, which is exactly the person you least want to mislead, because it is their first impression.
The fix is to divide by the history you actually have:
const earliestDate = window.reduce<Date | null>( (min, t) => (min === null || t.date < min ? t.date : min), null ); if (!earliestDate) return { dailyDrift: 0, historyDays: 0 }; const historyDays = Math.max( 1, differenceInDays(startOfDay(now), startOfDay(earliestDate)) ); The same historyDays is used to estimate how much of the window's spending was recurring (each bill's amount times how many of its intervals fit in that span), so both sides of the subtraction cover the same days.
The function now returns historyDays along with the rate, and the UI uses it. Under two weeks of history, the chart says plainly that the daily amount is based on only that many days and will get more accurate as transactions come in. The number did not get more certain, so the screen should not pretend it did.
What the forecast still does not do
Irregular income is the honest gap. Income only gets projected when it repeats on a schedule the detector can see: at least three deposits and a gap that snaps to a known frequency. A freelancer paid at random times gets no projected income, and the line will trend down. I think that is the right failure. A forecast that invents a paycheck is more dangerous than one that leaves it out. The same care applies the other way: once someone confirms or dismisses a detected series, the detector stops projecting its own copy, so a confirmed bill is never counted twice.
What I would tell another developer
- Averages hide bad denominators. Whenever you divide by a time window, ask whether the data actually fills it. A constant divisor is a bug waiting for a new user.
- "Money out" is not "spending". Transfers, card payments and loan payments need to be classified before you average anything, and if another part of your app already classifies them, reuse it so the two screens agree.
- When an estimate is thin, ship the uncertainty with the number. Returning
historyDaysnext todailyDriftcost one field and let the UI tell the truth. - Write the test for the new account, not just the established one. Both of these bugs only show up at the edges, and the fix landed with unit tests for the history span, each exclusion category, and the zero history case.
If you want to see how the forecast is presented, the overview is at finsight.it.com/cash-flow. The recurring detector behind it also powers a free Subscription Finder that runs in your browser on a bank CSV, no account needed. Happy to answer questions about any of this in the comments.
