Last week a program I wrote closed every open position on a live trading account and refused to do any more work for the rest of the day.

Nothing had gone wrong. The owner had moved some of their own money out of the account.

The bug turned out to be a shape I have written a dozen times in completely unrelated systems, so it is worth writing down.

The code

The program guards against losing too much in a day. It stores a baseline when the day starts and compares the current value against it: