- Docs
- SDK & Developer Tools
- 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-sdkOr 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
| Method | Description |
|---|---|
| get_symbol_list | Get symbol list |
| get_symbol_holidays | Get holiday information |
Stock
| Method | Description |
|---|---|
| get_stock_info | Get stock information |
| get_stock_ipo | Get stock IPO information |
| get_stock_split | Get stock split information |
| get_stock_tick | Get stock real-time trade |
| get_stock_quote | Get stock real-time quote |
| get_stock_depth | Get stock real-time order book |
| get_stock_kline | Get stock historical K-line |
| get_stock_ticks | Get stock batch real-time trades |
| get_stock_quotes | Get stock batch real-time quotes |
| get_stock_depths | Get stock batch real-time order books |
| get_stock_klines | Get stock batch historical K-lines |
| connect_stock_websocket | Connect to stock WebSocket |
Indices
| Method | Description |
|---|---|
| get_indices_tick | Get index real-time trade |
| get_indices_quote | Get index real-time quote |
| get_indices_depth | Get index real-time order book |
| get_indices_kline | Get index historical K-line |
| get_indices_ticks | Get index batch real-time trades |
| get_indices_quotes | Get index batch real-time quotes |
| get_indices_depths | Get index batch real-time order books |
| get_indices_klines | Get index batch historical K-lines |
| connect_indices_websocket | Connect to index WebSocket |
Futures
| Method | Description |
|---|---|
| get_future_tick | Get futures real-time trade |
| get_future_quote | Get futures real-time quote |
| get_future_depth | Get futures real-time order book |
| get_future_kline | Get futures historical K-line |
| get_future_ticks | Get futures batch real-time trades |
| get_future_quotes | Get futures batch real-time quotes |
| get_future_depths | Get futures batch real-time order books |
| get_future_klines | Get futures batch historical K-lines |
| connect_future_websocket | Connect to futures WebSocket |
Funds
| Method | Description |
|---|---|
| get_fund_tick | Get fund real-time trade |
| get_fund_quote | Get fund real-time quote |
| get_fund_depth | Get fund real-time order book |
| get_fund_kline | Get fund historical K-line |
| get_fund_ticks | Get fund batch real-time trades |
| get_fund_quotes | Get fund batch real-time quotes |
| get_fund_depths | Get fund batch real-time order books |
| get_fund_klines | Get fund batch historical K-lines |
| connect_fund_websocket | Connect to fund WebSocket |
Forex
| Method | Description |
|---|---|
| get_forex_tick | Get forex real-time trade |
| get_forex_quote | Get forex real-time quote |
| get_forex_depth | Get forex real-time order book |
| get_forex_kline | Get forex historical K-line |
| get_forex_ticks | Get forex batch real-time trades |
| get_forex_quotes | Get forex batch real-time quotes |
| get_forex_depths | Get forex batch real-time order books |
| get_forex_klines | Get forex batch historical K-lines |
| connect_forex_websocket | Connect to forex WebSocket |
Crypto
| Method | Description |
|---|---|
| get_crypto_tick | Get cryptocurrency real-time trade |
| get_crypto_quote | Get cryptocurrency real-time quote |
| get_crypto_depth | Get cryptocurrency real-time order book |
| get_crypto_kline | Get cryptocurrency historical K-line |
| get_crypto_ticks | Get cryptocurrency batch real-time trades |
| get_crypto_quotes | Get cryptocurrency batch real-time quotes |
| get_crypto_depths | Get cryptocurrency batch real-time order books |
| get_crypto_klines | Get cryptocurrency batch historical K-lines |
| connect_crypto_websocket | Connect 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()