Contents Lesson 13 of 18

10 min read · practitioner

Rules that tell you when to look

By the end of this lesson you will have one threshold rule stored as data, firing on your page as a sentence that names the rule and the number, a test of the evaluator, and a line on screen that says when rules are checked.

The point of a watchlist is not to be watched. It is to tell you when watching is worthwhile. This lesson adds threshold rules; it also draws a line under what they can honestly promise on the data you currently have.

A rule is data, not code

The instinct is to write an if. Don't. Keep rules as data, in a file of their own the way your tickers live in src/lib/watchlist.ts, so they can be edited, listed, and reasoned about. Not in browser storage like your notes: the server renders the page and checks the rules on that render, and it never sees what the browser stored. A rule you add by editing a file needs no editor of its own.

type Rule = {
  ticker: string;
  field: "change_p" | "close";
  op: ">" | "<";
  value: number;
};

{ ticker: "AAPL.US", field: "change_p", op: "<", value: -3 } reads as "tell me if Apple is down more than 3%". Data-shaped rules can be shown to the user, exported, and evaluated in one place:

function fires(rule: Rule, q: Quote): boolean {
  const actual = q[rule.field];
  return rule.op === ">" ? actual > rule.value : actual < rule.value;
}

One function, every rule. Compare that with fifteen bespoke if statements scattered through a component, which is what you get if you let this grow organically.

Say what fired, not that something fired

A row that turns red tells you nothing you can act on. The useful sentence names the rule and the number:

AAPL.US down 3.4% — your rule was −3%

That is the difference between an alert you trust and a decoration you learn to ignore. And once again: colour is never the only signal. Red plus a written reason survives a colour-blind reader and a phone in sunlight.

The honest limit, and it is the point of this lesson

Your rules evaluate when the page renders. That is all they can do. There is no server watching your instruments while you sleep, so:

  • A rule cannot fire while the tab is closed.
  • A rule cannot fire between two loads: a price can cross your threshold and come back, and you will never know it happened.
  • The number you compare against is as old as the timestamp on your data.

Write that in the interface. Something like "Rules are checked when this page loads" costs one line and prevents the worst possible outcome of this whole course: someone believing their tool is watching for them when it is not.

Real alerting needs something running on a schedule, something storing state so it does not tell you twice, and data fresh enough that the alert is not already stale when it arrives. All three are real work, and the third one is where you meet the wall in the next unit — which is exactly the honest version of this feature's story.

Do not fix this by polling

The tempting move is setInterval on a short timer. Do the arithmetic before you write it, with the per-symbol price from unit 1: a twelve-instrument watchlist refreshing every 30 seconds is 120 refreshes an hour × 12 = 1,440 calls an hour, roughly 9,400 in a 6.5-hour session, from one open tab.

Against a free allowance of 20 calls a day that is over in the first minute (the one-time 500 bonus within the hour), and you will have spent it on a page nobody was looking at.

Your usage meter will show this happening. That is why it has been on screen since unit 1.

Try it now

The prompt

Mock mode on, plan mode on, and paste:

Add threshold rules as data: a Rule type shaped { ticker, field, op,
value }, where field is "change_p" or "close" and op is ">" or "<",
and fires(rule, quote) in src/lib/rules.ts, and one rule for one of my tickers in
src/lib/my-rules.ts, close enough to its value in mock mode that it
fires. Plan first and name the files. Write tests/rules.test.ts before
the evaluator, against literal quotes: a rule that fires, one that does
not, and a value exactly at the threshold, which does not fire. Show me
them failing, then the code. On the page, a fired rule is a sentence
naming the ticker, the move and my threshold, never a colour alone.
Leave a marked place for one sentence I will write about when rules
are checked. No new request and no timer. Run npm test,
npm run typecheck and npm run check:leak.

A good answer adds src/lib/rules.ts, src/lib/my-rules.ts and the test, changes the page only where the fired rule is shown, and adds no setInterval and no request.

Check it yourself

  1. Red first, then green, and every earlier test still passes.
  2. In mock mode the fired rule reads as a sentence: the ticker, how far it moved, and your threshold.
  3. grep -rn setInterval src prints nothing.
  4. The honest sentence about when rules are checked is on the screen, in your own words. Read it back and ask whether a person who had not built this tool would be misled by anything on the screen.

Break it on purpose

Change the comparison in fires to its "or equal" form (< to <=, or > to >=: the one your at-the-threshold test uses) and run npm test. The threshold test goes red: a rule that says "down more than 3%" and fires at exactly 3% is a disagreement nobody spots on a screen. Put it back.

Compare with ours

Our boundary test, and what the page says
const down3 = { ticker: "X", field: "change_p", op: "<", value: -3 } as const;

it("fires below the threshold, not at it", () => {
  expect(fires(down3, quote("X", -3.4))).toBe(true);
  expect(fires(down3, quote("X", -3))).toBe(false);
});
X down 3.4% — your rule was −3%
Rules are checked when this page loads, not while it is closed.

The full state at the end of this unit is on the starter's branch ref/build-your-first-tool/u3.

Checkpoint

npm test
git status   # only the files you expect, and no .env*
git add -A
git commit -m "Rules as data, checked on load, and the page says so"

git status should list the rules module, your rules file, the test and whatever shows the fired rule, and no .env* file. Then answer this lesson's questions.