Blog

Rules as Code

When the rulebook is machine-readable and every order is checked against it, a promise becomes a process.

Abstract art on deep navy: a wide tangle of fine pale threads runs in from the left and meets a single luminous gold vertical line; every thread passes the line at its own point and continues to the right in calm, perfectly parallel order.

It’s 7:40 on a Tuesday morning again. The same position has been weak for weeks, the trend your framework watches has turned, and your rules say: cut it in half.

This time, a line is waiting for you before you’ve even opened your broker’s app: Due since yesterday’s close: reduce by half. Not proposed.

Nothing else has changed. Same market, same position, same you. You can still decide not to sell. But the trade that never happens can no longer disappear quietly. It has a date, a rule number and a place in a record that only grows.

This is the last of three posts about the last mile of discipline. The first was about where discipline breaks: not in the framework, but between knowing what to do and doing it. The second was about why a written investment policy doesn’t close that gap on its own. This one is about what changed it for me: rules written precisely enough that a machine can check them.

Not a trading robot

“Rules as code” sounds like algorithmic trading. It isn’t. Nothing in my setup trades, and no machine decides what gets bought. The code does something more modest, and in my experience more useful: it checks whether what you’re about to do matches what you said you would do, and it notices what you said you would do and haven’t.

Think of it as a checklist that reads itself. In 1935, the prototype of Boeing’s new bomber crashed on take-off at Wright Field in Ohio, with an Army test pilot at the controls and Boeing’s own chief test pilot on board. The crew had not released the locks that hold the control surfaces in place on the ground. Two of the five men on board died. What came out of it was not a better pilot, but a checklist.

Medicine learned the same lesson much later. When eight hospitals, from Seattle to Ifakara in Tanzania, introduced a one-page surgical safety checklist, deaths after major operations fell from 1.5 to 0.8 percent, and major complications from 11 to 7 percent. Same hospitals, same kinds of operations. One page.

What a one-page checklist did: eight hospitals, 7,688 patients, before and after a surgical safety checklist. Deaths after major operations fell from 1.5 to 0.8 percent, major complications from 11 to 7 percent. Same hospitals, same kinds of operations, one page. Source: Haynes et al. (2009), New England Journal of Medicine.

From prose to rules

Most investment rules are written for humans: “Keep positions to a sensible size.” “Trim when the trend weakens.” A human can live with that. A human can also reinterpret it on a Tuesday morning.

A rule a machine can check has to answer four questions. What exactly is the limit? What happens when it’s breached? By when? And which version of the rules says so? “Keep positions to a sensible size” turns into something like this:

rule:      max_position
limit:     10% of the portfolio
on_breach: block the purchase
checked:   every proposal, before an order exists
version:   7

Writing rules this way is uncomfortable, and that’s the point. A rule a machine can’t check is usually a rule a human can reinterpret.

From prose to a rule a machine can check: written for humans, "keep positions to a sensible size" and "trim when the trend weakens" are easy to agree with and easy to reinterpret. Written so it can be checked, the rule max_position has a limit of 10 percent of the portfolio, blocks the purchase on a breach, is checked on every proposal and has version 7. Every checkable rule answers four questions: what is the limit, what happens on a breach, by when, and which version says so.

My own investment policy now exists twice. The prose explains the why. A block of machine-readable constants holds the same numbers, and the agents and the code read them from there, without interpretation. Every check records which version of the policy it used. The two forms only ever change together, and if they disagree, the stricter value applies and the check reports the contradiction.

Governments have a name for this. The OECD’s public-sector innovation team calls it Rules as Code: a machine-consumable version of a rule, existing alongside the human-readable one. Asset managers do their own version. A vendor case study describes how one asset manager’s investment guidelines and regulatory limits “were encoded as rules so that pre-trade checks occur before orders are released.”

What the check does, and what it doesn’t

The check chain: a versioned, machine-readable rulebook, written calm; a proposal of what someone wants to do; the check, with PASS, WARN or BLOCK and the rule, target and actual value, which also flags omissions that are due by the rules but not proposed; a human decision and the order; execution from the broker's records; and adherence counted from records, not memory. Every verdict is stored, and an override is recorded next to it, with a reason, not instead of it.

The chain is the same for every proposal. It’s checked against the current version of the rules, and the verdict comes back as PASS, WARN or BLOCK, with the rule, the target and the actual value in one line. Then a human decides. For my broker account, the execution records flow back in after the trade, and adherence is counted from those records, not from anyone’s memory.

At the same time, the check looks for what isn’t there. It compares what the rules say is due with what has been proposed and done. A due reduction that nobody proposed isn’t a remark in a report. It’s flagged on its own, with a date and a rule number.

What the check may not do matters just as much. It has no discretion, never blocks a sale, can’t change a rule and can’t trade. Every verdict is stored, and an override is recorded next to it, not instead of it.

For the first weeks, the binding check in my setup was done by an AI agent reading those constants. Agents are good at this, but an agent isn’t a calculator. So the check chain itself now runs as code, built to give the same verdict for the same inputs, with every fact only ever added, never edited, and more than a thousand automated tests behind it. Until it has matched the agents for twenty trading days in a row, their verdict still counts.

Four leaks, closed

In the first post I described four ways discipline leaks. Here’s what the check does with each of them:

  • The extra few points are blocked before an order exists.
  • The trade that never happens is flagged, with a date and a rule number.
  • The story that beats the signal can still change my mind, but only through a dated rule change or a written, logged override, never quietly through an order.
  • The quiet rewrite has nowhere to hide: every verdict names the version of the rules it used, and the record only grows.

Four leaks, closed: the extra few points are blocked before an order exists; the trade that never happens is flagged with a date and a rule number; the story that beats the signal changes things only through a dated rule change or a written, logged override; the quiet rewrite is exposed because every verdict names the rule version it used, in a record that only grows.

Beyond the portfolio

In the last post I wrote about buckets: a cushion that has to be there in a bad year, a portfolio that keeps your standard of living, and a part that is allowed to swing for something bigger. Their limits are rules too. How much has to be available within a year. How large the part that may swing is allowed to grow. How much of a commitment to a private fund is still open.

Written down like this, the same check can compare them with today, across the brokerage account, real estate and private funds, and show each gap with target and actual side by side. Not every gap should stop an order. A bucket that has grown too large is a reason to look, not to block. But it no longer goes unnoticed.

Agents, one level up

The agents haven’t gone away. They have moved up a level. They read rules, facts and verdicts from the code, and they can do the work around it. An agent can propose a booking, say from a capital-call notice, with the document, the page and the exact passage it relied on. The code checks that proposal like any other: if the call is larger than what is still open on the commitment, it is blocked. Only a person can confirm a booking.

The same holds for the conversational AI assistant that explains what the code decided. Ask why a purchase was blocked, and the answer comes in plain language, with the rule, the target and the actual value. It may not do maths. Every number in its answer is checked against the record before anyone sees it, and a number that doesn’t match is sent back or flagged. It explains, and it never recommends. Agents are good at reading, gathering and explaining. Checking and calculating belong in code.

What we’re building

Running this for my family’s portfolios convinced me that the check chain is useful far beyond them. That’s what I’m building at Aureus 21 Labs, and a first version already runs in a small beta. You bring your own rulebook, whatever framework it encodes, and get it back as rules a machine can check: every proposal checked before an order is placed, what’s missing flagged, and a record that only grows.

It doesn’t pick investments. It only applies the rules you wrote. It’s a tool, not an adviser, and it doesn’t trade. It checks whether you did what you said you would. If you manage other people’s money, that record is also something you can show them.

Here is the one-minute demo, on a sample account with made-up holdings. The mandate says: gold at most 10 percent of the portfolio, and no more than 10 points added on any one trading day. A proposal comes in: buy €35,000 of gold, reason: rebalancing. It looks harmless.

The check answers in one line: BLOCK. The purchase would lift gold from just over 9 percent to about 23, above the limit and above the daily cap. Then comes the line that matters more: a reduction is due, and nobody has proposed it. An error and an omission, caught in the same check.

The machine decides nothing. It checks, it records, and a human decides. But it never forgets, and it never has a Tuesday morning.

Three posts, one idea

Knowing your rules was never the problem. Writing them down is where discipline starts. Making them checkable is how the last mile gets walked, every Tuesday morning, whether you feel like it or not.

If you run your own rules, for yourself or for clients, and you’d like to see them checked this way, try Trebun with your own portfolio or write to me.


Alexander Altenhofen is the founder of Aureus 21 Labs and SVP Product & Tech at a large media company. He privately runs his family’s portfolios on a written rulebook with a team of AI agents. This post describes his own approach and is not investment advice.