Forensic accounting software finds a missing bank statement by chaining balances: each statement's printed closing balance has to equal the next statement's printed opening balance, and any month where that link breaks is a month you don't have. It's the core check inside the forensic accounting software we build at ZeraBooks. It's also small enough to write yourself, so here it is in Python.
Quick disclosure. We make ZeraBooks and we rank it first in our own forensic rankings. ZeraBooks is AI bookkeeping software that runs books completely automated, and for accounting firms the same engine processes client documents at volume with custom workflows. None of that matters for the code below, which works on any parser's output.
What does a balance chain actually check?
Two links, at two levels.
Inside one statement, the opening balance plus money in minus money out has to land on the closing balance the bank printed. Between statements, March's closing has to be April's opening. The first link catches a misread row. The second catches a statement nobody produced, even when nobody mentions it.
The data here comes from Matter 25-117, a made-up matter we use in our forensic examples. Checking ••4417 has a March 2025 statement and a May 2025 statement. April never arrived.
How should the statements be modeled?
Use Decimal for every balance. Floats drift by fractions of a cent, and a balance chain built on floats reports gaps that aren't there.
from dataclasses import dataclass
from decimal import Decimal as D
from typing import Optional
@dataclass(frozen=True)
class Statement:
account: str # last 4 digits
period: str # "YYYY-MM"
opening: D # as printed
closing: Optional[D] # as printed; None if not read yet
money_in: Optional[D] = None
money_out: Optional[D] = None
source_file: str = ""
statements = [
Statement("4417", "2025-03", D("14902.35"), D("18240.55"),
D("8120.00"), D("4781.80"), "4417_2025-03.pdf"),
Statement("4417", "2025-05", D("21905.10"), None,
source_file="4417_2025-05.pdf"),
]
Does each statement tie on its own?
def ties(s):
if s.money_in is None:
return None # totals not loaded yet
return s.opening + s.money_in - s.money_out == s.closing
for s in statements:
print(s.account, s.period, ties(s))
# 4417 2025-03 True
# 4417 2025-05 None
March works: 14,902.35 + 8,120.00 − 4,781.80 = 18,240.55. I left May's totals out on purpose, since the chain only needs its opening.
Where does the chain break?
Sort each account by period, walk neighboring pairs, and report two things: calendar months skipped, and any difference between one closing and the next opening.
def month_index(period):
year, month = map(int, period.split("-"))
return year * 12 + month - 1
def month_label(idx):
return f"{idx // 12}-{idx % 12 + 1:02d}"
def chain_breaks(stmts):
by_account = {}
for s in stmts:
by_account.setdefault(s.account, []).append(s)
for account, items in by_account.items():
items.sort(key=lambda s: s.period)
for prev, nxt in zip(items, items[1:]):
if prev.closing is None:
continue
start = month_index(prev.period)
skipped = month_index(nxt.period) - start - 1
gap = nxt.opening - prev.closing
if skipped or gap:
missing = [month_label(start + k + 1) for k in range(skipped)]
yield account, prev, nxt, missing, gap
for account, prev, nxt, missing, gap in chain_breaks(statements):
print(f"{account}: {prev.period} closes {prev.closing}, "
f"{nxt.period} opens {nxt.opening}")
print(f" missing months: {missing}, unexplained movement: {gap}")
# 4417: 2025-03 closes 18240.55, 2025-05 opens 21905.10
# missing months: ['2025-04'], unexplained movement: 3664.55
That 3,664.55 is the net movement of a month you can't see. Report both signals separately. A skipped month with a zero gap is still a missing statement; the account just happened to end April where it started. A gap with no skipped month usually means the opening balance was misread.
What about the same statement uploaded twice?
Productions repeat themselves. In the same matter, the February statement for Amex ••1008 came in twice under different file names. Fingerprint the rows, not the file name.
import hashlib
def fingerprint(account, period, rows):
h = hashlib.sha256(f"{account}|{period}".encode())
for date, description, amount in rows:
h.update(f"{date}|{description}|{amount}".encode())
return h.hexdigest()
# Placeholder rows standing in for the 41 Amex lines.
feb_rows = [(f"2025-02-{i % 28 + 1:02d}", f"charge {i}", "10.00") for i in range(41)]
uploads = [
("amex_1008_feb.pdf", "1008", "2025-02", feb_rows),
("Amex Feb (2).pdf", "1008", "2025-02", list(feb_rows)),
]
seen, kept = {}, []
for name, account, period, rows in uploads:
key = fingerprint(account, period, rows)
if key in seen:
print(f"{name} repeats {seen[key]}, skipped")
continue
seen[key] = name
kept.extend(rows)
print(len(kept), "Amex rows counted") # 41, not 82
What won't this catch?
- An account nobody produced at all. It has no chain to break. You find it when a transfer names an account ending that isn't on your list.
- A month missing inside a quarterly brokerage statement. You need the monthly figures inside the quarter, not just the quarter's ends.
- Card statements that print a previous balance instead of an opening balance. Map that field before you chain anything.
- Two errors that cancel. Rare, but the tie-out can't see them.
That screenshot is a separate fictional sample run, a different account from the one above, with the same April hole.
If you'd rather compare tools that already do this, zerabooks.com's ranking of 9 statement analyzers lists what each one's own site says about proving rows.
Chain first. Everything you calculate later assumes the months are all there.

Top comments (0)