[TOC]

随着数字货币交易量的急剧增长,实时数据推送的效率和稳定性变得至关重要。MEXC交易所最新推出的基于Protocol Buffers(protobuf)的WebSocket推送方案,相比传统JSON格式具有更高的传输效率和更低的延迟。本文将详细介绍如何在FMZ量化交易平台上实现对MEXC新增WebSocket protobuf接口的访问和数据处理。
Protocol Buffers是Google开发的一种语言无关、平台无关的序列化数据结构的方法。相比JSON格式,protobuf具有以下优势:
MEXC新增的WebSocket protobuf推送接口主要提供以下数据流:
https://www.mexc.com/zh-MY/api-docs/spot-v3/websocket-market-streams
Private PrivateAccountV3Api PrivateDealsV3Api PrivateOrdersV3Api
Public PublicAggreBookTickerV3Api PublicAggreDealsV3Api PublicBookTickerV3Api …
在FMZ平台上实现MEXC protobuf WebSocket接口需要以下准备工作:
以建立PublicAggreDealsV3Api频道连接为例:
function main() {
var payload = {
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}
var conn = Dial("wss://wbs-api.mexc.com/ws|payload=" + JSON.stringify(payload))
while (true) {
var ret = conn.read()
if (ret) {
Log(ret)
}
}
}
找到需要的 .proto 文件:
https://github.com/mexcdevelop // 可以在这里找到交易所的 .proto 文件
syntax = "proto3";
option java_package = "com.mxc.push.common.protobuf";
option optimize_for = SPEED;
option java_multiple_files = true;
option java_outer_classname = "PushDataV3ApiWrapperProto";
message PublicAggreDealsV3Api {
repeated PublicAggreDealsV3ApiItem deals = 1;
string eventType = 2;
}
message PublicAggreDealsV3ApiItem {
string price = 1;
string quantity = 2;
int32 tradeType = 3;
int64 time = 4;
}
message PushDataV3ApiWrapper {
string channel = 1;
oneof body {
PublicAggreDealsV3Api publicAggreDeals = 314;
}
optional string symbol = 3;
optional string symbolId = 4;
optional int64 createTime = 5;
optional int64 sendTime = 6;
}
由于FMZ平台基于JavaScript,我们需要使用protobuf.js库来处理二进制数据:
JS库加载地址:
https://cdnjs.cloudflare.com/ajax/libs/protobufjs/7.5.3/protobuf.js
WebSocket接口推送的数据为二进制数据,需要解码:
while (true) {
var ret = conn.read()
if (ret) {
const uint8arrayData = new Uint8Array(ret)
const message = PushDataV3ApiWrapper.decode(uint8arrayData)
data = PushDataV3ApiWrapper.toObject(message, {
longs: String,
enums: String,
bytes: String,
defaults: true,
arrays: true,
objects: true
})
Log("data:", data)
}
LogStatus(_D(), data)
}
let strPushDataV3ApiWrapper = `syntax = "proto3";
option java_package = "com.mxc.push.common.protobuf";
option optimize_for = SPEED;
option java_multiple_files = true;
option java_outer_classname = "PushDataV3ApiWrapperProto";
message PublicAggreDealsV3Api {
repeated PublicAggreDealsV3ApiItem deals = 1;
string eventType = 2;
}
message PublicAggreDealsV3ApiItem {
string price = 1;
string quantity = 2;
int32 tradeType = 3;
int64 time = 4;
}
message PushDataV3ApiWrapper {
string channel = 1;
oneof body {
PublicAggreDealsV3Api publicAggreDeals = 314;
}
optional string symbol = 3;
optional string symbolId = 4;
optional int64 createTime = 5;
optional int64 sendTime = 6;
}`
let code = HttpQuery("https://cdnjs.cloudflare.com/ajax/libs/protobufjs/7.5.3/protobuf.js")
let exports = {}
let module = { exports }
new Function("module", "exports", code)(module, exports)
let protobuf = module.exports
function main() {
const PushDataV3ApiWrapper = protobuf.parse(strPushDataV3ApiWrapper).root.lookupType("PushDataV3ApiWrapper")
var payload = {
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}
// proxy=socks5://x.x.x.x:xxxx
var conn = Dial("wss://wbs-api.mexc.com/ws|payload=" + JSON.stringify(payload))
var data = null
while (true) {
var ret = conn.read()
if (ret) {
const uint8arrayData = new Uint8Array(ret)
const message = PushDataV3ApiWrapper.decode(uint8arrayData)
data = PushDataV3ApiWrapper.toObject(message, {
longs: String,
enums: String,
bytes: String,
defaults: true,
arrays: true,
objects: true
})
Log("data:", data)
}
LogStatus(_D(), data)
}
}

可以看到,接收到了接口推送的数据二进制数据,经过解码后,打印出了可读的数据:
{
"channel":"[email protected]@100ms@BTCUSDT",
"symbol":"BTCUSDT",
"sendTime":"1754487330985",
"publicAggreDeals":{
"deals":[
{"price":"113897.97","quantity":"0.00003103","tradeType":2,"time":"1754487330933"},
{"price":"113897.97","quantity":"0.00095331","tradeType":2,"time":"1754487330934"},
{"price":"113897.97","quantity":"0.00154766","tradeType":2,"time":"1754487330935"}
],
"eventType":"[email protected]@100ms"
}
}
通过在FMZ平台上实现MEXC的protobuf WebSocket接口,我们获得了以下优势:
该方案特别适用于: - 高频交易策略 - 实时套利系统 - 市场数据分析 - 风险监控系统
本文基于MEXC交易所最新的WebSocket protobuf接口规范编写,代码示例已在FMZ平台测试验证。