Skip to content

spot.streams

Public methods exposed through client.spot.streams.

Constructors

new

Source code in pkg/src/mexc/spot/streams/__init__.py
@classmethod
def new(
  cls, api_key: str | None = None, api_secret: str | None = None, *,
  api_url: str = MEXC_SPOT_API_BASE,
  ws_url: str = MEXC_SPOT_SOCKET_URL,
):
  import os
  if api_key is None:
    api_key = os.environ['MEXC_ACCESS_KEY']
  if api_secret is None:
    api_secret = os.environ['MEXC_SECRET_KEY']
  auth_http = AuthHttpClient(api_key=api_key, api_secret=api_secret)
  return cls(api_url=api_url, ws_url=ws_url, auth_http=auth_http)

Listen Keys

create

Create a spot user-stream listen key.

Source code in pkg/src/mexc/spot/streams/__init__.py
async def create(self) -> str:
  """Create a spot user-stream listen key."""
  return await self.auth_ws.listen_key()

keepalive

Keep a spot user-stream listen key alive.

Source code in pkg/src/mexc/spot/streams/__init__.py
async def keepalive(self, key: str):
  """Keep a spot user-stream listen key alive."""
  return await self.auth_ws.refresh_key(key)

close

Close a spot user-stream listen key.

Source code in pkg/src/mexc/spot/streams/__init__.py
async def close(self, key: str):
  """Close a spot user-stream listen key."""
  return await self.auth_ws.delete_key(key)

Market

book_ticker

Subscribe to best bid/ask updates for a spot symbol.

Parameters:

Name Type Description Default
symbol str

Spot symbol, for example BTCUSDT.

required
aggregation Aggregation

Update aggregation interval.

'100ms'
References
Source code in pkg/src/mexc/spot/streams/market/book_ticker.py
def book_ticker(
  self, symbol: str, aggregation: Aggregation = '100ms',
) -> StreamManager[PublicAggreBookTickerV3Api, Reply, Reply]:
  """
  Subscribe to best bid/ask updates for a spot symbol.

  Args:
    symbol: Spot symbol, for example `BTCUSDT`.
    aggregation: Update aggregation interval.

  References:
    - [MEXC spot WebSocket market streams](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#bookticker-streams)
  """
  return StreamManager(lambda: self._book_ticker_impl(symbol, aggregation))

book_ticker_batch

Subscribe to batch best bid/ask updates for a spot symbol.

Parameters:

Name Type Description Default
symbol str

Spot symbol, for example BTCUSDT.

required
References
Source code in pkg/src/mexc/spot/streams/market/book_ticker_batch.py
def book_ticker_batch(self, symbol: str) -> StreamManager[PublicBookTickerBatchV3Api, Reply, Reply]:
  """
  Subscribe to batch best bid/ask updates for a spot symbol.

  Args:
    symbol: Spot symbol, for example `BTCUSDT`.

  References:
    - [MEXC spot WebSocket market streams](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#bookticker-streams)
  """
  return StreamManager(lambda: self._book_ticker_batch_impl(symbol))

candles

Subscribe to klines (candles) for a given symbol.

  • symbol: The symbol being traded, e.g. BTCUSDT.
  • interval: The interval of the klines (default: 1m).

MEXC API docs

Source code in pkg/src/mexc/spot/streams/market/candles.py
def candles(self, symbol: str, interval: Interval) -> StreamManager[PublicSpotKlineV3Api, Reply, Reply]:
  """Subscribe to klines (candles) for a given symbol.

  - `symbol`: The symbol being traded, e.g. `BTCUSDT`.
  - `interval`: The interval of the klines (default: 1m).

  > [MEXC API docs](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#k-line-streams)
  """
  return StreamManager(lambda: self._candles_impl(symbol, interval))

depth

Subscribe to the order book for a given symbol.

  • symbol: The symbol being traded, e.g. BTCUSDT.
  • level: The level of the order book (default: 5).

MEXC API docs

Source code in pkg/src/mexc/spot/streams/market/depth.py
def depth(self, symbol: str, level: Literal[5, 10, 20] = 5) -> StreamManager[PublicLimitDepthsV3Api, Reply, Reply]:
  """Subscribe to the order book for a given symbol.

  - `symbol`: The symbol being traded, e.g. `BTCUSDT`.
  - `level`: The level of the order book (default: 5).

  > [MEXC API docs](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#partial-book-depth-streams)
  """
  return StreamManager(lambda: self._depth_impl(symbol, level))

depth_updates

Subscribe to aggregated spot order-book delta updates.

Parameters:

Name Type Description Default
symbol str

Spot symbol, for example BTCUSDT.

required
aggregation Aggregation

Update aggregation interval.

'10ms'
References
Source code in pkg/src/mexc/spot/streams/market/depth_updates.py
def depth_updates(
  self, symbol: str, aggregation: Aggregation = '10ms',
) -> StreamManager[PublicAggreDepthsV3Api, Reply, Reply]:
  """
  Subscribe to aggregated spot order-book delta updates.

  Args:
    symbol: Spot symbol, for example `BTCUSDT`.
    aggregation: Update aggregation interval.

  References:
    - [MEXC spot WebSocket market streams](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#diffdepth-stream)
    - [Maintaining a local order book](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#how-to-properly-maintain-a-local-copy-of-the-order-book)
  """
  return StreamManager(lambda: self._depth_updates_impl(symbol, aggregation))

trades

Subscribe to aggregated spot trade updates.

Parameters:

Name Type Description Default
symbol str

Spot symbol, for example BTCUSDT.

required
aggregation Aggregation

Update aggregation interval.

'100ms'
References
Source code in pkg/src/mexc/spot/streams/market/trades.py
def trades(
  self, symbol: str, aggregation: Aggregation = '100ms',
) -> StreamManager[PublicAggreDealsV3Api, Reply, Reply]:
  """
  Subscribe to aggregated spot trade updates.

  Args:
    symbol: Spot symbol, for example `BTCUSDT`.
    aggregation: Update aggregation interval.

  References:
    - [MEXC spot WebSocket market streams](https://www.mexc.com/api-docs/spot-v3/websocket-market-streams#trade-streams)
  """
  return StreamManager(lambda: self._trades_impl(symbol, aggregation))

User

account

Subscribe to spot account balance updates.

References
Source code in pkg/src/mexc/spot/streams/user/account.py
def account(self) -> StreamManager[PrivateAccountV3Api, Reply, Reply]:
  """
  Subscribe to spot account balance updates.

  References:
    - [MEXC spot WebSocket user streams](https://www.mexc.com/api-docs/spot-v3/websocket-user-data-streams#spot-account-update)
  """
  return StreamManager(self._account_impl)

trades

Subscribe to your trades.

MEXC API docs

Source code in pkg/src/mexc/spot/streams/user/my_trades.py
def trades(self) -> StreamManager[PrivateDealsV3Api, Reply, Reply]:
  """Subscribe to your trades.

  > [MEXC API docs](https://www.mexc.com/api-docs/spot-v3/websocket-user-data-streams#spot-account-deals)
  """
  return StreamManager(self._trades_impl)

orders

Subscribe to spot order updates.

References
Source code in pkg/src/mexc/spot/streams/user/orders.py
def orders(self) -> StreamManager[PrivateOrdersV3Api, Reply, Reply]:
  """
  Subscribe to spot order updates.

  References:
    - [MEXC spot WebSocket user streams](https://www.mexc.com/api-docs/spot-v3/websocket-user-data-streams#spot-account-orders)
  """
  return StreamManager(self._orders_impl)