AllTick Real-time Forex, Crypto, Stock & Finance WebSocket API

repository·main·Indexed 20 days ago

https://github.com/alltick/alltick-realtime-forex-crypto-stock-tick-finance-websocket-api

A real-time financial market data provider offering high-frequency tick data and order book depth for stocks (HK, US, A-share), forex, commodities, and cryptocurrencies. The API provides two primary interfaces: a RESTful HTTP interface for request-response queries (latest prices, K-line data, static product info) and a WebSocket interface for real-time streaming of transaction quotes and order book updates. Implementation examples are provided for PHP, Python, Go, and Java.

Tokens
38.5K
Snippets
99
Records
171
Agent score
69%

What's inside AllTick API

  1. Overview of AllTick Real-time Quotation APIs

    main

    AllTick provides real-time financial market data APIs covering various asset classes including:

    • Stocks: Hong Kong (HK), US, and A-shares (Shanghai/Shenzhen).
    • Forex: Real-time currency exchange rates and order books.
    • Cryptocurrency: Real-time quotes and multi-level order books.
    • Commodities: Precious metals and other commodity data.

    The service provides both HTTP interfaces (for polling/request-response) and WebSocket interfaces (for real-time streaming/subscriptions) for tick-by-tick quotes, order books, and K-line (candlestick) data.

  2. How IP and Token-based Rate Limiting Works

    main

    Rate limiting is applied to the Token, not the IP address. Multiple IPs using the same Token will share the same rate limit pool.

    Example Behavior: If your plan allows 1 request per second and you call /kline at 14:03:01, the request succeeds. If you call /trade-tick using the same Token at the exact same second (14:03:01), the second request will receive an error response because the Token's limit for that second has been reached.

  3. How to access K-line data

    main

    Alltick's WebSocket interface does not support K-line data push. To access both historical and real-time K-lines, you must use the HTTP interface.

    To build a reliable K-line system, combine the following HTTP interfaces:

    1. Initial Sync: Use the /kline interface to poll and store historical data in a local database. Once stored, retrieve historical data from your database instead of making repeated API requests.
    2. Continuous Updates: Use the /batch-kline interface to continuously request the latest two K-lines for multiple products. Use this data to update your local database.

    This hybrid approach ensures quick updates of the latest K-lines while minimizing the load from frequent historical data requests.

  4. Understand IP and Token-based rate limiting

    main

    Rate limiting is applied to the API token, not the IP address.

    Multiple requests from the same IP using different tokens are treated independently. However, multiple requests using the same token within the allowed frequency window will be rejected.

    Example (Basic Plan - 1 request per second):

    • Requesting /kline at 14:03:01 and /trade-tick at 14:03:01 is allowed (different interfaces).
    • Requesting /kline twice at 14:03:01 will result in the first succeeding and the second being rejected.
  5. Available API Interfaces and Documentation Structure

    main

    The AllTick API ecosystem is organized into several functional areas. Use the following categories to find specific documentation:

    1. Setup and Fundamentals

    • Access Guide: How to integrate the APIs.
    • Error Codes: Description of API error responses.
    • Token Acquisition: How to apply for a free token.

    2. Product Identification

    To query specific assets, you must use their unique product codes. Lists are provided for:

    • A-shares
    • HK stocks
    • US stocks
    • Cryptocurrency
    • Commodities (Precious Metals)
    • Forex

    3. HTTP Interfaces (Request-Response)

    Used for querying historical or snapshot data:

    • Latest Transaction Price: Batch or single product latest price.
    • Order Book: Get the latest order book snapshot.
    • K-line Data: Query historical K-line (OHLC) data for a single product or in batches.
    • Static Information: Get basic product info and suspension/resumption status.
    • Market Status: Information on price changes, holidays, and delistings.

    4. WebSocket Interfaces (Real-time Streaming)

    Used for low-latency data streaming:

    • Real-time Transaction Quotes: Subscribe to latest price and tick-by-tick data.
    • Real-time Order Book: Subscribe to live order book updates.
    • Subscription Management: How to cancel real-time quote subscriptions.
    • Heartbeat: Maintaining the connection.
  6. Understand WebSocket connection limits by plan

    main

    WebSocket connection limits are enforced per Token, not per IP address. If your plan allows $N$ connections, you can distribute those connections across multiple IP addresses or use them all from a single IP.

    Important Exception: Requests for "Stock Market Data" (股票大盘类数据) and "Forex/Precious Metals/Crude Oil Data" (外汇贵金属原油类数据) use different request URLs. These two categories are counted separately. For example, if a Basic plan allows only 1 connection, you can have one connection active for Stocks and a separate connection active for Forex/Metals simultaneously.

  7. Batch Subscribe to Real-time Transaction Quotes (Tick Data)

    main

    This interface allows for batch subscription to the latest transaction prices (real-time tick-by-tick data). It does not support historical tick data.

    Key Subscription Behavior

    • Overwrite Model: Each subscription request sent over a WebSocket connection will overwrite the previous subscription. To append new symbols, you must re-send the entire list including both the old and new symbols (e.g., if you have A, B, C and want to add E, F, G, you must send A, B, C, E, F, G).
    • Heartbeat Requirement: Once subscribed, you do not need to resend the subscription request. However, you must send a heartbeat every 10 seconds. If no heartbeat is received within 30 seconds, the server will consider the connection timed out and disconnect the WebSocket.
    • Reconnection: It is recommended to implement automatic reconnection logic to handle network interruptions.
    /* Concept: Subscription Overwrite Pattern */
    // Initial subscription
    subscribe(["AAPL.US", "MSFT.US"])
    
    // To add GOOGL.US, you must resend the full list
    subscribe(["AAPL.US", "MSFT.US", "GOOGL.US"])
  8. How to obtain K-line (candlestick) data

    main

    Alltick's WebSocket interface does not support K-line data pushing. Both historical and real-time K-line data must be retrieved via HTTP interfaces.

    To implement a high-performance K-line update system, the following pattern is recommended:

    1. Initial Sync: Use the /kline interface to poll historical data and store it in your local database.
    2. Real-time Updates: Continuously use the /batch-kline interface to batch request the latest two K-lines for multiple products. Update your local database with this data.

    This approach ensures rapid updates of the latest K-lines while avoiding rate limits associated with repeatedly requesting large amounts of historical data.

  9. Understand WebSocket connection and IP limits

    main

    WebSocket connection limits are enforced per Token, not per IP address.

    • Token-based enforcement: If your plan allows only one connection (e.g., Basic plan), attempting a second connection from the same IP or a different IP will be rejected.
    • Plan-based capacity: The Advanced plan allows up to three simultaneous connections, which can be distributed across multiple IPs or concentrated on one.
    • Data Type Exception: "Stock Market Data" and "Forex, Precious Metals, and Crude Oil Data" use different request URLs. These are treated as separate connection types. For example, in a Basic plan (1 connection limit), you can have one connection active for Stock data and a separate connection active for Forex/Commodity data simultaneously.
  10. Important considerations for AllTick API usage

    main

    When consuming AllTick APIs, keep the following operational constraints and data formats in mind:

    Data Constraints

    • No Pagination: All interfaces do not support pagination; they return the full dataset for the most recent year.
    • Sorting: Data is returned in descending order by announcement time (newest first).
    • Large Payloads: Because interfaces return full yearly datasets, you should set appropriate timeouts in your client to handle large data volumes.

    Time Formats

    • timestamp field: Uses ISO format (yyyy-MM-dd'T'HH:mm:ss).
    • Other time fields: Use yyyy-MM-dd HH:mm:ss format.

    Nullability Rules

    When reviewing field documentation, pay attention to the nullability indicator:

    • "否" (No): The field always contains a value and will not be null.
    • "是" (Yes): The field may be null or an empty string; your implementation must include null/empty checks.
  11. Access AllTick APIs via HTTP or WebSocket

    main

    AllTick offers two primary interface types for data consumption:

    1. HTTP Interface: Best for request-response patterns such as querying the latest transaction price, latest order book, K-line data, or static product information.
    2. WebSocket Interface: Best for real-time streaming of transaction quotes and order book updates via subscriptions.

    Before integrating, you must acquire a token via the Token Application process.