Skip to main content

Websocket Market Streams

  • The base endpoint is: wss://wbs-api.mexc.com/ws
  • Each connection to wss://wbs-api.mexc.com/ws is valid for no more than 24 hours. Please handle disconnections and reconnections properly.
  • All trading pair names in the symbol must be in uppercase. For example: [email protected]@<symbol>
    Example: [email protected]@100ms@BTCUSDT
  • If there is no valid subscription on the websocket, the server will actively disconnect after 30 seconds. If the subscription is successful but there is no data flow, the server will disconnect after one minute. The client can send a ping to keep the connection alive.
  • One ws connection supports a maximum of 30 subscriptions.
  • Please process the data according to the parameters returned in the documentation. Parameters not returned in the documentation will be optimized soon, so please do not use them.

Live Subscription/Unsubscription to Data Streams

  • The following data can be sent via websocket to subscribe or unsubscribe from data streams. Examples are provided below.
  • The id in the response is an unsigned integer and serves as the unique identifier for communication.
  • If the msg in the response matches the corresponding request field, it indicates that the request was sent successfully.

Protocol Buffers Integration

The current websocket push uses the protobuf format. The specific integration process is as follows:

1.PB File Definition
The PB definition files can be obtained via the provided link:https://github.com/mexcdevelop/websocket-proto

2.Generate Deserialization Code
Use the tool available at https://github.com/protocolbuffers/protobuf to compile the .proto files and generate deserialization code.

Java

protoc *.proto --java_out=python custom_path

Python

protoc *.proto --python_out=python custom_path

Others

Multiple languages are supported, including C++, C#, Go, Ruby, PHP, JS, etc. For details, see <a href="https://github.com/protocolbuffers/protobuf" title="https://github.com/protocolbuffers/protobuf" aria-label="https://github.com/protocolbuffers/protobuf" rel="nofollow">https://github.com/protocolbuffers/protobuf</a>.

3.Data Deserialization
Use the code generated in the previous step to deserialize the data.

Java
Include the protobuf-java dependency:

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>{protobuf.version}</version> <!-- Specify the version as per your project requirements -->
</dependency>
//Parsing example:

// Assemble the object
PushDataV3ApiWrapper pushDataV3ApiWrapper = PushDataV3ApiWrapper.newBuilder()
.setChannel("[email protected]@10ms")
.setSymbol("BTCUSDT")
.setSendTime(System.currentTimeMillis())
.build();

// Serialize to a byte array
byte[] serializedData = pushDataV3ApiWrapper.toByteArray();

// Deserialize into a PushDataV3ApiWrapper object
PushDataV3ApiWrapper resultV3 = PushDataV3ApiWrapper.parseFrom(serializedData);

Python

#Parsing example:

import PushDataV3ApiWrapper_pb2

# Assemble the object
pushData = PushDataV3ApiWrapper_pb2.PushDataV3ApiWrapper()
pushData.channel = '[email protected]@10ms'
pushData.symbol = 'BTCUSDT'

# Serialize to a string
serializedData = pushData.SerializeToString()

# Deserialize into a PushDataV3ApiWrapper object
result = PushDataV3ApiWrapper_pb2.PushDataV3ApiWrapper()
result.ParseFromString(serializedData)
print(result)

Subscribe to a Data Stream

Subscription Channel Response

{
"id": 0,
"code": 0,
"msg": "[email protected]@100ms@BTCUSDT"
}
  • Request
{
"method": "SUBSCRIPTION",
"params": ["[email protected]@100ms@BTCUSDT"]
}

Unsubscribe from a Data Stream

Unsubscription Response

{
"id": 0,
"code": 0,
"msg": "[email protected]@100ms@BTCUSDT"
}
  • Request
{
"method": "UNSUBSCRIPTION",
"params": ["[email protected]@100ms@BTCUSDT"]
}

PING/PONG Mechanism

PING/PONG Response

{
"id": 0,
"code": 0,
"msg": "PONG"
}
  • Request
{"method": "PING"}

Trade Streams

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}

Response:

{
"channel": "[email protected]@100ms@BTCUSDT",
"publicdeals": {
"dealsList": [
{
"price": "93220.00", // Trade price
"quantity": "0.04438243", // Trade quantity
"tradetype": 2, // Trade type (1: Buy, 2: Sell)
"time": 1736409765051 // Trade time
}
],
"eventtype": "[email protected]@100ms" // Event type
},
"symbol": "BTCUSDT", // Trading pair
"sendtime": 1736409765052 // Event time
}

Request Parameter: [email protected]@(100ms|10ms)@<symbol>

The Trade Streams push raw trade information; each trade has a unique buyer and seller

Response Parameters:

ParameterData TypeDescription
dealsListarrayTrade information
pricestringTrade price
quantitystringTrade quantity
tradetypeintTrade type (1: Buy, 2: Sell)
timelongTrade time
eventtypestringEvent type
symbolstringTrading pair
sendtimelongEvent time

K-line Streams

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@BTCUSDT@Min15"
]
}

Response:

{
"channel": "[email protected]@BTCUSDT@Min15",
"publicspotkline": {
"interval": "Min15", // K-line interval
"windowstart": 1736410500, // Start time of the K-line
"openingprice": "92925", // Opening trade price during this K-line
"closingprice": "93158.47", // Closing trade price during this K-line
"highestprice": "93158.47", // Highest trade price during this K-line
"lowestprice": "92800", // Lowest trade price during this K-line
"volume": "36.83803224", // Trade volume during this K-line
"amount": "3424811.05", // Trade amount during this K-line
"windowend": 1736411400 // End time of the K-line
},
"symbol": "BTCUSDT",
"symbolid": "2fb942154ef44a4ab2ef98c8afb6a4a7",
"createtime": 1736410707571
}

The Kline/Candlestick Stream push updates to the current klines/candlestick every second.

Request Parameter: [email protected]@<symbol>@<interval>

Response Parameters:

ParameterData TypeDescription
publicspotklineobjectK-line information
intervalstringK-line interval
windowstartlongStart time of the K-line
openingpricebigDecimalOpening trade price during this K-line
closingpricebigDecimalClosing trade price during this K-line
highestpricebigDecimalHighest trade price during this K-line
lowestpricebigDecimalLowest trade price during this K-line
volumebigDecimalTrade volume during this K-line
amountbigDecimalTrade amount during this K-line
windowendlongEnd time of the K-line
symbolstringTrading pair
symbolidstringTrading pair ID
createtimelongEvent time

K-line Interval Parameters:

  • Min: Minutes; Hour: Hours; Day: Days; Week: Weeks; M: Month

Available intervals:

  • Min1
  • Min5
  • Min15
  • Min30
  • Min60
  • Hour4
  • Hour8
  • Day1
  • Week1
  • Month1

Diff.Depth Stream

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}

Response:

{
"channel": "[email protected]@100ms@BTCUSDT",
"publicincreasedepths": {
"asksList": [], // asks: Sell orders
"bidsList": [ // bids: Buy orders
{
"price": "92877.58", // Price level of change
"quantity": "0.00000000" // Quantity
}
],
"eventtype": "[email protected]@100ms", // Event type
"fromVersion" : "10589632359", // from version
"toVersion" : "10589632359" // to version
},
"symbol": "BTCUSDT", // Trading pair
"sendtime": 1736411507002 // Event time
}

If the order quantity (quantity) for a price level is 0, it indicates that the order at that price has been canceled or executed, and that price level should be removed.

Request Parameter: [email protected]@(100ms|10ms)@<symbol>

Response Parameters:

ParameterData TypeDescription
pricestringPrice level of change
quantitystringQuantity
eventtypestringEvent type
versionstringVersion number
symbolstringTrading pair
sendtimelongEvent time

Partial Book Depth Streams

This stream pushes limited level depth information. The "levels" indicate the number of order levels for buy and sell orders, which can be 5, 10, or 20 levels.

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@BTCUSDT@5"
]
}

Response:

{
"channel": "[email protected]@BTCUSDT@5",
"publiclimitdepths": {
"asksList": [ // asks: Sell orders
{
"price": "93180.18", // Price level of change
"quantity": "0.21976424" // Quantity
}
],
"bidsList": [ // bids: Buy orders
{
"price": "93179.98",
"quantity": "2.82651000"
}
],
"eventtype": "[email protected]", // Event type
"version": "36913565463" // Version number
},
"symbol": "BTCUSDT", // Trading pair
"sendtime": 1736411838730 // Event time
}

Request Parameter: [email protected]@<symbol>@<level>

Response Parameters:

ParameterData TypeDescription
pricestringPrice level of change
quantitystringQuantity
eventtypestringEvent type
versionstringVersion number
symbolstringTrading pair
sendtimelongEvent time

Individual Symbol Book Ticker Streams

Pushes any update to the best bid or ask's price or quantity in real-time for a specified symbol.

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}

Response:

{
"channel": "[email protected]@100ms@BTCUSDT",
"publicbookticker": {
"bidprice": "93387.28", // Best bid price
"bidquantity": "3.73485", // Best bid quantity
"askprice": "93387.29", // Best ask price
"askquantity": "7.669875" // Best ask quantity
},
"symbol": "BTCUSDT", // Trading pair
"sendtime": 1736412092433 // Event time
}

Request Parameter: [email protected]@(100ms|10ms)@<symbol>

Response Parameters:

ParameterData TypeDescription
bidpricestringBest bid price
bidquantitystringBest bid quantity
askpricestringBest ask price
askquantitystringBest ask quantity
symbolstringTrading pair
sendtimelongEvent time

Individual Symbol Book Ticker Streams(Batch Aggregation)

This batch aggregation version pushes the best order information for a specified trading pair.

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@BTCUSDT"
]
}

Response:

{
"channel" : "[email protected]@BTCUSDT",
"symbol" : "BTCUSDT",
"sendTime" : "1739503249114",
"publicBookTickerBatch" : {
"items" : [ {
"bidPrice" : "96567.37",
"bidQuantity" : "3.362925",
"askPrice" : "96567.38",
"askQuantity" : "1.545255"
} ]
}
}

Request Parameter: [email protected]@<symbol>

Response Parameters:

ParameterData TypeDescription
bidpricestringBest bid price
bidquantitystringBest bid quantity
askpricestringBest ask price
askquantitystringBest ask quantity
symbolstringTrading pair
sendtimelongEvent time

MiniTickers

minitickers of all trading pairs in the specified timezone, pushed every 3 seconds.
UTC value range: 24H, UTC-10, UTC-8, UTC-7, UTC-6, UTC-5, UTC-4, UTC-3, UTC+0, UTC+1, UTC+2, UTC+3, UTC+4, UTC+4:30, UTC+5, UTC+5:30, UTC+6, UTC+7, UTC+8, UTC+9, UTC+10, UTC+11, UTC+12, UTC+12:45, UTC+13

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@UTC+8"
]
}

Response:

{
"channel": "[email protected]@UTC+8",
"sendTime": "1755076614201",
"publicMiniTickers":
{
"items":
[
{
"symbol": "METAUSDT",
"price": "0.055",
"rate": "-0.2361",
"zonedRate": "-0.2361",
"high": "0.119",
"low": "0.053",
"volume": "814864.474",
"quantity": "10764997.16",
"lastCloseRate": "-0.2567",
"lastCloseZonedRate": "-0.2567",
"lastCloseHigh": "0.119",
"lastCloseLow": "0.053"
},
{
"symbol": "FCATUSDT",
"price": "0.0000031",
"rate": "-0.4464",
"zonedRate": "-0.4464",
"high": "0.0000066",
"low": "0.0000025",
"volume": "2825.4350195",
"quantity": "654649950.75",
"lastCloseRate": "-0.4464",
"lastCloseZonedRate": "-0.4464",
"lastCloseHigh": "0.0000066",
"lastCloseLow": "0.0000025"
},
{
"symbol": "CRVETH",
"price": "0.00022592",
"rate": "0.028",
"zonedRate": "0.028",
"high": "0.00022856",
"low": "0.00021024",
"volume": "1062.48406269",
"quantity": "4884456.998",
"lastCloseRate": "0.0276",
"lastCloseZonedRate": "0.0276",
"lastCloseHigh": "0.00022856",
"lastCloseLow": "0.00021024"
}
]
}
}

Request Parameter: channel": "[email protected]@<UTC-TIMEZONE>

Response Parameters:

Parameter NameData TypeDescription
symbolstringTrading pair name
pricestringLatest price
ratestringPrice change percentage (UTC+8 timezone)
zonedRatestringPrice change percentage (local timezone)
highstringRolling highest price
lowstringRolling lowest price
volumestringRolling turnover amount
quantitystringRolling trading volume
lastCloseRatestringPrevious close change percentage (UTC+8 timezone)
lastCloseZonedRatestringPrevious close change percentage (local timezone)
lastCloseHighstringPrevious close rolling highest price
lastCloseLowstringPrevious close rolling lowest price

MiniTicker

miniticker of the specified trading pair in the specified timezone, pushed every 3 seconds.
UTC value range: 24H, UTC-10, UTC-8, UTC-7, UTC-6, UTC-5, UTC-4, UTC-3, UTC+0, UTC+1, UTC+2, UTC+3, UTC+4, UTC+4:30, UTC+5, UTC+5:30, UTC+6, UTC+7, UTC+8, UTC+9, UTC+10, UTC+11, UTC+12, UTC+12:45, UTC+13

Request:

{
"method": "SUBSCRIPTION",
"params": [
"[email protected]@MXUSDT@UTC+8"
]
}

Response:

{
"channel" : "[email protected]@MXUSDT@UTC+8",
"symbol" : "MXUSDT",
"sendTime" : "1755076752201",
"publicMiniTicker" : {
"symbol" : "MXUSDT",
"price" : "2.5174",
"rate" : "0.0766",
"zonedRate" : "0.0766",
"high" : "2.6299",
"low" : "2.302",
"volume" : "11336518.0264",
"quantity" : "4638390.17",
"lastCloseRate" : "0.0767",
"lastCloseZonedRate" : "0.0767",
"lastCloseHigh" : "2.6299",
"lastCloseLow" : "2.302"
}
}

Request Parameter: channel": "[email protected]@<symbol>@<UTC-TIMEZONE>

Response Parameters:

Parameter NameData TypeDescription
symbolstringTrading pair name
pricestringLatest price
ratestringPrice change percentage (UTC+8 timezone)
zonedRatestringPrice change percentage (local timezone)
highstringRolling highest price
lowstringRolling lowest price
volumestringRolling turnover amount
quantitystringRolling trading volume
lastCloseRatestringPrevious close change percentage (UTC+8 timezone)
lastCloseZonedRatestringPrevious close change percentage (local timezone)
lastCloseHighstringPrevious close rolling highest price
lastCloseLowstringPrevious close rolling lowest price

How to Properly Maintain a Local Copy of the Order Book

1.Open a WebSocket connection and subscribe to [email protected]@(100ms|10ms)@{symbol}.

2.Start caching the received incremental depth updates, and record the fromVersion of the first received update.

3.Request the order book snapshot from https://api.mexc.com/api/v3/depth?symbol={symbol}&limit=5000 and record the lastUpdateId.

4.If lastUpdateId is less than the fromVersion of the first cached update, return to Step 3 and fetch the snapshot again.

5.From the cached incremental updates, discard all updates where toVersion is less than or equal to lastUpdateId.

6.Take the first remaining update. If fromVersion is greater than lastUpdateId + 1, the data is considered discontinuous. Return to Step 3 and reinitialize.

7.Initialize the local Order Book using the snapshot data, and set the local version to lastUpdateId.Then, sequentially apply all remaining cached updates and all subsequent incoming updates. After each update is applied, update the local version to the update’s toVersion. If any update does not satisfy: fromVersion = previous toVersion + 1, the Order Book must be reinitialized starting from Step 3.

Note: Since the depth snapshot has a limitation on the number of price levels, price levels outside the initial snapshot that have not changed in quantity will not appear in incremental push messages. Therefore, the local order book may differ slightly from the real order book. However, for most use cases, the 5000-depth limit is sufficient to effectively understand the market and trading activity.