Skip to content
iTick
  1. Docs
  2. SDK & Developer Tools
  3. Python SDK

Python SDK

Official iTick Python SDK, providing REST API queries and WebSocket real-time data subscription for basic, stock, index, futures, fund, forex, and cryptocurrency data.

Python SDK Documentation

The Python version of iTick API SDK provides REST API queries and WebSocket real-time data subscription for basic, stock, index, futures, fund, forex, and cryptocurrency data.

Features

  • Supports REST API queries for basic, stock, index, futures, fund, forex, and cryptocurrency data
  • Supports WebSocket real-time data subscription
  • Automatic reconnection mechanism
  • Heartbeat to maintain connection
  • Callback-based event handling

Installation

bash
pip install itick-sdk

Or install from source:

bash
cd python
pip install -e .

Quick Start

Initialize Client

python
from itick.sdk import Client

token = "your_api_token"
client = Client(token)

REST API Usage

Forex Data Query

python
# Get forex real-time trade
tick = client.get_forex_tick("GB", "EURUSD")
print("Forex Tick:", tick)

# Get forex real-time quote
quote = client.get_forex_quote("GB", "EURUSD")
print("Forex Quote:", quote)

# Get forex real-time order book
depth = client.get_forex_depth("GB", "EURUSD")
print("Forex Depth:", depth)

# Get forex historical K-line
kline = client.get_forex_kline("GB", "EURUSD", 2, 10)
print("Forex Kline:", kline)

Stock Data Query

python
# Get stock real-time trade
tick = client.get_stock_tick("US", "AAPL")
print("Stock Tick:", tick)

# Get stock real-time quote
quote = client.get_stock_quote("US", "AAPL")
print("Stock Quote:", quote)

# Get stock real-time order book
depth = client.get_stock_depth("US", "AAPL")
print("Stock Depth:", depth)

# Get stock historical K-line
kline = client.get_stock_kline("US", "AAPL", 2, 10)
print("Stock Kline:", kline)

Cryptocurrency Data Query

python
# Get cryptocurrency real-time trade
tick = client.get_crypto_tick("BA", "BTCUSDT")
print("Crypto Tick:", tick)

# Get cryptocurrency real-time quote
quote = client.get_crypto_quote("BA", "BTCUSDT")
print("Crypto Quote:", quote)

# Get cryptocurrency real-time order book
depth = client.get_crypto_depth("BA", "BTCUSDT")
print("Crypto Depth:", depth)

# Get cryptocurrency historical K-line
kline = client.get_crypto_kline("BA", "BTCUSDT", 2, 10)
print("Crypto Kline:", kline)

WebSocket Usage

The SDK provides enhanced WebSocket functionality including automatic reconnection and heartbeat maintenance, eliminating the need for manual connection management.

Set Callback Functions

python
# Set message handler
def on_message(message):
    print(f"Received WebSocket message: {message}")

# Set error handler
def on_error(error):
    print(f"WebSocket error: {error}")

client.set_message_handler(on_message)
client.set_error_handler(on_error)

Connect and Subscribe

python
# Connect to forex WebSocket
client.connect_forex_websocket()

# Send subscription message
client.send_websocket_message('{"action": "subscribe", "codes": ["EURUSD"]}')

# Wait to receive messages
import time
time.sleep(10)

# Check connection status
print(f"WebSocket connected: {client.is_websocket_connected()}")

# Close WebSocket
client.close_websocket()

Other WebSocket Connections

python
# Connect to stock WebSocket
client.connect_stock_websocket()

# Connect to cryptocurrency WebSocket
client.connect_crypto_websocket()

API Interface List

Basics

MethodDescription
get_symbol_listGet symbol list
get_symbol_holidaysGet holiday information

Stock

MethodDescription
get_stock_infoGet stock information
get_stock_ipoGet stock IPO information
get_stock_splitGet stock split information
get_stock_tickGet stock real-time trade
get_stock_quoteGet stock real-time quote
get_stock_depthGet stock real-time order book
get_stock_klineGet stock historical K-line
get_stock_ticksGet stock batch real-time trades
get_stock_quotesGet stock batch real-time quotes
get_stock_depthsGet stock batch real-time order books
get_stock_klinesGet stock batch historical K-lines
connect_stock_websocketConnect to stock WebSocket

Indices

MethodDescription
get_indices_tickGet index real-time trade
get_indices_quoteGet index real-time quote
get_indices_depthGet index real-time order book
get_indices_klineGet index historical K-line
get_indices_ticksGet index batch real-time trades
get_indices_quotesGet index batch real-time quotes
get_indices_depthsGet index batch real-time order books
get_indices_klinesGet index batch historical K-lines
connect_indices_websocketConnect to index WebSocket

Futures

MethodDescription
get_future_tickGet futures real-time trade
get_future_quoteGet futures real-time quote
get_future_depthGet futures real-time order book
get_future_klineGet futures historical K-line
get_future_ticksGet futures batch real-time trades
get_future_quotesGet futures batch real-time quotes
get_future_depthsGet futures batch real-time order books
get_future_klinesGet futures batch historical K-lines
connect_future_websocketConnect to futures WebSocket

Funds

MethodDescription
get_fund_tickGet fund real-time trade
get_fund_quoteGet fund real-time quote
get_fund_depthGet fund real-time order book
get_fund_klineGet fund historical K-line
get_fund_ticksGet fund batch real-time trades
get_fund_quotesGet fund batch real-time quotes
get_fund_depthsGet fund batch real-time order books
get_fund_klinesGet fund batch historical K-lines
connect_fund_websocketConnect to fund WebSocket

Forex

MethodDescription
get_forex_tickGet forex real-time trade
get_forex_quoteGet forex real-time quote
get_forex_depthGet forex real-time order book
get_forex_klineGet forex historical K-line
get_forex_ticksGet forex batch real-time trades
get_forex_quotesGet forex batch real-time quotes
get_forex_depthsGet forex batch real-time order books
get_forex_klinesGet forex batch historical K-lines
connect_forex_websocketConnect to forex WebSocket

Crypto

MethodDescription
get_crypto_tickGet cryptocurrency real-time trade
get_crypto_quoteGet cryptocurrency real-time quote
get_crypto_depthGet cryptocurrency real-time order book
get_crypto_klineGet cryptocurrency historical K-line
get_crypto_ticksGet cryptocurrency batch real-time trades
get_crypto_quotesGet cryptocurrency batch real-time quotes
get_crypto_depthsGet cryptocurrency batch real-time order books
get_crypto_klinesGet cryptocurrency batch historical K-lines
connect_crypto_websocketConnect to cryptocurrency WebSocket

WebSocket Functionality

Automatic Reconnection

The SDK has a built-in automatic reconnection mechanism that automatically attempts to reconnect when network exceptions or disconnections occur:

  • Reconnection interval: 5 seconds
  • Maximum reconnection attempts: 10 times
  • Automatically restore subscriptions after successful reconnection

Heartbeat Maintenance

The SDK automatically maintains WebSocket connection heartbeats:

  • Heartbeat interval: 30 seconds
  • Automatically sends ping messages to keep the connection active

Connection Status Check

python
# Check if WebSocket is connected
connected = client.is_websocket_connected()

Complete Example

python
from itick.sdk import Client
import time

# Initialize client
token = "your_api_token"
client = Client(token)

# Set WebSocket message handler
def on_message(message):
    print(f"Received WebSocket message: {message}")

# Set WebSocket error handler
def on_error(error):
    print(f"WebSocket error: {error}")

client.set_message_handler(on_message)
client.set_error_handler(on_error)

# Test REST API
tick = client.get_forex_tick("GB", "EURUSD")
print("Forex Tick:", tick)

# Test WebSocket
try:
    client.connect_forex_websocket()

    # Send subscription message
    client.send_websocket_message('{"action": "subscribe", "codes": ["EURUSD"]}')

    # Wait to receive messages
    print("Waiting for WebSocket messages...")
    time.sleep(10)

    # Check connection status
    print(f"WebSocket connected: {client.is_websocket_connected()}")

finally:
    # Close WebSocket
    client.close_websocket()