Skip to content

futures.streams

Public methods exposed through client.futures.streams.

Constructors

new

Source code in pkg/src/mexc/futures/streams/__init__.py
@classmethod
def new(
  cls, api_key: str | None = None, api_secret: str | None = None, *,
  url: str = MEXC_FUTURES_SOCKET_URL,
  default_validate: bool = True,
):
  if api_key is None:
    api_key = os.environ['MEXC_ACCESS_KEY']
  if api_secret is None:
    api_secret = os.environ['MEXC_SECRET_KEY']
  ws = StreamsClient(url=url)
  auth_ws = AuthedStreamsClient(api_key=api_key, api_secret=api_secret, url=url)
  return cls(auth_ws=auth_ws, ws=ws, default_validate=default_validate)

Market

candles

Subscribe to futures candlestick updates.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
interval Interval

Candlestick interval.

required
validate bool

Validate pushed candle payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/candles.py
async def candles(
  self, symbol: str, interval: Interval, *, validate: bool = True,
) -> Stream[Candle, Reply, Reply]:
  """
  Subscribe to futures candlestick updates.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    interval: Candlestick interval.
    validate: Validate pushed candle payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('kline', {'symbol': symbol, 'interval': interval})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

deal

Subscribe to futures trade updates for one contract.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
validate bool

Validate pushed trade payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/deal.py
async def deal(self, symbol: str, *, validate: bool = True) -> Stream[Deal, Reply, Reply]:
  """
  Subscribe to futures trade updates for one contract.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    validate: Validate pushed trade payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('deal', {'symbol': symbol})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

depth

Subscribe to incremental futures order-book updates.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
compress bool | None

Request merged depth updates when provided.

None
validate bool

Validate pushed depth payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/depth.py
async def depth(
  self, symbol: str, *, compress: bool | None = None, validate: bool = True,
) -> Stream[Depth, Reply, Reply]:
  """
  Subscribe to incremental futures order-book updates.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    compress: Request merged depth updates when provided.
    validate: Validate pushed depth payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  params: dict[str, Any] = {'symbol': symbol}
  if compress is not None:
    params['compress'] = compress
  stream = await self.subscribe('depth', params)
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

depth_full

Subscribe to futures full-depth snapshots.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
limit DepthLimit

Number of price levels in each snapshot.

20
validate bool

Validate pushed depth payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/depth_full.py
async def depth_full(
  self, symbol: str, *, limit: DepthLimit = 20, validate: bool = True,
) -> Stream[Depth, Reply, Reply]:
  """
  Subscribe to futures full-depth snapshots.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    limit: Number of price levels in each snapshot.
    validate: Validate pushed depth payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('depth.full', {'symbol': symbol, 'limit': limit})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

fair_price

Subscribe to fair-price updates for one futures contract.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
validate bool

Validate pushed price payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/fair_price.py
async def fair_price(self, symbol: str, *, validate: bool = True) -> Stream[FairPrice, Reply, Reply]:
  """
  Subscribe to fair-price updates for one futures contract.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    validate: Validate pushed price payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('fair.price', {'symbol': symbol})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

funding_rate

Subscribe to funding-rate updates for one futures contract.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
validate bool

Validate pushed funding-rate payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/funding_rate.py
async def funding_rate(self, symbol: str, *, validate: bool = True) -> Stream[FundingRate, Reply, Reply]:
  """
  Subscribe to funding-rate updates for one futures contract.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    validate: Validate pushed funding-rate payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('funding.rate', {'symbol': symbol})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

index_price

Subscribe to index-price updates for one futures contract.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
validate bool

Validate pushed price payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/index_price.py
async def index_price(self, symbol: str, *, validate: bool = True) -> Stream[IndexPrice, Reply, Reply]:
  """
  Subscribe to index-price updates for one futures contract.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    validate: Validate pushed price payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('index.price', {'symbol': symbol})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

ticker

Subscribe to ticker updates for one futures contract.

Parameters:

Name Type Description Default
symbol str

Futures contract symbol, for example BTC_USDT.

required
validate bool

Validate pushed ticker payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/ticker.py
async def ticker(self, symbol: str, *, validate: bool = True) -> Stream[Ticker, Reply, Reply]:
  """
  Subscribe to ticker updates for one futures contract.

  Args:
    symbol: Futures contract symbol, for example `BTC_USDT`.
    validate: Validate pushed ticker payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('ticker', {'symbol': symbol})
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

all_tickers

Subscribe to ticker updates for all futures contracts.

Parameters:

Name Type Description Default
validate bool

Validate pushed ticker payloads.

True
References
Source code in pkg/src/mexc/futures/streams/market/tickers.py
async def all_tickers(self, *, validate: bool = True) -> Stream[list[AllTicker], Reply, Reply]:
  """
  Subscribe to ticker updates for all futures contracts.

  Args:
    validate: Validate pushed ticker payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
  """
  stream = await self.subscribe('tickers')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

tickers

Subscribe to ticker updates for all futures contracts.

Parameters:

Name Type Description Default
validate bool

Validate pushed ticker payloads.

True
Source code in pkg/src/mexc/futures/streams/market/tickers.py
async def tickers(self, *, validate: bool = True) -> Stream[list[AllTicker], Reply, Reply]:
  """
  Subscribe to ticker updates for all futures contracts.

  Args:
    validate: Validate pushed ticker payloads.
  """
  return await self.all_tickers(validate=validate)

User

adl_level

Subscribe to futures ADL level updates.

Parameters:

Name Type Description Default
validate bool

Validate pushed ADL level payloads.

True
References
Source code in pkg/src/mexc/futures/streams/user/adl_level.py
async def adl_level(self, *, validate: bool = True) -> Stream[AdlLevel, None, None]:
  """
  Subscribe to futures ADL level updates.

  Args:
    validate: Validate pushed ADL level payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#position-adl-level)
  """
  stream = await self.authenticated_ws.subscribe('personal.adl.level')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

asset

Subscribe to futures asset updates.

Parameters:

Name Type Description Default
validate bool

Validate pushed asset payloads.

True
References
Source code in pkg/src/mexc/futures/streams/user/asset.py
async def asset(self, *, validate: bool = True) -> Stream[Asset, None, None]:
  """
  Subscribe to futures asset updates.

  Args:
    validate: Validate pushed asset payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#asset-information)
  """
  stream = await self.authenticated_ws.subscribe('personal.asset')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

login

Authenticate the futures private WebSocket connection.

References
Source code in pkg/src/mexc/futures/streams/user/login.py
async def login(self):
  """
  Authenticate the futures private WebSocket connection.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#login)
  """
  await self.authenticated_ws.authenticate()

trades

Subscribe to your future trades.

  • validate: Whether to validate the response against the expected schema (default: True).

MEXC API docs

Source code in pkg/src/mexc/futures/streams/user/my_trades.py
async def trades(self, *, validate: bool = True) -> Stream[Deal, None, None]:
  """Subscribe to your future trades.

  - `validate`: Whether to validate the response against the expected schema (default: True).

  > [MEXC API docs](https://www.mexc.com/api-docs/futures/websocket-api#fill-details)
  """
  stream = await self.authenticated_ws.subscribe('personal.order.deal')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

order

Subscribe to futures order updates.

Parameters:

Name Type Description Default
validate bool

Validate pushed order payloads.

True
References
Source code in pkg/src/mexc/futures/streams/user/order.py
async def order(self, *, validate: bool = True) -> Stream[Order, None, None]:
  """
  Subscribe to futures order updates.

  Args:
    validate: Validate pushed order payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#order-data)
  """
  stream = await self.authenticated_ws.subscribe('personal.order')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

position

Subscribe to futures position updates.

Parameters:

Name Type Description Default
validate bool

Validate pushed position payloads.

True
References
Source code in pkg/src/mexc/futures/streams/user/position.py
async def position(self, *, validate: bool = True) -> Stream[Position, None, None]:
  """
  Subscribe to futures position updates.

  Args:
    validate: Validate pushed position payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#position-information)
  """
  stream = await self.authenticated_ws.subscribe('personal.position')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

position_mode

Subscribe to futures position-mode updates.

Parameters:

Name Type Description Default
validate bool

Validate pushed position-mode payloads.

True
References
Source code in pkg/src/mexc/futures/streams/user/position_mode.py
async def position_mode(self, *, validate: bool = True) -> Stream[PositionMode, None, None]:
  """
  Subscribe to futures position-mode updates.

  Args:
    validate: Validate pushed position-mode payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#position-mode)
  """
  stream = await self.authenticated_ws.subscribe('personal.position.mode')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)

risk_limit

Subscribe to futures risk-limit updates.

Parameters:

Name Type Description Default
validate bool

Validate pushed risk-limit payloads.

True
References
Source code in pkg/src/mexc/futures/streams/user/risk_limit.py
async def risk_limit(self, *, validate: bool = True) -> Stream[RiskLimit, None, None]:
  """
  Subscribe to futures risk-limit updates.

  Args:
    validate: Validate pushed risk-limit payloads.

  References:
    - [MEXC futures WebSocket API](https://mexcdevelop.github.io/apidocs/contract_v1_en/#risk-limit)
  """
  stream = await self.authenticated_ws.subscribe('personal.risk.limit')
  async def parsed_stream():
    async for msg in stream:
      yield validate_response(msg) if self.validate(validate) else msg
  return Stream(reply=stream.reply, stream=parsed_stream(), unsubscribe=stream.unsubscribe)