Glean 拾遗
日刊 /2026-06-24 / 如何用循环工程构建自我进化的量化交易系统

如何用循环工程构建自我进化的量化交易系统

原文 x.com 收录 2026-06-24 06:00 阅读 13 min
AI 解读

本文由一位后端开发兼量化交易系统实践者撰写,核心论点是告别逐次提示AI的工作模式,转而构建自运行的循环(Loop)。作者拆解了生产级循环的六个必要组件:自动化触发、技能文件(SKILL.md)、状态文件(STATE.md)、验证器(独立Agent)、Git worktrees 隔离、以及基于 MCP 的连接器。进而将量化交易的五个阶段(数据摄入、信号生成、验证、执行、风险监控)各自封装为子循环,并引入‘经验写入技能文件’的机制,实现系统自我进化。适合对AI工程、量化系统架构感兴趣的工程师。

原文 13 分钟
原文 x.com ↗
§ 1

I will break down exactly how to build the loops that run an entire quant trading system on their own.

Let's get straight to it.

Bookmark This - I'm Roan, a backend developer working on system design, HFT-style execution, and quantitative trading systems. My work focuses on how prediction markets actually behave under load. For any suggestions, thoughtful collaborations, partnerships DMs are open.

One thing I am starting from today.

If you are building a quant system, about to start or even just thinking about it, DM me what you are working on or just reply under this article and I will reach out to you (even you can simply give me the screenshot of your current architecture).

I will personally walk through the first 20 setups show you the gap between what you have and a system that actually prints alpha.

我将详细拆解如何构建能够自主运行整个量化交易系统的循环。

直接进入正题。

收藏本文 - 我是 Roan,一名专注于系统设计、HFT 风格执行和量化交易系统的后端开发者。我的工作核心是研究预测市场在负载下的真实表现。欢迎私信提出建议、进行深度合作或建立伙伴关系。

从今天开始,我有一件事要宣布。

如果你正在构建量化系统,即将开始,甚至只是在思考这件事,请私信告诉我你正在做什么,或直接在本文下方回复,我会联系你(你甚至可以只给我发一张当前架构的截图)。

我将亲自审阅前 20 个配置,向你展示你现有系统与一个真正能打印 Alpha 的系统之间的差距。

§ 2

Most quants still prompt Claude. They type. They wait. They read the output. They type again.

The smartest builders on the planet have stopped doing that.

They write loops. The loops prompt Claude. The loops verify the output. The loops decide what happens next. The loops keep running after the laptop is closed.

Boris Cherny, the head of Claude Code at Anthropic, said it plainly two weeks ago. "I don't prompt Claude anymore. I have loops running that prompt Claude and figuring out what to do. My job is to write loops." That single sentence reframed how every serious AI engineer on earth thinks about building. And it pairs perfectly with quant trading.

Most retail quants will read this and say it does not apply to them because they are too small. They are wrong. The smaller your capital, the more this matters. A self running loop is the only way a solo builder ever closes the gap with a fund running 100 PhDs.

大多数量化交易者仍然在手动提示 Claude。他们输入命令,等待,阅读输出,然后再次输入。

全球最聪明的构建者已经不再这样做了。

他们编写循环。循环负责提示 Claude,循环验证输出,循环决定下一步做什么。即使合上笔记本电脑,循环仍在持续运行。

Anthropic 公司 Claude Code 的负责人 Boris Cherny 两周前说得非常直白:“我不再手动提示 Claude 了。我在运行循环,这些循环会提示 Claude 并决定下一步行动。我的工作就是编写循环。” 这一句话重新定义了地球上每一位严肃 AI 工程师对构建系统的思考方式。而它也与量化交易完美契合。

大多数个人量化交易者读到此处可能会认为这对自己不适用,因为他们的规模太小。他们错了。资金规模越小,这一点反而越重要。一个自主运行的循环,是独立构建者缩小与拥有 100 名博士的基金之间差距的唯一途径。

§ 3

Because quant trading is already a loop. Pull data. Generate signals. Backtest. Execute. Monitor risk. Repeat.

Every fund on Wall Street runs that exact cycle. Renaissance has been running it since 1988. Citadel runs it with teams of engineers monitoring every stage. Two Sigma, Jane Street, all of them.

The only difference is they need hundreds of humans sitting inside the loop. You do not.

I have already built this loop for myself. It pulls market data on schedule. It runs alpha research. It verifies every signal through a separate agent. It executes only what passes verification. It writes every lesson back to memory.

This article is everything I have learned about loop engineering and how to wire it into a complete autonomous trading system.

By the end of this you will know:

The exact difference between prompting an agent and engineering a loop.

The six pieces that run every working loop in production.

How to wire those six pieces into a self improving quant trading system from scratch.

Let's get into it.

因为量化交易本身就是一个循环。拉取数据,生成信号,回测,执行,监控风险,然后重复。

华尔街的每家基金都在运行这个精确的循环。文艺复兴自 1988 年以来就一直如此。Citadel 有工程师团队监控每个阶段。Two Sigma、Jane Street 等等,无一例外。

唯一的区别是,它们需要数百人坐在循环内部。而你不需要。

我已经为自己构建了这个循环。它按计划拉取市场数据,进行 Alpha 研究,通过单独智能体验证每个信号,只执行通过验证的信号,并将每一次教训写回记忆。

本文涵盖了我所学到的关于循环工程的一切,以及如何将其接入一个完整的自主交易系统。

读完全文,你将了解:

手动提示智能体与设计循环之间的确切区别。

驱动生产中每个有效循环的六个组成部分。

如何从零开始将这六个部分组合成一个自我改进的量化交易系统。

让我们开始吧。

§ 4

Part 1: The Difference Between Prompting And Loop Engineering

For the last two years, working with AI looked like this.

You typed a prompt. You read what came back. You typed the next prompt based on what you saw.

You were the loop.

The agent was a tool. You held it the entire time. Every move was you sitting at your keyboard deciding what to do next.

Loop engineering ends that.

You stop being the thing inside the loop. You become the architect who designs it.

A loop is a recursive goal. You define a purpose. The agent iterates against it. The loop keeps running until a real stopping condition is met.

The agent forgets between runs. The loop does not.

That single fact is the entire architecture.

This is what Boris meant when he said his job is to write loops. He stopped typing instructions one at a time. He built systems that send the instructions for him, read the results, and decide what happens next.

For coding, this changes how software ships.

For trading, this changes everything.

Because no quant has ever made money by typing one prompt and walking away. The edge comes from running the same cycle thousands of times, getting one percent better every iteration, and never sleeping.

That is exactly what a loop does.

If you are still typing prompts into Claude one trade at a time, you are doing what Boris stopped doing two years ago. The leverage point has moved one floor up. You are not writing better prompts anymore. You are writing the system that writes the prompts.

第一部分:提示与循环工程之间的差异

过去两年,人们使用 AI 的方式是这样的:

你输入一个提示,阅读返回的结果,然后根据看到的内容再输入下一个提示。

你本身就是那个循环。

智能体只是一个工具,你全程掌控。每一步都是你坐在键盘前,决定下一步做什么。

循环工程终结了这种模式。

你不再是循环内部的操作者,而是设计循环的架构师。

一个循环就是一个递归目标。你定义一个目的,智能体围绕它进行迭代。循环持续运行,直到满足一个真正的停止条件。

智能体会在每次运行之间遗忘,但循环不会。

这一事实本身就是整个架构的核心。

这正是 Boris 说他的工作是编写循环时的意思。他不再一次次手动输入指令,而是构建了系统,由系统替他发送指令、读取结果并决定下一步。

对于编码来说,这改变了软件发布的方式。

对于交易来说,这改变了一切。

因为没有哪个量化交易者是通过输入一次提示就离开并赚到钱的。优势来自于将同一个循环运行数千次,每次迭代进步百分之一,而且永不休息。

而这正是循环做的事情。

如果你仍在每次交易时手动向 Claude 输入提示,那你正在做 Boris 两年前就不再做的工作。杠杆点已经上移了一层。你不再需要编写更好的提示,你需要编写生成提示的系统。

§ 5

Part 2: The Six Pieces That Run Every Working Loop

A working loop is built out of six parts. Miss one and the loop breaks quietly.

  1. The automation.

This is the heartbeat. A cron schedule, a webhook, a /loop command, or a hook inside Claude Code that fires without you typing.

There are two flavors worth knowing. /loop reruns on a cadence regardless of state. /goal keeps going until a verifiable condition you wrote is actually true, with a separate small model grading whether the work is done.

In trading, /loop is your data pull every minute. /goal is "keep iterating on this signal until the backtest Sharpe is above 1.5."

第二部分:驱动每个有效循环的六个组成部分

一个有效的循环由六个部分组成。缺少任何一个,循环都会悄然失效。

  1. 自动化

这是循环的心跳。它可以是一个 cron 定时任务、一个 webhook、一个 /loop 命令,或者 Claude Code 内部一个无需你输入即可触发的钩子。

有两种值得了解的变体。/loop 会按固定节奏重新运行,无论当前状态如何。/goal 会持续运行,直到你编写的可验证条件真正成立,并由一个独立的小模型来评判工作是否完成。

在交易中,/loop 是你的每分钟数据拉取任务。/goal 则是“持续迭代这个信号,直到回测的 Sharpe 比率高于 1.5。”

§ 6
  1. The skill.

A skill is a procedure manual the agent reads instead of being told from scratch every session.

It lives in a SKILL.md file. It holds your conventions, your rules, your "we don't do it like this because of that one incident."

Without skills, every loop run starts from zero. With skills, intent compounds.

  1. The state file.

A markdown file. Usually called STATE.md or PROGRESS.md.

It survives between runs. The agent forgets. The file does not.

The agent reads it at the start of every run. It writes back what happened at the end.

This sounds too dumb to matter. It is actually the spine of every working loop.

  1. 技能

技能是一份操作手册,智能体每次运行时阅读它,而非每次从头被告知怎么做。

它存储在一个 SKILL.md 文件中,包含你的惯例、规则,以及“因为某次事故我们不再这样操作”的教训。

没有技能文件,每次循环运行都从零开始。有了技能文件,意图会不断累积。

  1. 状态文件

一个 Markdown 文件,通常命名为 STATE.mdPROGRESS.md

它在多次运行之间持续存在。智能体会遗忘,但文件不会。

智能体在每次运行开始时读取它,并在结束时写入发生了什么。

这听起来简单得不像回事,但它实际上是每个有效循环的脊梁。

§ 7
  1. The verifier.

The agent that wrote the code is the worst possible judge of whether the code is correct.

Apply this to trading. The agent that generated the signal is the worst possible judge of whether the signal is real alpha or noise.

You need a separate agent, with different instructions, ideally a different model, whose only job is to verify the work.

This is the maker checker pattern. Every prop shop on Wall Street is structured this way internally. At Jane Street, the trader who proposes a trade does not approve the trade. At Citadel, the researcher who builds the model does not validate the model.

  1. The worktrees.

The moment you run more than one agent against the same files, they start colliding.

Git worktrees give each agent its own isolated working directory pointed at its own branch.

In trading, this lets you run signal research, backtesting, and risk monitoring in parallel without ever stepping on each other.

  1. 验证器

编写代码的智能体是判断代码是否正确的最大障碍。

将此应用到交易中:生成信号的智能体是判断该信号是真实 Alpha 还是噪声的最大障碍。

你需要一个单独的智能体,使用不同的指令——理想情况下是使用不同的模型——它的唯一工作就是验证成果。

这就是“制造者-检查者”模式。华尔街的每一家自营交易公司内部都是这样结构化的。在 Jane Street,提出交易的交易员不会批准该交易。在 Citadel,构建模型的研究员不会验证该模型。

  1. 工作树

一旦你对同一文件运行超过一个智能体,它们就会开始发生冲突。

Git 工作树为每个智能体提供各自独立的、指向自己分支的隔离工作目录。

在交易中,这让你可以并行运行信号研究、回测和风险监控,而互不干扰。

§ 8
  1. The connectors.

A loop that can only read files is a tiny loop.

Connectors built on the Model Context Protocol let the loop hit a broker API, query a database, post to Slack, send orders to the exchange.

This is the difference between a loop that suggests trades and a loop that actually places them.

These six pieces are universal. They show up in Claude Code. They show up in Codex. They show up in every working agentic system on the planet.

Now let me show you how to wire them together into a complete trading system.

  1. 连接器

一个只能读取文件的循环,是微不足道的循环。

基于模型上下文协议(Model Context Protocol)构建的连接器,让循环能够调用券商 API、查询数据库、向 Slack 发送消息、向交易所下达订单。

这就是“建议交易”的循环与“实际执行交易”的循环之间的区别。

这六个组件是通用的。它们出现在 Claude Code、Codex 以及地球上每一个有效的智能体系统中。

现在,让我向你展示如何将它们组合成一个完整的交易系统。

§ 9

Part 3: How To Build A Self-Improving Quant Trading Loop

The quant trading loop has five stages. Each stage is its own sub loop, with its own skill, its own state file, and its own verifier.

Stage one. Data ingestion.

An automation fires every minute, every hour, or every day depending on the asset class.

@loop(interval="1h")
def ingest_data():
    data = fetch_market_data(symbols=universe, lookback="30d")
    state.write("latest_data.parquet", data)

The data goes into a shared state file the next stage reads.

第三部分:如何构建一个自我改进的量化交易循环

量化交易循环包含五个阶段。每个阶段都是一个独立的子循环,拥有自己的技能文件、状态文件和验证器。

第一阶段:数据摄取

一个自动化任务根据资产类别按每分钟、每小时或每天触发。

@loop(interval="1h")
def ingest_data():
    data = fetch_market_data(symbols=universe, lookback="30d")
    state.write("latest_data.parquet", data)

数据被写入一个共享状态文件,供下一阶段读取。

§ 10

Stage two. Signal generation.

This is where the alpha research happens.

@loop(trigger="data_updated")
def generate_signal():
    data = state.read("latest_data.parquet")
    signal = claude.run_skill("alpha_research", data)
    state.write("pending_signal.json", signal)

The signal generation agent reads from a SKILL.md file that holds your alpha research rules.

# alpha_research_skill.md

## Goal
Generate signals using linear regression on the last 30 days
of price and volume data.

## Rules
- Sharpe ratio must be above 1.5 in 3 of the last 5 backtests
- Position size limited to 2 percent of capital per signal
- Skip signals on FOMC announcement days
- Skip signals 48 hours before earnings releases

## Lessons learned
- 2026-02-14: Lost 4.2 percent during earnings week.
  New rule: skip any signal 48 hours before earnings.
- 2026-03-08: Sector exposure breach caused 6 percent drawdown.
  New rule: cap sector exposure at 30 percent.
- 2026-04-22: Momentum signal blew up on FOMC day.
  New rule: kill all momentum signals on FOMC days.

The skill grows over time. Every loss writes a new lesson back. Every lesson becomes a new rule for the next run.

This is what makes the system self improving.

第二阶段:信号生成

这就是 Alpha 研究发生的阶段。

@loop(trigger="data_updated")
def generate_signal():
    data = state.read("latest_data.parquet")
    signal = claude.run_skill("alpha_research", data)
    state.write("pending_signal.json", signal)

信号生成智能体读取一个包含 Alpha 研究规则的 SKILL.md 文件。

# alpha_research_skill.md

## 目标
利用过去 30 天的价格和成交量数据进行线性回归,生成信号。

## 规则
- 最近 5 次回测中,必须有 3 次的 Sharpe 比率高于 1.5
- 每个信号的头寸规模限制为资金的 2%
- 跳过 FOMC 公告日的信号
- 跳过财报发布前 48 小时的信号

## 经验教训
- 2026-02-14:财报周亏损 4.2%。
  新规则:跳过财报发布前 48 小时内的任何信号。
- 2026-03-08:行业敞口超限导致 6% 回撤。
  新规则:将行业敞口上限设为 30%。
- 2026-04-22:动量信号在 FOMC 日爆仓。
  新规则:在 FOMC 日终止所有动量信号。

技能文件会随着时间的推移不断增长。每一次亏损都会写入一条新教训;每一条教训都成为下一次运行的新规则。

这就是系统实现自我改进的方式。

§ 11

Stage three. Verification.

The signal goes to a completely separate agent. Different model. Different instructions. No exposure to how the original signal was reasoned.

@checker
def verify_signal(signal):
    result = claude.invoke(
        skill="backtest_verification_skill.md",
        signal=signal,
        rules=[
            "Sharpe ratio above 1.5",
            "Max drawdown below 10 percent",
            "Newey-West t-stat above 2.0",
            "Out of sample period at least 2 years"
        ]
    )
    return result.verdict

If verification fails, the signal is killed. If it passes, it moves to execution.

The verifier never sees what the maker reasoned. That separation is the entire edge.

You can also use a stronger model for the checker than the maker. Claude Opus for verification, Claude Sonnet for generation. Different model architectures catch different kinds of errors. This is the same logic ensemble methods use in machine learning.

Stage four. Execution.

Only verified signals reach this stage.

@auto_mode
def execute(signal):
    if verify_signal(signal):
        broker.send_orders(signal, max_position=0.02)
        state.write("active_trades", signal)

The MCP connector handles the broker API. The loop never asks for permission. Auto mode lets it run hands off.

第三阶段:验证

信号被发送给一个完全独立的智能体。使用不同的模型、不同的指令。该智能体不了解原始信号是如何推理出来的。

@checker
def verify_signal(signal):
    result = claude.invoke(
        skill="backtest_verification_skill.md",
        signal=signal,
        rules=[
            "Sharpe 比率高于 1.5",
            "最大回撤低于 10%",
            "Newey-West t 统计量高于 2.0",
            "样本外周期至少 2 年"
        ]
    )
    return result.verdict

如果验证失败,信号被丢弃。如果通过,则进入执行阶段。

验证智能体永远不会看到制造智能体的推理过程。这种分离本身就是全部优势。

你还可以对检查者使用比制造者更强的模型——例如 Claude Opus 用于验证,Claude Sonnet 用于生成。不同的模型架构能捕获不同类型的错误。这与机器学习中使用集成方法的逻辑相同。

第四阶段:执行

只有通过验证的信号才能进入此阶段。

@auto_mode
def execute(signal):
    if verify_signal(signal):
        broker.send_orders(signal, max_position=0.02)
        state.write("active_trades", signal)

MCP 连接器负责处理券商 API。循环从不请求许可。自动模式使其自主运行。

§ 12

Stage five. Risk monitoring.

Running in a parallel worktree the entire time.

@loop(interval="1m")
def monitor_risk():
    positions = broker.get_positions()
    if drawdown(positions) > 0.05:
        broker.close_all()
        state.append("STATE.md", "Drawdown trigger hit. All positions closed.")

This is the kill switch. It enforces rules without negotiation.

Together these five sub loops form one self running system.

Data flows in. Signals get generated. Signals get verified. Verified signals get executed. Risk gets monitored. Lessons get written back to memory.

Then it starts over.

I designed this once. I have not prompted any of these steps since.

That is loop engineering. That is what Boris meant when he said his job is to write loops.

One warning. A loop without a real stopping condition fails quietly. The agent emits a completion signal believing the half done job is finished. The loop exits. The bad trade sits open.

Your stop conditions need to be checkable by something other than the agent's own claim. "Sharpe above 1.5 over the last 30 trades." "Drawdown below 5 percent." "Test suite passes." Never "the agent says it is done."

In my game theory article I explained why every trade is a strategic move in a multi player game with imperfect information. If you missed it, you will want to read it right after this:

The loop is what lets you sit at that table forever without burning out.

第五阶段:风险监控

该阶段始终在并行的工作树中运行。

@loop(interval="1m")
def monitor_risk():
    positions = broker.get_positions()
    if drawdown(positions) > 0.05:
        broker.close_all()
        state.append("STATE.md", "Drawdown trigger hit. All positions closed.")

这是紧急开关。它无需商议即可强制执行规则。

这五个子循环共同构成了一个自主运行的系统。

数据流入,信号生成,信号验证,已验证信号执行,风险监控,教训写回记忆。

然后循环重新开始。

我只设计了一次。此后我再也没有手动提示过其中任何一个步骤。

这就是循环工程。这就是 Boris 说他的工作是编写循环时的含义。

但要警告一点:一个没有真正停止条件的循环会悄然失效。智能体发出完成信号,相信自己完成了一半的工作,循环退出,糟糕的交易便敞口运行。

你的停止条件必须能够由智能体自身主张之外的其他东西来检查。例如“过去 30 笔交易的 Sharpe 比率高于 1.5”,“回撤低于 5%”,“测试套件通过”。永远不要使用“智能体说它完成了”。

在我的博弈论文章中,我解释了为什么每一笔交易都是在不完美信息的多方游戏中的一步策略棋。如果你错过了,建议在读完本文后立即阅读:

循环让你能够永远坐在那张牌桌旁而不会精疲力竭。

§ 13

Summary

Quant trading is already a loop. Every fund on Wall Street runs it. They just need hundreds of humans sitting inside.

Loop engineering removes the humans.

Six pieces make every working loop. Automations fire the heartbeat. Skills hold the conventions. State files hold the memory. Verifiers grade the output. Worktrees isolate parallel work. Connectors give the loop hands in the real world.

Wire them around the five stage trading cycle and you have a self improving system that runs alpha research, verifies signals, executes trades, and monitors risk on its own.

The system gets smarter every cycle. Every loss writes a new lesson. Every lesson becomes a new rule. After a hundred trades, the skill file is a living document. After a thousand, it is closer to institutional knowledge than anything a single human could remember.

The funds that build this first will compound for the next decade. The ones still prompting will be left behind.

So here is the question to sit with.

If loop engineering is the next abstraction above prompting, and quant trading is the highest stakes loop in the world, are you the person still typing prompts one trade at a time, or are you the architect who designed the loop that trades for you while you sleep?

There is no wrong answer but there are very revealing ones.

总结

量化交易本身就是一个循环。华尔街的每家基金都在运行它。只是它们需要数百人坐在循环内部。

循环工程去除了这些人。

六个部分构成了每个有效的循环。自动化提供心跳,技能文件保存惯例,状态文件保存记忆,验证器评判输出,工作树隔离并行工作,连接器为循环赋予在现实世界中行动的能力。

将这六个部分围绕五个阶段的交易周期组合起来,你就得到了一个自我改进的系统,它能自主进行 Alpha 研究、验证信号、执行交易和监控风险。

系统每个周期都会变得更聪明。每一次亏损都写下一个新的教训,每个教训都成为一条新规则。经过一百笔交易,技能文件变成了一份活跃的文档。经过一千笔交易,它比任何单个人类能记住的东西都更接近机构级别的知识。

最先构建这套系统的基金将在未来十年里实现复利增长。而仍然在手动提示的那些将被抛在后面。

所以,这里有一个值得深思的问题。

如果说循环工程是提示工程的下一代抽象,而量化交易是世界上最值得冒险的循环,那么你是一个仍在每次交易时手动输入提示的人,还是一个设计了循环、让循环在你睡觉时为你交易的架构师?

没有错误的答案,但有些答案能揭示很多。

打开原文 ↗