Skip to content

自带数据算指标

POST /api/indicators/compute 让你用自己的 K 线调用引擎的 60+ 指标计算逻辑 —— 不依赖引擎的内存缓存,也不限定 venue / symbol。

适合场景:

  • 回测:数据库 / CSV 里的历史 OHLCV 想跑指标
  • 自定义数据源:对手交易所、聚合合约、你自己的 tick 重采样
  • 离线分析:某段时段想验证指标值,但不想被引擎 cache 限制

不适合:

  • 实时行情:走 POST /api/indicators 让引擎从内存 store 取,延迟更低
  • 跨周期(MTF)指标(比如 SMC 的 fvg_timeframe):这类参数需要引擎自己 prefetch 多周期数据,只能走 store 路径

30 秒上手

bash
curl -X POST 'https://api.mobiusquant.ai/api/indicators/compute' \
  -H 'authorization: Bearer mq_xxx' \
  -H 'content-type: application/json' \
  -d '{
    "klines": [
      [1735689600000, 95000.0, 95500.0, 94800.0, 95200.0, 1234.5],
      [1735693200000, 95200.0, 95800.0, 95100.0, 95650.0, 1500.2]
    ],
    "interval": "1h",
    "calc": [{"name": "rsi", "params": {"period": 14}}]
  }'

返回:

jsonc
{
  "count": 2,
  "interval": "1h",
  "klines": [[1735689600000, 95000.0, ...], ...],   // echo
  "indicators": {
    "rsi:period=14": {
      "columns": ["open_time", "rsi"],
      "data":    [[1735689600000, null], [1735693200000, null]],
      "explain": { /* knowledge: summary_focus / signals / caveats */ }
    }
  }
}

RSI 在前 14 根之前返回 null —— 这是正常的,所有"累积型"指标都需要 warmup。详见下方限制


Python 集成示例

python
import requests
import pandas as pd

# 从你自己的数据源拉历史 OHLCV
df = pd.read_csv('btc_1h_history.csv')   # 列:open_time(ms), open, high, low, close, volume

klines = df[['open_time', 'open', 'high', 'low', 'close', 'volume']].values.tolist()

resp = requests.post(
    'https://api.mobiusquant.ai/api/indicators/compute',
    headers={'Authorization': 'Bearer mq_xxx'},
    json={
        'klines':   klines,
        'interval': '1h',
        'calc': [
            {'name': 'ema', 'params': {'period': 20}, 'id': 'ema20'},
            {'name': 'ema', 'params': {'period': 200}, 'id': 'ema200'},
            {'name': 'rsi', 'params': {'period': 14}},
        ],
    },
)
result = resp.json()
print(result['indicators']['ema20']['data'][-1])   # 最新 EMA20 值
print(result['indicators']['rsi:period=14']['data'][-1])

SMC 等"复合指标"也支持

像 SMC (Smart Money Concepts) 这种非数值输出指标,返回结果除了 per-bar columns 还会附带 objects sidecar:

bash
curl -X POST 'https://api.mobiusquant.ai/api/indicators/compute?explain=false' \
  -H 'content-type: application/json' \
  -d '{
    "klines": [/* ≥500 根历史 K 线 */],
    "calc": [{"id": "smc", "name": "smc",
              "params": {"show_swing_points": true, "show_premium_discount_zones": true}}]
  }'

响应里 indicators.smc.objects 包含 swing_structures / order_blocks_swing / fair_value_gaps / premium_zone 等结构化对象,可以直接喂给 chart 渲染器或 LLM 分析。详见 SMC 指标页


请求字段

字段类型必填说明
klinesarrayOHLCV 数组,每根 K 线两种格式自动识别(见下)
calcarray指标列表,每项 {name, params, id?},跟 /api/indicators
intervalstring仅元数据,响应回显;某些 MTF 指标用不到
echo_klinesbool默认 true,响应回 echo;false 时只返指标

klines 元素的两种格式:

jsonc
// 紧凑(Binance 嵌套列表风格):[open_time_ms, open, high, low, close, volume, quote_volume?]
[1735689600000, 95000.0, 95500.0, 94800.0, 95200.0, 1234.5, 117526200.0]

// 对象(更显式)
{
  "open_time":    1735689600000,
  "open":         95000.0,
  "high":         95500.0,
  "low":          94800.0,
  "close":        95200.0,
  "volume":       1234.5,
  "quote_volume": 117526200.0     // 可选,部分指标用(VWAP / CVD 等);缺省补 0
}

响应字段

字段类型说明
countint处理的 K 线根数
intervalstring回显 request 里的 interval(传了才有)
klinesarrayecho 输入的 K 线(echo_klines=false 时省略)
indicatorsobjectkey = id 或自动生成的 name:p1=v1,...,value = 单个指标的 columns/data/objects/explain

每个 indicators.<key> 的结构跟 POST /api/indicators 完全一致:

jsonc
{
  "columns": ["open_time", "rsi"],
  "data":    [[1735689600000, null], [1735693200000, 67.4], ...],
  "objects": { /* 仅 SMC 等非数值指标 */ },
  "explain": { /* knowledge: 仅 ?explain=true 时 */ }
}

限制

限制触发
K 线数量2000 根超返 413
open_time严格单调递增的毫秒否则 400
OHLCV必须 finite(无 NaN / inf)否则 400
calc 列表非空,且 name 必须在 registry否则 400
calc 非法参数Pydantic / signature 校验422

关键:无 warmup buffer

/api/indicators 内部会自动预取 1000 根 warmup 喂给 EMA / RSI / ATR 这类"累积型"指标。/api/indicators/compute 不会 —— 它老老实实只用你给的 K 线。

所以请确保 history 够长:

指标 / 用法最少 K 线
RSI(14) 想稳定≥ 50
EMA(200) / ATR(200) 想稳定≥ 200
Bollinger / Keltner / Donchian 默认参数≥ 30
SMC 内部结构 + FVG≥ 50
SMC swing 结构 / OB / Strong-Weak / P/D zones≥ 100(默认 swing_size=50,加 buffer)
SMC 完整功能 + EQH/EQL 稳定≥ 200(ATR 收敛阈值)
生产分析建议500+

前 N 根的指标值返回 null 是正常的,你应该丢掉那段 warmup,只用尾部稳定的结果。


鉴权与速率限制

/api/indicators 同套:

  • 不带 Authorization header → 走匿名 IP 桶(低限速)
  • 带合法 Bearer mq_xxx → 走 token 桶(高限速)
  • 没 token 的话先 申请一个,免费

错误码

Code含义处理
400校验失败(非单调递增 / NaN / 未知指标 / 列数不够)detail,改请求
401带了 Authorization 但 token 无效重新申请
413klines 超 2000 根分批,或缩短窗口
422Pydantic 校验失败(缺字段 / 类型错 / 多余 key)detail.loc
429超 rate limitRetry-After,带 token 限速更高
500指标计算异常(指标自身 bug)报 issue,附 detail 里的 indicator name

相关

AI 交易的基础设施平台