trader-cloud-backtest

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cloud backtest / train (neural-trader on a Managed Agent)

云端回测/训练(基于Managed Agent的neural-trader)

Dispatch a heavy
neural-trader
job to an Anthropic Claude Managed Agent (cloud container) instead of running it locally. See project ADR-117 (recipe + cost rules) and ADR-115 (the
managed_agent_*
runtime).
大型
neural-trader
任务分发至Anthropic Claude Managed Agent(云端容器)运行,而非本地执行。详情请参考项目ADR-117(方案及成本规则)和ADR-115(
managed_agent_*
运行时)。

When to use this vs
trader-backtest
(local)

何时使用本方案 vs
trader-backtest
(本地版)

JobRuntime
Quick sanity check; one short backtest (< ~1 min)local — use the
trader-backtest
skill
Multi-year walk-forward, big Monte-Carlo count, parameter sweep over a grid, or model training (LSTM/Transformer/N-BEATS)cloud — this skill
Prereq:
ANTHROPIC_API_KEY
(or
CLAUDE_API_KEY
) + Managed Agents beta access. If
managed_agent_*
returns "needs ANTHROPIC_API_KEY", fall back to the local
trader-backtest
skill.
任务类型运行环境
快速sanity check;单次短时间回测(<约1分钟)本地 — 使用
trader-backtest
技能
多年滚动回测、大规模蒙特卡洛模拟次数、基于网格的参数寻优,或模型训练(LSTM/Transformer/N-BEATS)云端 — 本技能
前置条件:
ANTHROPIC_API_KEY
(或
CLAUDE_API_KEY
)+ Managed Agents测试版访问权限。若
managed_agent_*
返回"needs ANTHROPIC_API_KEY",则fallback到本地
trader-backtest
技能。

Steps

步骤

  1. Estimate first. From the job size, print an estimated cost (≈ container-minutes × rate + tokens) — a long sweep is a deliberate choice, not a default.
  2. Provision (or reuse) the container — install neural-trader at container start so the agent doesn't reinstall mid-run:
    managed_agent_create({
      name: "nt-cloud",
      model: "claude-haiku-4-5-20251001",            // orchestration only — the compute is the Rust engine, not the LM (ADR-026)
      system: "You operate the `neural-trader` CLI in this container. Run exactly the commands asked, report the metrics, write requested artifacts, then stop.",
      networking: "unrestricted",                     // or "restricted" pinned to your data host
      packages: { npm: ["neural-trader"] },           // add apt:["build-essential"] ONLY if there's no prebuilt NAPI binary for the arch (neural-trader ships prebuilds → usually omit)
      initScript: "npm install -g --ignore-scripts neural-trader >/dev/null 2>&1 || npx -y neural-trader --version >/dev/null 2>&1 || true"
    })
    → { sessionId, agentId, environmentId }
    For a sweep: create the environment once, run all configs in one
    managed_agent_prompt
    (one container), not N sessions.
  3. Pre-flight cheap. Before a 1000-path / multi-year run, do a tiny smoke first (1 MC path, ~3 months) — catches a bad strategy name / symbol in seconds:
    managed_agent_prompt({ sessionId, message: "Run `npx neural-trader --backtest --strategy <name> --symbol <TICKER> --period <last 3 months> --mc-paths 1`. Just confirm it ran and report the Sharpe. Then stop.", maxWaitMs: 60000 })
    If that fails, fix the args before the real run (and
    managed_agent_terminate
    ).
  4. Run the real job:
    managed_agent_prompt({
      sessionId,
      message: "Run `npx neural-trader --backtest --strategy <name> --symbol <TICKER> --period <range> --walk-forward --mc-paths <N>` (for training: `npx neural-trader --train --model <lstm|transformer|nbeats> --symbol <TICKER> --period <range>`; for a sweep: loop the configs and run each). Report: total return, annualized return, Sharpe, Sortino, max drawdown, win rate, profit factor, # trades, 95% CVaR. Write the equity curve to /tmp/equity.csv and the trade log to /tmp/trades.csv. Then stop.",
      maxWaitMs: <generous — minutes>
    })
    → { finished, status, stopReason, assistantText (the metrics), toolUses }
    If
    finished:false
    , follow up with
    managed_agent_events({ sessionId })
    until idle.
  5. Pull artifacts (if needed):
    managed_agent_prompt({ sessionId, message: "cat /tmp/equity.csv" })
    or
    managed_agent_events
    and read the tool_result.
  6. Ingest locally:
    • memory_store({ key: "backtest-<strategy>-<ts>", value: <metrics+params JSON>, namespace: "trading-backtests" })
    • If Sharpe > 1.5:
      agentdb_pattern-store({ pattern: "profitable-<strategy-type>", data: "<params + results>" })
    • Record the run's container time + token cost to the
      cost-tracking
      namespace (per ADR-117 — cloud sessions bill until terminated).
  7. Terminate immediately — results in hand:
    managed_agent_terminate({ sessionId, environmentId })   → { sessionDeleted: true, environmentDeleted: true }
    Never leave an idle billing container. (
    ruflo doctor
    / GC catches orphans — #1931.)
  1. 先估算成本。根据任务规模,打印预估成本(≈容器分钟数 × 费率 + 令牌消耗)——长周期参数寻优是主动选择,而非默认选项。
  2. 创建(或复用)容器——在容器启动时安装neural-trader,避免代理在运行中途重新安装:
    managed_agent_create({
      name: "nt-cloud",
      model: "claude-haiku-4-5-20251001",            // 仅用于编排——计算由Rust引擎执行,而非大语言模型(ADR-026)
      system: "You operate the `neural-trader` CLI in this container. Run exactly the commands asked, report the metrics, write requested artifacts, then stop.",
      networking: "unrestricted",                     // 或设置为"restricted"并固定到你的数据主机
      packages: { npm: ["neural-trader"] },           // 仅当对应架构没有预构建NAPI二进制文件时才添加apt:["build-essential"](neural-trader已提供预构建版本→通常无需添加)
      initScript: "npm install -g --ignore-scripts neural-trader >/dev/null 2>&1 || npx -y neural-trader --version >/dev/null 2>&1 || true"
    })
    → { sessionId, agentId, environmentId }
    对于参数寻优:只需创建一次环境,在同一个
    managed_agent_prompt
    (同一个容器)中运行所有配置,而非N个会话。
  3. 低成本预校验。在运行1000路径/多年任务前,先执行一次小型冒烟测试(1次蒙特卡洛路径,约3个月)——可在数秒内发现错误的策略名称/交易代码:
    managed_agent_prompt({ sessionId, message: "Run `npx neural-trader --backtest --strategy <name> --symbol <TICKER> --period <last 3 months> --mc-paths 1`. Just confirm it ran and report the Sharpe. Then stop.", maxWaitMs: 60000 })
    如果测试失败,在正式运行前修复参数(并执行
    managed_agent_terminate
    终止容器)。
  4. 运行正式任务
    managed_agent_prompt({
      sessionId,
      message: "Run `npx neural-trader --backtest --strategy <name> --symbol <TICKER> --period <range> --walk-forward --mc-paths <N>`(训练任务使用:`npx neural-trader --train --model <lstm|transformer|nbeats> --symbol <TICKER> --period <range>`;参数寻优任务使用:循环遍历所有配置并逐一运行)。需报告:总收益、年化收益、夏普比率(Sharpe)、索提诺比率(Sortino)、最大回撤、胜率、盈利因子、交易次数、95%条件风险价值(CVaR)。将权益曲线写入/tmp/equity.csv,交易日志写入/tmp/trades.csv。完成后停止。",
      maxWaitMs: <充足时长 — 以分钟为单位>
    })
    → { finished, status, stopReason, assistantText (指标结果), toolUses }
    finished:false
    ,后续执行
    managed_agent_events({ sessionId })
    直到容器处于空闲状态。
  5. 拉取产物(如需):执行
    managed_agent_prompt({ sessionId, message: "cat /tmp/equity.csv" })
    或通过
    managed_agent_events
    读取tool_result。
  6. 本地存储
    • memory_store({ key: "backtest-<strategy>-<ts>", value: <指标+参数JSON>, namespace: "trading-backtests" })
    • 若夏普比率>1.5:
      agentdb_pattern-store({ pattern: "profitable-<strategy-type>", data: "<params + results>" })
    • 将本次运行的容器时长+令牌消耗记录到
      cost-tracking
      命名空间(遵循ADR-117——云端会话在终止前持续计费)。
  7. 立即终止容器——获取结果后:
    managed_agent_terminate({ sessionId, environmentId })   → { sessionDeleted: true, environmentDeleted: true }
    切勿让计费容器处于空闲状态。(
    ruflo doctor
    /垃圾回收机制会清理孤立容器——#1931。)

Cost rules (don't skip)

成本规则(请勿跳过)

  • Install once (
    initScript
    ), reuse the environment, batch sweeps into one prompt, pre-flight cheap, terminate eagerly, use Haiku/Sonnet for the agent loop, estimate before kicking off. (ADR-117 §"Cost optimization".)
  • A cloud backtest that runs for an hour costs an hour of container time + the agent-loop tokens. Be deliberate.
  • 仅安装一次(通过
    initScript
    ),复用环境,将参数寻优任务批量放入同一个prompt,执行低成本预校验,及时终止容器,使用Haiku/Sonnet作为代理循环模型,启动前先估算成本。(ADR-117 §"成本优化"章节)
  • 运行1小时的云端回测将产生1小时的容器时长费用+代理循环令牌消耗。请谨慎操作。

Quick example

快速示例

managed_agent_create  { "name":"nt-cloud", "model":"claude-haiku-4-5-20251001", "packages":{"npm":["neural-trader"]}, "initScript":"npm install -g --ignore-scripts neural-trader >/dev/null 2>&1 || true" }
  → { sessionId:"sesn_…", environmentId:"env_…" }
managed_agent_prompt   { "sessionId":"sesn_…", "message":"Run `npx neural-trader --backtest --strategy multi-indicator --symbol SPY --period 2020-2024 --walk-forward --mc-paths 1000`. Report Sharpe/Sortino/max-DD/win-rate/CVaR; write /tmp/equity.csv. Then stop.", "maxWaitMs":600000 }
  → { finished:true, status:"idle", assistantText:"<metrics>", toolUses:[{bash:"npx neural-trader --backtest …"}] }
managed_agent_create  { "name":"nt-cloud", "model":"claude-haiku-4-5-20251001", "packages":{"npm":["neural-trader"]}, "initScript":"npm install -g --ignore-scripts neural-trader >/dev/null 2>&1 || true" }
  → { sessionId:"sesn_…", environmentId:"env_…" }
managed_agent_prompt   { "sessionId":"sesn_…", "message":"Run `npx neural-trader --backtest --strategy multi-indicator --symbol SPY --period 2020-2024 --walk-forward --mc-paths 1000`. Report Sharpe/Sortino/max-DD/win-rate/CVaR; write /tmp/equity.csv. Then stop.", "maxWaitMs":600000 }
  → { finished:true, status:"idle", assistantText:"<metrics>", toolUses:[{bash:"npx neural-trader --backtest …"}] }

… memory_store the metrics, agentdb_pattern-store if Sharpe>1.5, record cost …

… 将指标存入memory_store,若夏普比率>1.5则存入agentdb_pattern-store,记录成本 …

managed_agent_terminate { "sessionId":"sesn_…", "environmentId":"env_…" }
undefined
managed_agent_terminate { "sessionId":"sesn_…", "environmentId":"env_…" }
undefined