How to Build a Self-Improving AI Trading Research Loop

This guide walks you through building the same research loop every quant desk runs, with AI agents doing the heavy lifting: research, code, backtest, paper trade, post-mortem, and the part almost nobody has built yet, a memory that makes every cycle smarter than the last. You get the full architecture, copy-paste prompts for Claude Code, the testing rules that keep a "self-improving" system from fooling you, and a kill switch that actually works. Expect a weekend to build the first version and weeks of paper trading before any real money touches it.
Don't want to figure this out alone? I walk members through every step inside the community. Join the Skool → skool.com/raycfu
Read This First

This is not financial advice, and it is not a money machine. Most trading strategies lose money, including most AI-built ones. What this system does is run the research process faster and more honestly. Whether it ever finds a real edge is a separate question.
There is one danger you need to understand before anything else. A loop that says "the backtest failed, rewrite the strategy, test again, repeat until it passes" will always eventually pass. Not because it found something real, but because it tried enough versions that one fit the past by luck. This is called overfitting, and it's the number one way automated research loses money. Half of this guide is about stopping it.
Everything here runs in paper trading, with fake money on real market data, until the system has proven itself. Do not skip that.
The Loop Every Quant Desk Runs
Strip away the salaries and the office, and a hedge fund research desk is one cycle repeating:
1. Research: find an idea worth testing.
2. Code: turn the idea into a strategy a computer can run.
3. Backtest: attack it with historical data until it proves itself or dies.
4. Live: run it (for you, paper trading first).
5. Post-mortem: read what actually happened, and why.
6. Remember: fold the lesson back in so the next idea is sharper.
A human team runs this loop slowly. AI agents can run it far faster. The first five steps are now easy to build with tools you already have. The sixth, memory, is the one that turns a pile of experiments into a system that actually learns.
What You Need
Claude Code, to build and run the agents.
Python 3.10 or newer.
A free Alpaca account with paper trading turned on (alpaca.markets). It gives you real market data and a fake-money account through an API.
Historical price data for what you want to test. Alpaca provides some. For deeper history, use any data provider you trust.
A weekend for the first build.
Step 1: Set Up the Project
Open Claude Code in an empty folder and paste this to build the skeleton:
I'm building an AI-assisted trading research loop that runs in paper trading only. Create this project structure and stop for my approval:
- research/ for idea memos
- strategies/ for strategy code, one file per strategy, each with a version number
- backtests/ for results
- journal/ for every live paper-trading decision and its reasoning
- memory/ for the hypothesis ledger (a SQLite database)
- config/ for risk limits and API keys (gitignored)
- data/ for historical prices (gitignored)
Use alpaca-py for paper trading. Never connect a live-money account. Put my Alpaca paper keys in config/.env and never print them.
Step 2: The Research Agents

Research is where ideas come from. The pattern that works is several specialist agents that each look at the market from one angle, working independently so they don't copy each other's opinions.
Give each one a narrow job. For example:
A Price Analyst that looks only at price and volume patterns.
A Fundamentals Reader that reads earnings reports and company filings.
A Macro Watcher that tracks interest rates, economic data releases, and sector trends.
A Skeptic whose only job is to argue why each idea will fail.
Two rules make this work:
Run them separately. Each agent writes its own memo without seeing the others. If they read each other's work first, they anchor on the first opinion and you lose the point of having several.
Check the memory first. Before any agent proposes an idea, it checks the hypothesis ledger from Step 7 to see whether that idea was already tested. This is what stops the system from rediscovering the same failed idea every week.
Prompt for each research agent:
You are the [ROLE]. Look only at [YOUR ANGLE] for [MARKET OR WATCHLIST]. Before proposing anything, query memory/ledger.db for ideas already tested on this market and do not repeat any that failed unless market conditions have clearly changed. Propose at most 3 testable trading ideas. For each, state: the idea in one sentence, why it might work, the exact market conditions it needs, and what result would prove it wrong. Save to research/[ROLE]-[DATE].md. Do not read other agents' memos.
Step 3: Turn Ideas Into Code
This is the part AI coding agents are best at. Strategy code is small, clearly defined, and easy to test.
Every strategy should be written in the same shape, so the backtester and the live system can run any of them:
Entry rules: exactly when to buy or sell.
Exit rules: when to take profit and when to cut a loss.
Position sizing: how much to trade.
Risk limits: the maximum it's allowed to lose.
Prompt:
Take the idea in research/[MEMO].md and write it as a strategy in strategies/, following the same structure as the other strategies: entry rules, exit rules, position sizing, and risk limits. Include a version number and a one-line description of the hypothesis at the top. Write unit tests that confirm the entry and exit rules trigger exactly when they should. Run the tests and fix anything that fails. Do not run a backtest yet.
Step 4: Backtest Like You're Trying to Kill It

This is the most important step in the guide. A backtest's job is not to make a strategy look good. Its job is to find out if it's fake.
Five rules:
1. Write the prediction first. Before the backtest runs, write down what you expect: the return, the win rate, and what would count as a failure. Save it so it can't be edited later. This one habit, borrowed from clinical trials, stops you from moving the goalposts after you see the results.
2. Lock away a test period you never touch. Split your history into two parts. Build and adjust the strategy on the first part. The second part, say the most recent 20 to 30 percent of your data, stays sealed until the very end. You get to test on it once. If you test on it, tweak, and test again, it's no longer a real test.
3. Use walk-forward testing. Instead of one big backtest, train on one stretch of time, test on the next, then slide forward and repeat. A strategy that only works in one window is a coincidence.
4. Adjust for how many things you tried. If you test 500 variations, one of them will look amazing by pure luck. A measure called the Deflated Sharpe Ratio adjusts a strategy's performance for how many versions you tried. Your ledger tracks that count, so the system can apply it honestly.
5. Send in a breaker agent. A separate agent whose only job is to make the strategy fail: run it at double the trading costs, against the worst market periods for that asset, and with delayed fills. If it only survives perfect conditions, it doesn't survive.
Prompt:
Backtest strategies/[STRATEGY].py. First, save my written prediction from research/ to backtests/[STRATEGY]-prediction.md and do not change it afterward. Use only the development period of the data. The final [X]% of the data is sealed; do not touch it. Run a walk-forward test, then a stress test at 2x transaction costs and across the 5 worst historical periods for this market. Report the return, max drawdown, win rate, number of trades, and Sharpe ratio, plus the Deflated Sharpe Ratio using the total number of variations tested on this idea from memory/ledger.db. Compare the results against my saved prediction. Save everything to backtests/.
The self-improving part, done safely. When a backtest fails, it's fine to have the agent explain where the strategy loses money and propose one improvement. But every rewrite counts as another attempt in the ledger, which raises the bar the Deflated Sharpe has to clear. Cap it at 3 to 5 revisions per idea, and never touch the sealed test period during revisions. A strategy that needs 40 rewrites to pass has told you everything you need to know.
Step 5: Paper Trade With a Real Kill Switch

Only strategies that pass Step 4, including one clean run on the sealed test period, move to paper trading.
The single most important rule here: the kill switch lives in code, never in the AI's instructions. A risk limit written into a prompt is a suggestion. An AI reasoning its way through a situation can talk itself past a suggestion. Code cannot.
Here's a simple version that runs before every order:
import json
from pathlib import Path
LIMITS = json.loads(Path("config/risk.json").read_text())
def check_order(order, account):
if account["daily_loss_pct"] >= LIMITS["max_daily_loss_pct"]:
return False, "Daily loss limit hit. Trading stopped."
if account["drawdown_pct"] >= LIMITS["max_drawdown_pct"]:
return False, "Max drawdown hit. Trading stopped."
if order["value"] > account["equity"] * LIMITS["max_position_pct"]:
return False, "Order is larger than the position limit."
if account["open_positions"] >= LIMITS["max_open_positions"]:
return False, "Too many open positions."
return True, "OK"
And the limits file, config/risk.json:
{
"max_daily_loss_pct": 2.0,
"max_drawdown_pct": 8.0,
"max_position_pct": 0.10,
"max_open_positions": 5
}
These numbers are examples, not advice. Set your own.
Every order goes through check_order first. If it returns False, the order never reaches the broker, no matter what the AI argues.
Prompt:
Connect the strategies that passed backtesting to my Alpaca paper trading account using alpaca-py. Before every order, run it through check_order in risk.py using the limits in config/risk.json. If the check fails, block the order and log why. Log every decision to journal/ with the timestamp, the strategy and version, the market conditions at that moment, the decision, and the reasoning behind it. Paper account only.
Step 6: The Post-Mortem Agent
Every week, a critic agent reads the journal and tells you the truth about what happened.
Prompt (set it to run every Sunday):
You are the Critic. Read every entry in journal/ from the past 7 days. For each strategy, compare its paper trading results against its backtest and its original written prediction. Tell me: which strategies are performing worse live than in testing and by how much, what market conditions the losing trades had in common, any mistake that keeps repeating, and whether any strategy should be paused. Be blunt. Save to journal/review-[DATE].md and write every finding to memory/ledger.db.
The comparison between backtest and paper trading results is the real test. A strategy that did great in testing and falls apart live was overfit, and now you know.
Step 7: The Memory That Makes It Self-Improving

This is the part almost nobody has built.
Right now in most setups, the lesson from a losing trade lands in a log file and stays there. The next idea the AI comes up with doesn't know about it, so every cycle starts from roughly the same place.
The fix is a hypothesis ledger: a database that remembers every idea the system has ever tested, what happened, and in what kind of market. Failed ideas are the most valuable records in it. Quant researchers rarely keep them, and that's exactly why they repeat mistakes.
Prompt:
Create memory/ledger.db with SQLite. One table, hypotheses, with these columns: id, idea (one sentence), source agent, market, date tested, strategy file and version, number of variations tried, market regime during testing (trending up, trending down, choppy, or high volatility), backtest result, sealed test result, paper trading result, status (untested, failed, passed backtest only, passed paper, paused, or retired), and lessons learned. Write a small helper so any agent can search past ideas by market, regime, or status before proposing something new. Update the ledger automatically after every backtest, paper trading review, and critic report.
Now the loop actually closes:
1. Research agents check the ledger before proposing ideas, so they skip what already failed.
2. Every backtest attempt is counted, so the luck adjustment in Step 4 stays honest.
3. The critic writes its lessons back into the ledger.
4. Next week's research starts from everything the system has learned, including every failure and the exact market conditions it happened in.
That's what makes it self-improving: not a strategy that rewrites itself until it looks good, but a research system that remembers everything and stops repeating its mistakes.
When Things Go Wrong
Every backtest looks amazing. You're probably leaking future data into the past, testing on the sealed period too often, or ignoring trading costs. Check all three.
Great backtest, bad paper trading. The strategy was overfit. Mark it failed in the ledger with the reason, and look at how many variations were tried to get it to pass.
The research agents keep proposing the same ideas. They aren't checking the ledger, or the ledger isn't being updated. Confirm both.
The AI tried to place an order that broke a limit. Good, that's what the kill switch is for. Check that it was blocked. If an order ever gets through, your check isn't running before every order. Fix that before anything else.
Everything is losing money. That's the most common outcome in trading, and it's useful information. The system did its job by telling you cheaply, with fake money.
Rules to Remember
Paper trading only, until the system proves itself over weeks, not days.
Write the prediction before the test, and never edit it.
The sealed test period gets used once per idea.
Count every variation. More attempts mean a higher bar.
Cap rewrites at 3 to 5 per idea.
Kill switch in code, never in the prompt.
Every decision gets logged with its reasoning.
Failed ideas are the most valuable records you have. Keep all of them.
The goal is not one perfect strategy. It's a research process that gets a little smarter every week.
You now have the full blueprint for an AI research loop that generates ideas, tests them honestly, runs them safely with fake money, and remembers every lesson, so each cycle starts smarter than the last.
Don't want to figure this out alone? I walk members through every step inside the community. Join the Skool → skool.com/raycfu
