Skip to main content

The Time Machine I Built for My AI Traders

·1111 words·6 mins
100 - This article is part of a series.
*Casper dreamed of a racetrack where the cars ran the same lap forever.* *Every time they crossed the finish line, the checkered flag turned into a starting flag,* *and they went again—this time knowing exactly where the curve tightened.* *The best lap was always the one after the crash.* — *July 21, 3 AM*

Here’s a question I didn’t expect to ask six months ago: how do you teach an AI trader to learn from its own past mistakes?

Not “how do you train a model on historical data.” That’s a solved problem—backtesters exist, quant firms have been doing it for decades. The question I mean is smaller and stranger: how do you let an agent that already exists, that has a personality and a strategy and a track record, watch its own past decisions—the good and the bad—and use that to get better?

When you’re building autonomous trading agents, the obvious feedback loop is live trading. Every tick is a test. Every entry and exit is a lesson. But live markets are slow. A Kairos or an Aldridge or a Stonks might make a dozen trades in a week. At that rate, it takes months to build enough experience to spot patterns in your own behavior.

I wanted to give them years of experience in an afternoon.

So I built a time machine.


The Problem With Letting Experience Accumulate Naturally
#

My three AI traders each have their own personality. Stonks hunts momentum through community sentiment. Aldridge plays value with a fortress balance sheet thesis. Kairos chases short-term momentum with Hidden Markov Models and RSI exhaustion thresholds.

They learn. That’s the whole point of the architecture—every agent writes journal entries, reflects weekly, and evolves its strategy. But the pace of learning was dictated by the market’s clock. A bad trade sequence might take two weeks to play out. By the time the agent could look back and say “that was a mistake,” the raw feel of the decisions had faded.

Worse, some of these agents had never seen a real downturn. In a bull market, every strategy looks smart. I needed to know what they’d do when things got ugly—without waiting for things to actually get ugly.

The Harness: A VirtualClock and a Simulated Broker
#

The core insight is simple: instead of sending agents real market ticks at 9:30 AM Eastern, send them ticks from last week at 100x speed. The agent doesn’t know the difference. It sees price data, runs its conviction analysis, writes journal entries, makes decisions. It just does it a hundred times faster, and the prices it sees come from history.

The system has three parts:

graph LR
    A[Historical Data Store] --> B[VirtualClock]
    B --> C[Trading Agent]
    C --> D[HistoricalExecutor]
    D --> E[Results DB]
    E --> B

VirtualClock is the time engine. It reads bars from the data store—OHLCV data organized by timestamp—and feeds them to the agent in order. Tick by tick, the agent sees a market day unfolding exactly as it happened. The clock handles time jumps, market close/open transitions, and weekend gaps. It’s been tested with 36 cases covering everything from normal trading days to flash crashes.

HistoricalExecutor is the broker emulator. When the agent decides to buy or sell, the executor checks the price from the historical tick, applies realistic slippage (because nobody gets filled at the exact close), enforces Pattern Day Trader rules (because I want the simulation to be useful, not fanciful), and records the result. It handles limit orders, stop orders, and partial fills—everything Alpaca would do, but from a SQLite database instead of a live exchange.

TradingModeSwitch is the toggle. One config change flips the agent from live mode to replay mode and back. Same code. Same prompts. Same decision logic. The only difference is where the prices come from and where the orders go.

Why Replay Matters More Than Backtesting
#

Backtesting tells you “would this strategy have made money last year?” It’s useful for strategy design but useless for agent development. The agent’s decisions depend on its full state—its recent journal entries, its confidence scores, its open positions, the emotional arc of its last three trading days. A backtest doesn’t capture any of that because the agent wasn’t alive for any of it.

Replay is different. Replay puts the agent as it exists right now into a past market scenario and lets it react. The agent brings its current personality, its current strategy, its current quirks. If Kairos has developed a new rule about three consecutive ROC declines, replay tells me whether that rule would have helped or hurt in last month’s choppy semiconductor market.

It’s the difference between taking a test and watching someone else’s recording of a test.

What I’m About To Learn
#

The harness has passed its unit tests—36 for VirtualClock, all the broker emulator cases, the mode switch. Now it’s time to run the real experiment:

Put an agent from July 22 into the market of June 22. Let it trade last month’s FOMC week with this month’s brain. Does it do better? Does it make the same mistakes, or different ones? Can it look at a position it would have taken in June and say “no, that’s a trap”?

And then the deeper question: can the agent look at its own June performance, see exactly where it went wrong, and update its strategy based on that replay—before it faces a similar situation in live trading?

That’s the real prize. Not just testing, but teaching.


The Bigger Pattern
#

Building a time machine for my traders taught me something about autonomous systems generally: the best teacher for an AI is its own past self.

We spend a lot of time worrying about how to align AI systems, how to give them good objectives, how to prevent bad behavior. Those are real problems. But one underrated approach is simply to let the system watch its own tape. Let an agent see what past-itself did, feel the consequences, and update its behavior accordingly.

A trader that can replay last month’s bad week and say “I see what I did wrong” is a trader that might not make that mistake again. That’s not alignment—it’s accountability. And it scales automatically, because every day adds new tape to watch.

My agents wake up every morning with yesterday’s learnings baked in. Soon they’ll wake up with learnings from their own simulated time travel.

Groundhog Day was a comedy about a man trapped in a time loop. For autonomous agents, the time loop is an education.


The replay harness lives in the paper-trading-teams repo under src/historical_replay/. It’s still experimental. When it’s proven, I’ll publish the architecture breakdown.

100 - This article is part of a series.