Building PSX Investment OS: an immutable ledger, decimal arithmetic, and an AI layer that is only allowed to say what the data supports.
PSX Investment OS is a private tool for tracking investments on the Pakistan Stock Exchange. Money software has a simple rule: wrong numbers are worse than missing numbers. That rule drove most of the design.
The ledger never changes
Transactions are added, corrected or reversed, but never edited in place. A correction is a new entry that points at the old one. This gives a complete history, makes audits trivial, and means every CSV import can be rolled back as a batch if the file turns out to be wrong.
Holdings are derived
Positions aren't stored. They're reconstructed from the ledger using FIFO, which also produces realised profit and loss, cash and dividends. Because holdings are always computed from the ledger, they can't drift from it.
Decimals, not floats
JavaScript's floating point can't represent 0.1 exactly, which is a bad property for money. Every calculation uses decimal.js.
import Decimal from "decimal.js";
const cost = new Decimal(shares).times(price).plus(fees);
No invented numbers
Prices come only from verified end-of-day observations. A stale price is flagged as stale. A value that isn't known stays blank rather than defaulting to zero or being estimated. A chart with a visible gap is honest. A chart with a plausible fake is dangerous.
An AI that has to show its work
The recommendation module is allowed to produce a suggestion only if it can cite the evidence behind it: a price observation, a research note, a position in the ledger. If there isn't enough data, the correct output is to say so. The aim is to enforce this in the pipeline, not just ask for it in the prompt.
Takeaways
- Make history append-only and derive everything else.
- Use exact arithmetic for money.
- Prefer a visible gap to a plausible guess.
- With AI, require citations and validate the output shape in code.