Skip to main content
這份指南是給第三方開發者的。目標是讓你可以用自己的 AI agent,透過 Traseq 的 public API key,完成 validate -> create -> finalize -> backtest 整條策略流程。

建議的 authoring contract

建議使用以下其中一種 payload:
  • signalGraph 最適合 AI authoring。graph 比 AST 更高階,也更容易依 validation feedback 修正。
  • strategyAst 只在你的 agent 已經能直接產 canonical payload 時使用。
不要把 token lists 當成主要 write contract。它們目前比較像 editor-oriented provenance / read-time compatibility data。

最小 scopes

若你的 agent 需要自行驗證、寫入策略、並執行回測,建議最小 scopes 是:
  • workspace_read
  • strategies_write
  • backtests_write
  • backtests_read
常見補充 scopes:
  • strategies_read 當 agent 需要讀取既有策略與版本時很有用。
  • system_strategies_read 當 agent 需要從內建模板起手時很有用。

建議呼叫順序

  1. GET /public/v1 先讀 manifest,確認目前 API workflow contract。
  2. GET /public/v1/workspace 讀 workspace identity、API key scopes、subscription tier。
  3. GET /public/v1/capabilities 在 authoring 前先拿可用 indicator、node inputs、bindings、operator、tier limits 與 validation issue shape。
  4. POST /public/v1/strategies/validate 驗證草稿 payload,直到 valid === true
  5. POST /public/v1/strategies 建立 draft strategy。
  6. POST /public/v1/strategies/:id/versions/finalize 把版本升成 ready
  7. POST /public/v1/backtests 建立回測。
  8. GET /public/v1/backtests/:id 輪詢直到回測進入終態。

Machine-readable capabilities

外部 agent 應在寫策略前先呼叫 GET /public/v1/capabilities 這個回應會列出:
  • 可用的 indicators
  • 可用的 signalGraph.nodes
  • 可用的 signalGraph.bindings
  • 可用的 operators
  • 可用 enum sets,例如 timeframesmarketFieldspatterns
  • tier-aware 的 limits
  • validation issue shape,包括 codepathmessagesuggestion
外部 agent 應把這個 endpoint 視為 authoring source of truth,而不是靠 prompt 自己猜 node shape、indicator params 或 operator。

結構化 validation issues

POST /public/v1/strategies/validate 現在會回傳 machine-readable issues。token、 AST、graph 驗證問題至少包含:
  • code
  • path
  • message
  • suggestion
範例:
建議修復 loop:
  1. 產生 signalGraphstrategyAst
  2. validate
  3. severity 分類問題
  4. 使用 codepathsuggestion 修 payload
  5. 重新 validate,直到 blocking issues 消失

cURL 範例

可直接複製的資產

repo 內已附上兩個可直接複製的資產:
  • docs/reference/traseq-agent-tool-schema.json 適合 tool-enabled agent 的薄工具層 schema。
  • docs/reference/traseq-agent-example.ts 最小可用的 TypeScript client,加上 validate-repair loop helper。
這個 repo 內 SDK 建議對外 package 名稱使用 @traseq/agent

TypeScript SDK 範例

若你要完整可複製版本,直接使用 docs/reference/traseq-agent-example.ts。 核心流程應該長這樣:
真正重要的不是 wrapper class 本身,而是 SDK 強制維持這個順序:
  1. 讀 workspace 與 capabilities
  2. 產生 signalGraph
  3. validate
  4. repair
  5. create
  6. finalize
  7. backtest

Agent tool schema

建議替 agent 包一層薄的 tool interface,而不是讓 model 自由拼 HTTP。完整 JSON asset 可直接使用 docs/reference/traseq-agent-tool-schema.json 建議工具:
  • get_manifest
  • get_workspace_context
  • get_capabilities
  • validate_strategy
  • create_strategy
  • finalize_strategy_version
  • run_backtest
  • get_backtest
建議的 validate_strategy input shape:

建議 system prompt

system prompt 應強制 model 尊重 capability contract 與 validate-repair loop。

Repair loop 模板

不要每次都要求 model 重新生成整份策略。建議使用 deterministic repair loop。

實務建議

  • 第一版 graph 先做小。單一 trigger 加單一 filter 比大而複雜的 graph 更容易修。
  • GET /public/v1/capabilities 可以依 workspace tier 快取,但 workspace 或 subscription 改變時要刷新。
  • 修復策略時,優先根據 issue.code 寫 deterministic repair rule,不要每次都 讓 model 重新解讀同一段錯誤文案。
  • 第一輪先只處理 blocking issues;等 payload 有效後,再決定要不要處理 warnings。