跳到正文

实时 K 线(SSE)

实时 K 线的 SSE 契约:snapshot、forming、closed 与 status 帧。

连接器通过 Server-Sent Events 向图表推送实时 K 线。一条连接只承载一个序列:数据源、代码、周期与 K 线聚合方式。任一维度变化都会关闭旧流、建立新流。

Endpoint

GET /api/v1/market-data/sources/{sourceId}/stream?symbol=…&period=…&barAggregation=…&instrumentId=…
Accept: text/event-stream
查询参数必填
symbol是
period是
barAggregation是
instrumentId—

instrumentId 是已解析品种的稳定 id,仅在已知时发送,用于区分在两个交易所同名的裸代码。

帧

每个 SSE data: 行是一个 JSON 帧。注释行(: 心跳)和空数据会被忽略。

type字段含义
snapshotsymbol: string period: string bars: LiveBar[]序列尾部全量。首帧和重连后发送;图表整批写入,并清空暂存的收线。
formingsymbol: string period: string bar: LiveBar正在形成的 K 线。与暂存的收线合并为一次原子写入。
closedsymbol: string period: string bar: LiveBar某根 K 线的终值。暂存到下一根 forming 到达(或断流)时一起写入。
statussymbol: string period: string status: string detail?: string该订阅的连接器状态,可附 detail,不修改 K 线。

LiveBar

字段类型必填
timestampnumber是
opennumber是
highnumber是
lownumber是
closenumber是
volumenumber—
turnovernumber—

timestamp 为 UTC 毫秒。缺省的 volume 与 turnover 写为 0。

data: {"type":"snapshot","symbol":"600519","period":"1min","bars":[…]}

data: {"type":"forming","symbol":"600519","period":"1min","bar":{"timestamp":…,"open":…,"high":…,"low":…,"close":…}}

data: {"type":"closed","symbol":"600519","period":"1min","bar":{…}}

连接状态

流会报告 connecting、connected、disconnected。浏览器的 EventSource 会自动重连;连接器可凭 Last-Event-ID 补发错过的帧。

本页内容