AllTick
-
real-time market datastock api Forex APIMarket Data Subscription

How Can WebSocket Market Data Streaming Subscribe to Multiple Stocks and Currency Pairs Simultaneously?

Learn how to subscribe to multiple stocks and forex pairs using the AllTick WebSocket API, including connection endpoints, batch subscriptions, subscription replacement rules, and unified market data processing.

AllTick5 min read

When developing a real-time market data page, it is often necessary to display multiple stocks and forex currency pairs simultaneously. For example, a trading monitoring dashboard may need to track Tencent (700.HK) and Apple (AAPL.US) while also monitoring EUR/USD and USD/JPY.

Creating a new WebSocket connection every time a symbol is added not only increases connection management complexity but may also quickly reach the connection limits of the subscribed plan.

A more practical approach is to include multiple symbols of the same market type in one subscription request and use separate WebSocket connections for different market types. This reduces unnecessary connections and makes it easier to process data from both connections in a unified way at the application level.

Stocks and Forex Cannot Use the Same WebSocket Address

When using the AllTick WebSocket API, the first step is to distinguish between the connection addresses for stock and forex data.

These two types of data use different WebSocket paths:

Data TypeWebSocket Address PathExample Symbols
Stocks and indices/quote-stock-b-ws-api700.HK, AAPL.US
Forex and other instruments/quote-b-ws-apiEURUSD, USDJPY

An API Token must be appended to the connection address. Stocks and forex cannot simply be combined in the same connection, so separate connections must be established for each type.

In addition, symbol codes must match the format and capitalization used in AllTick's product list. For example, US stocks use AAPL.US, Hong Kong stocks use 700.HK, and forex currency pairs use EURUSD. Before subscribing, confirm that the relevant instruments are available in the product list.

Subscribe to Multiple Stocks and Currency Pairs in One Request

Once the connection is established, multiple symbols can be submitted together through the symbol_list array. There is no need to send a separate subscription request for each stock or currency pair.

For example, first subscribe to Hong Kong and US stocks through the stock connection:

text
{
  "cmd_id": 22004,
  "seq_id": 101,
  "trace": "stock-tick-sub",
  "data": {
    "symbol_list": [
      {"code": "700.HK"},
      {"code": "AAPL.US"}
    ]
  }
}

Then subscribe to multiple currency pairs through the forex connection:

text
{
  "cmd_id": 22004,
  "seq_id": 102,
  "trace": "forex-tick-sub",
  "data": {
    "symbol_list": [
      {"code": "EURUSD"},
      {"code": "USDJPY"}
    ]
  }
}

Here, cmd_id: 22004 corresponds to the subscription request for the latest traded price. After receiving a successful subscription response, the server pushes the corresponding real-time data using cmd_id: 22998.

Each pushed message contains the corresponding instrument code. Developers can use this field to identify which stock or currency pair the market data belongs to.

Pay Attention to the Subscription Replacement Rule When Adding New Symbols

In real-world development, the symbol list is not always fixed. For example, a dashboard may initially subscribe to two stocks, and a user may later add a third.

One detail that is easy to overlook is that AllTick WebSocket subscriptions follow a replacement rule. When another subscription request is sent through the same connection, the new request replaces the previous subscription list rather than automatically appending to it.

Suppose the original subscription contains:

text
{
  "symbol_list": [
    {"code": "700.HK"},
    {"code": "AAPL.US"}
  ]
}

If you want to add 600416.SH, you should submit the complete updated list:

text
{
  "symbol_list": [
    {"code": "700.HK"},
    {"code": "AAPL.US"},
    {"code": "600416.SH"}
  ]
}

If you send only the newly added stock code, the previous subscriptions may no longer be retained. Therefore, it is recommended to maintain the current subscription list on the application server. Whenever a symbol is added or removed, update the list first and then send the complete subscription request.

How to Process Data from Both WebSocket Connections in One System

Although stocks and forex use different WebSocket connections, both data streams can be integrated into a shared market data processing module at the application level.

For example, the stock connection receives data for 700.HK and AAPL.US, while the forex connection receives data for EURUSD and USDJPY. When a message arrives, the application updates the latest quote for the corresponding instrument based on the code field in the message.

text
function handleMarketMessage(message) {
  const data = JSON.parse(message);

  if (data.cmd_id === 22998) {
    const { code, price, volume } = data.data;

    // Update the corresponding quote by instrument code
    latestQuotes[code] = {
      price,
      volume
    };
  }
}

With this approach, the frontend only needs to read from a unified market data object. It does not need to know which connection each instrument's data comes from.

In an actual deployment, the status and heartbeat of the two connections must also be managed separately. AllTick requires a heartbeat every 10 seconds. A connection will be disconnected if no heartbeat is sent for more than 30 seconds. If the network connection is interrupted, the application should reconnect and resubmit the complete subscription list.

In addition, the interval between requests on the same WebSocket connection must be at least 1 second, while the interval between requests on different WebSocket connections must be at least 3 seconds.

Subscription Limits and Data Types Also Matter

Being able to subscribe to multiple instruments simultaneously does not mean that an unlimited number of symbols can be added. AllTick sets subscription limits on the number of instruments per connection according to the subscribed plan. If the limit is exceeded, some of the additional symbol codes may not be processed. The subscription scale for stocks and forex should therefore be planned according to the permissions of the actual account.

The official interface restriction documentation also explains that stocks and forex use different addresses, but when both data types are connected simultaneously, they are counted as one WebSocket connection.

It is also important to distinguish between different data types. 22004 is used to subscribe to the latest traded price. If the application requires bid and ask order book depth, use 22002 to subscribe to order book data, which is pushed by the server through 22999. If historical or real-time updated K-line data is required, use the corresponding HTTP API. K-line subscriptions cannot be mixed into the WebSocket traded-price stream.

For developers who need to monitor stocks and forex simultaneously, the key is not to keep increasing the number of connections. Instead, subscriptions should be separated by market type, complete symbol lists should be maintained, and market data should then be integrated through a unified data processing layer.

Before integration, refer to the AllTick WebSocket API documentation and the WebSocket interface limitations to confirm the subscription format, instrument limits, and connection requirements. You can also visit the AllTick official website to explore real-time market data APIs and account plans.

Start streaming market data today

Generate a free API key in seconds and connect to every market from one endpoint.