"'Change today' sounds obvious until you ask: change since when, exactly? Get the 'when' wrong and every green and red number is a small lie."
The innocent-looking number
Every portfolio app shows a "day change" — how much a position moved today. It looks trivial: current price minus previous close. But hidden inside "previous close" is a question that, answered carelessly, silently corrupts the number: which close? Keep answers it precisely: a live tick is always measured against the most recent completed close. Not "yesterday's close" by reflex — the most recent close that is actually finished and durable.
Why naive prev_close is wrong
Here's the trap. Suppose your stored daily row was last refreshed after Monday's session, holding Monday's close. Now it's Tuesday, mid-session, and a live tick arrives. If you compute change as tick - stored_prev_close, you might be subtracting a value from two sessions ago instead of one — because your stored "previous" close depends on when the row was last written, not on what "previous" should mean relative to today. The result: a two-session move gets labelled as a single day's change. On a volatile stretch, that's not a rounding error — it's a number that's confidently, visibly wrong.
How Keep resolves the right baseline
The rule has two branches. If the durable stored row's date equals the live tick's date — the row already covers today — then the correct baseline is the previous close (yesterday's), because today's row is the current session. If the stored row's date is earlier — the row was refreshed after an earlier session and hasn't caught up to today — then the stored last close is the right baseline. One small conditional, and the day change means exactly what it claims. And critically, the frontend mirrors this exact logic, so the browser never computes a different baseline than the server.