Python SDK REST API 和 WebSocket 實時推送接口 金融數據API文檔 SDK & 開發工具 iTick 官方 Python SDK,提供基礎、股票、指數、期貨、基金、外匯、加密貨幣數據的 REST API 查詢和 WebSocket 實時數據訂閱功能。
Python SDK 文檔
Python 語言版本的 iTick API SDK,提供基礎、股票、指數、期貨、基金、外匯、加密貨幣數據的 REST API 查詢和 WebSocket 實時數據訂閱功能。
功能特性
- 支持 REST API 查詢基礎、股票、指數、期貨、基金、外匯、加密貨幣數據
- 支持 WebSocket 實時數據訂閱
- 自動重連機制
- 心跳保持連接
- 回調式事件處理
安裝
pip install itick-sdk
或從源碼安裝:
cd python
pip install -e .
快速開始
初始化客戶端
from itick.sdk import Client
token = "your_api_token"
client = Client(token)
REST API 使用
外匯數據查詢
# 獲取外匯實時成交
tick = client.get_forex_tick("GB", "EURUSD")
print("Forex Tick:", tick)
# 獲取外匯實時報價
quote = client.get_forex_quote("GB", "EURUSD")
print("Forex Quote:", quote)
# 獲取外匯實時盤口
depth = client.get_forex_depth("GB", "EURUSD")
print("Forex Depth:", depth)
# 獲取外匯歷史K線
kline = client.get_forex_kline("GB", "EURUSD", 2, 10)
print("Forex Kline:", kline)
股票數據查詢
# 獲取股票實時成交
tick = client.get_stock_tick("US", "AAPL")
print("Stock Tick:", tick)
# 獲取股票實時報價
quote = client.get_stock_quote("US", "AAPL")
print("Stock Quote:", quote)
# 獲取股票實時盤口
depth = client.get_stock_depth("US", "AAPL")
print("Stock Depth:", depth)
# 獲取股票歷史K線
kline = client.get_stock_kline("US", "AAPL", 2, 10)
print("Stock Kline:", kline)
加密貨幣數據查詢
# 獲取加密貨幣實時成交
tick = client.get_crypto_tick("BA", "BTCUSDT")
print("Crypto Tick:", tick)
# 獲取加密貨幣實時報價
quote = client.get_crypto_quote("BA", "BTCUSDT")
print("Crypto Quote:", quote)
# 獲取加密貨幣實時盤口
depth = client.get_crypto_depth("BA", "BTCUSDT")
print("Crypto Depth:", depth)
# 獲取加密貨幣歷史K線
kline = client.get_crypto_kline("BA", "BTCUSDT", 2, 10)
print("Crypto Kline:", kline)
WebSocket 使用
SDK 提供了增強的 WebSocket 功能,包括自動重連和心跳保持,用戶無需手動管理連接狀態。
設置回調函數
# 設置消息處理器
def on_message(message):
print(f"Received WebSocket message: {message}")
# 設置錯誤處理器
def on_error(error):
print(f"WebSocket error: {error}")
client.set_message_handler(on_message)
client.set_error_handler(on_error)
連接和訂閱
# 連接外匯 WebSocket
client.connect_forex_websocket()
# 發送訂閱消息
client.send_websocket_message('{"action": "subscribe", "codes": ["EURUSD"]}')
# 等待接收消息
import time
time.sleep(10)
# 檢查連接狀態
print(f"WebSocket connected: {client.is_websocket_connected()}")
# 關閉 WebSocket
client.close_websocket()
其他 WebSocket 連接
# 連接股票 WebSocket
client.connect_stock_websocket()
# 連接加密貨幣 WebSocket
client.connect_crypto_websocket()
API 接口列表
基礎 (Basics)
| 方法 | 說明 |
|---|---|
| get_symbol_list | 獲取符號列表 |
| get_symbol_holidays | 獲取節假日信息 |
股票 (Stock)
| 方法 | 說明 |
|---|---|
| get_stock_info | 獲取股票信息 |
| get_stock_ipo | 獲取股票IPO信息 |
| get_stock_split | 獲取股票分拆信息 |
| get_stock_tick | 獲取股票實時成交 |
| get_stock_quote | 獲取股票實時報價 |
| get_stock_depth | 獲取股票實時盤口 |
| get_stock_kline | 獲取股票歷史K線 |
| get_stock_ticks | 獲取股票批量實時成交 |
| get_stock_quotes | 獲取股票批量實時報價 |
| get_stock_depths | 獲取股票批量實時盤口 |
| get_stock_klines | 獲取股票批量歷史K線 |
| connect_stock_websocket | 連接股票 WebSocket |
指數 (Indices)
| 方法 | 說明 |
|---|---|
| get_indices_tick | 獲取指數實時成交 |
| get_indices_quote | 獲取指數實時報價 |
| get_indices_depth | 獲取指數實時盤口 |
| get_indices_kline | 獲取指數歷史K線 |
| get_indices_ticks | 獲取指數批量實時成交 |
| get_indices_quotes | 獲取指數批量實時報價 |
| get_indices_depths | 獲取指數批量實時盤口 |
| get_indices_klines | 獲取指數批量歷史K線 |
| connect_indices_websocket | 連接指數 WebSocket |
期貨 (Futures)
| 方法 | 說明 |
|---|---|
| get_future_tick | 獲取期貨實時成交 |
| get_future_quote | 獲取期貨實時報價 |
| get_future_depth | 獲取期貨實時盤口 |
| get_future_kline | 獲取期貨歷史K線 |
| get_future_ticks | 獲取期貨批量實時成交 |
| get_future_quotes | 獲取期貨批量實時報價 |
| get_future_depths | 獲取期貨批量實時盤口 |
| get_future_klines | 獲取期貨批量歷史K線 |
| connect_future_websocket | 連接期貨 WebSocket |
基金 (Funds)
| 方法 | 說明 |
|---|---|
| get_fund_tick | 獲取基金實時成交 |
| get_fund_quote | 獲取基金實時報價 |
| get_fund_depth | 獲取基金實時盤口 |
| get_fund_kline | 獲取基金歷史K線 |
| get_fund_ticks | 獲取基金批量實時成交 |
| get_fund_quotes | 獲取基金批量實時報價 |
| get_fund_depths | 獲取基金批量實時盤口 |
| get_fund_klines | 獲取基金批量歷史K線 |
| connect_fund_websocket | 連接基金 WebSocket |
外匯 (Forex)
| 方法 | 說明 |
|---|---|
| get_forex_tick | 獲取外匯實時成交 |
| get_forex_quote | 獲取外匯實時報價 |
| get_forex_depth | 獲取外匯實時盤口 |
| get_forex_kline | 獲取外匯歷史K線 |
| get_forex_ticks | 獲取外匯批量實時成交 |
| get_forex_quotes | 獲取外匯批量實時報價 |
| get_forex_depths | 獲取外匯批量實時盤口 |
| get_forex_klines | 獲取外匯批量歷史K線 |
| connect_forex_websocket | 連接外匯 WebSocket |
加密貨幣 (Crypto)
| 方法 | 說明 |
|---|---|
| get_crypto_tick | 獲取加密貨幣實時成交 |
| get_crypto_quote | 獲取加密貨幣實時報價 |
| get_crypto_depth | 獲取加密貨幣實時盤口 |
| get_crypto_kline | 獲取加密貨幣歷史K線 |
| get_crypto_ticks | 獲取加密貨幣批量實時成交 |
| get_crypto_quotes | 獲取加密貨幣批量實時報價 |
| get_crypto_depths | 獲取加密貨幣批量實時盤口 |
| get_crypto_klines | 獲取加密貨幣批量歷史K線 |
| connect_crypto_websocket | 連接加密貨幣 WebSocket |
WebSocket 功能說明
自動重連
SDK 內置自動重連機制,當網絡異常或連接斷開時,會自動嘗試重新連接:
- 重連間隔:5 秒
- 最大重連次數:10 次
- 重連成功後自動恢復訂閱
心跳保持
SDK 自動維護 WebSocket 連接的心跳:
- 心跳間隔:30 秒
- 自動發送 ping 消息保持連接活躍
連接狀態檢查
# 檢查 WebSocket 是否連接
connected = client.is_websocket_connected()
完整示例
from itick.sdk import Client
import time
# 初始化客戶端
token = "your_api_token"
client = Client(token)
# 設置 WebSocket 消息處理器
def on_message(message):
print(f"Received WebSocket message: {message}")
# 設置 WebSocket 錯誤處理器
def on_error(error):
print(f"WebSocket error: {error}")
client.set_message_handler(on_message)
client.set_error_handler(on_error)
# 測試 REST API
tick = client.get_forex_tick("GB", "EURUSD")
print("Forex Tick:", tick)
# 測試 WebSocket
try:
client.connect_forex_websocket()
# 發送訂閱消息
client.send_websocket_message('{"action": "subscribe", "codes": ["EURUSD"]}')
# 等待接收消息
print("Waiting for WebSocket messages...")
time.sleep(10)
# 檢查連接狀態
print(f"WebSocket connected: {client.is_websocket_connected()}")
finally:
# 關閉 WebSocket
client.close_websocket()