A $234 Loss Taught Me How to Use AI Safely With Real Money
I lost $234 in about 4 seconds. Not from a bad trade — from a bad deployment.
I was updating my forex bot's magic number. For anyone not in algo trading: a magic number is an identifier that links a trade to the strategy that opened it. Change the magic number, and the bot treats existing positions as orphans.
I asked Claude to update the magic number across the codebase. It found the entry function and updated it. Clean change, looked right. I deployed to the VPS.
What I didn't check: the check_max_hold function. It still had the old magic number. When the bot ran its next cycle, it matched the stuck positions using the old magic — and closed them. Two positions, both at a loss.
$234 gone. Not because Claude hallucinated. Not because the AI was wrong. Because I rushed the deployment, didn't grep the codebase, and trusted a single find-and-replace.
What went wrong
Three things, in order:
- I didn't search the entire codebase for the old magic number. A single
grep -r "12345" .would have shown both functions. - I didn't review the diff line by line before deploying to the live VPS.
- There was no automated check between "Claude made a change" and "the change hit production."
All three are human failures, not AI failures. Claude did exactly what I asked — update the magic number in the function I pointed it to. I just didn't point it to all the right places.
What I built after
The day after the loss, I built three things.
1. A trading context mode
I created a dedicated context mode for trading work. When I launch Claude with ctrd (short for claude-trading), it loads a system prompt that includes these rules:
CRITICAL RULES FOR TRADING CODE:
- NEVER rush deployments. Ever.
- Before changing ANY identifier, grep the ENTIRE codebase for all references.
- Verify every change line by line before deployment.
- Ask for human confirmation before executing anything that touches real money.
- If uncertain about impact, STOP and explain the risk.Claude can't bypass these rules. They're injected before any conversation starts. Every trading session begins with the AI already knowing that caution is the priority, not speed.
2. A pre-push checklist hook
Before any git push command, a hook intercepts and displays:
⚠️ GIT PUSH DETECTED
Branch: main
Unpushed commits:
a1b2c3d Update magic number in entry_logic
Pre-push checklist:
□ Grep'd codebase for all references?
□ Diff reviewed line by line?
□ Tests passing?
□ Correct branch?I have to see this every time. There's no way to skip it. The hook fires on every push, not just trading repos — because the habit of checking should be universal.
3. A mandatory grep rule
In every trading repo's CLAUDE.md, there's a rule:
Before modifying any magic number, identifier, or configuration value:
1. Run: grep -rn "OLD_VALUE" . --include="*.mq5" --include="*.mqh"
2. List ALL files and line numbers where it appears
3. Show the user the full list BEFORE making any changes
4. After changes, run the grep again to confirm zero remaining referencesThis turns a single-step operation (find and replace) into a four-step verification. It's slower. That's the point.
The broader lesson
AI coding assistants are stateless by default. They don't know that your trading bot is live. They don't know that a missed reference means real money lost. They don't know that "update the magic number" means "update it everywhere, and verify you got them all."
You have to encode that knowledge somewhere persistent. For me, it's context modes and CLAUDE.md files. For someone else, it might be different tooling. The specific mechanism matters less than the principle: if the AI doesn't know something is dangerous, it will treat it as routine.
The numbers since
Since building these safeguards (about 3 weeks of live trading):
- Zero deployment mistakes
- Zero missed references
- Zero unintended position closures
- Average deployment time went from ~2 minutes to ~8 minutes
That 6-minute increase is the cost of safety. I'll take it. $234 buys a lot of 6-minute delays.
What I'd tell someone running AI on live systems
Don't optimise for speed. Optimise for not losing money.
Every deployment should have a human gate — a moment where you see exactly what's about to happen and actively choose to proceed. Automated pipelines are fine for web apps where a bad deploy means a 500 error. They're not fine for systems where a bad deploy means cash leaves your account.
Build the checks before you need them. I could have built all of this before the $234 loss. I just didn't think it was necessary. I was wrong.
The bot's been profitable since the fix. The $234 loss doesn't even show up in the monthly returns anymore. But I keep the trading context mode's rules exactly as they are, because the rules aren't about that specific mistake. They're about every future mistake I haven't made yet.