Skip to content
dsh-invest 文档
Esc
↑↓navigate↵open⌘Jpreview
On this page

交易执行

实现 execution 切面:执行器、账户、凭据与下单。

设置 → 交易账户:交易所连接与执行器。

设置 → 交易账户:交易所连接与执行器。

目标

让一个 venue 能出现在「设置 → 交易账户」与执行会话的执行器列表里,并能 place / cancel / 查余额与持仓。paper、testnet、live 是同一份契约的三种 mode。

要改哪些文件

src/shared/manifest.ts     给 facets.execution 填 caps / credential / adapter / create
src/host/execution.ts      create(mode, scope) → TradingExecutor
src/host/exchange.ts       ExchangeAdapter:探针、账户卡、执行器 id
src/host/credentials.ts    ProviderCredentials(读写官方凭据域)
tests/execution.spec.ts    用假 egress 钉住下单参数、错误码与幂等

契约

const credential = [{ key: 'apiKey', label: 'API Key', required: true, env: 'DEMO_API_KEY' }];

execution: {
  caps: {
    modes: ['paper', 'testnet', 'live'],
    markets: ['spot'],
    methods: ['createOrder', 'cancelOrder', 'fetchBalance', 'fetchPositions'],
  },
  credential,
  create(mode, scope) {
    const store = scope.credentialStore({
      scope: 'dsh-invest-provider-demo',
      fields: credential,
      idOf: (target) => target.connectionId ?? target.id,
    });
    return {
      id: `demo:${mode}`,
      modes: [mode],
      mode,
      market: 'spot',
      venue: 'demo',
      marketType: 'spot',
      settleCurrency: 'USDT',
      credentialFields: credential,
      credentials: store,
      async place(intent, account) {
        const values = await store.read(account);
        const egress = await scope.egress(account.connectionId);
        return postOrder(intent, values, egress);
      },
      async cancel(orderId, account) { /* ... */ },
      async balance(account) { /* ... */ },
    };
  },
}

要点:

  • id 与 mode 前缀一致;多市场用 perMarket: true + createIn(mode, market, scope),或声明 executorId(mode, market)。
  • 账户不是单独注册表:一次交易所连接派生只读账户卡与数据源卡。
  • 密钥由 provider 自己写 DSH 官方凭据域;核心不保存交易所密钥,接口不返回明文。
  • 出站走 scope.egress(connectionId);不要直接 fetch,否则会绕过代理、探针与出口 IP。
  • instrumentIdOf(ticker) 把行情面 ticker 映射到交易面 instrumentId;缺省时界面按 ticker 猜,容易把现货和永续搞混。

失败与重试

码 含义
approval-pending / human-approval 等人工批准,不要重提
bad-args 参数不合法;按消息改
mode-unsupported 执行器不支持这个 mode
not-implemented 这个方法没实现;不要重试
unknown-instrument 交易面没有这个标的
venue-error 上游业务错误;带 HTTP/业务码

下单是 mutable:用 clientOrderId 幂等。重试前先 trade.status 查这张单,不要直接再发一次。撤单/查单失败要区分「已经不存在」和「上游失败」。

paper 执行器用 ExecutorScope.paper 读湖数据;手续费、保证金、资金费入账仍在执行器里,宿主只提供数据。

验证

curl -s http://127.0.0.1:<port>/dsh-invest/trading/executors | jq '.executors[] | select(.venue == "demo")'

预期至少一条,modes 与 methods 与 manifest 一致,credentialFields 非空(如果需要 Key)。

invest_action 路径:

{"name":"executor.list","args":{}}

预期列表里出现 demo:*。再建一个 paper 执行会话并下一张 dryRun: true 的单,确认参数校验与错误码,而不是直接打上游。

Was this page helpful?