← Notes

Trust the wallet, not the ledger

For a week our database showed a modest profit while the wallet was flat. Every trading system keeps its own books, and they drift from reality in ways that are anything but random.

A trading system writes down what it did: what it bought, what it sold and what it received. Those records drive everything else, from the profit dashboard and position sizes to the labels the next model trains on. It is natural to treat them as the truth.

They aren’t. They are the system’s belief about what happened. The only truth is what actually arrived in the wallet, and the two drift apart more than you’d expect.

How a ledger drifts

Three ways ours did, each found by comparing the books against the chain:

  • A confirmation that never came. For one kind of trade, the system was not listening on the channel that would have confirmed the sale. Every sale timed out waiting, and the fallback recorded the expected proceeds instead of the real ones. The trades themselves were fine; their records were estimates.
  • A cost added back. When a sale needed to land urgently, the system paid an extra fee to get it included quickly. The recorded proceeds were taken before that fee. Every urgent sale looked better on paper than in the wallet.
  • A sale that unhappened. A sale was seen in a block that the network later dropped. The system saw it, recorded it as sold and moved on. The tokens were still in the wallet.

The errors are not random

Any one of these sounds like a small accounting error that averages out. They don’t, and this is the important part: each of them is worse on losing trades.

Urgent sales happen when the price is falling fast, so the fee was paid mostly on losers. Timed-out confirmations were worst when the market was moving away from the expected price, which is, again, mostly on losers. When we corrected the records from the chain for one batch, the total barely moved, by less than half a percent. Per trade, though, the losing trades had been overstated by about forty percent on average, while the winning ones were almost exact.

A ledger that is right on average and wrong on the losers is quietly dangerous. Its total looks fine. But a model trained on those records learns that its bad trades were less bad than they were, and sizes positions accordingly.

How we reconcile

  • Compare totals over clean windows. Pick a period with no deposits or withdrawals and compare the wallet’s balance change with the sum the ledger reports. Any gap larger than fees is a bug.
  • Read what landed. Record the amount from the confirmed transaction itself, not the amount the system expected.
  • Check after closing. After every sale, check a little later that the position really is empty, from a source independent of the one that reported the sale.
  • Never judge profitability from the database alone. It is a cache of the chain. When they disagree, the chain wins.

The week the database showed a profit and the wallet was flat was the clearest version of the lesson. It was also a cheap one: the trades were real, but the only thing wrong was what we believed about them.