Building PSX Investment OS: an immutable ledger, decimal arithmetic, and an AI layer that has to point at the data behind what it says.
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 allocates cost basis and produces realised profit and loss. Cash and dividends come from their own ledger entries. Because holdings are always computed from the ledger, there is no separate stored balance to fall out of step with it. Bugs in the calculation or an import can still produce wrong numbers, which is why imports can be rolled back.
Decimals, not floats
JavaScript's floating point can't represent 0.1 exactly, which is a bad property for money. Calculations use decimal.js. It has configurable precision and rounding, so the rounding policy has to be decided explicitly, and values should be built from strings rather than floats.
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. A citation only proves the output points at some evidence. It doesn't prove the evidence supports the claim, so that is a separate check.
Takeaways
- Make history append-only and derive everything else.
- Use decimal arithmetic for money and choose the rounding rules on purpose.
- Prefer a visible gap to a plausible guess.
- With AI, require citations and validate the output shape in code.