- 实现 REST API 客户端,支持股票、加密货币、外汇、期货、基金和指数数据 - 实现 WebSocket 客户端,支持实时数据订阅 - 添加异常处理、工具函数和常量定义 - 包含安装配置和示例代码 - 添加文档爬虫工具用于生成离线文档
46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
import asyncio
|
|
import logging
|
|
from itick import SocketClient, Environment, Product
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
async def main():
|
|
token = "YOUR_TOKEN"
|
|
|
|
def on_message(msg):
|
|
print(f"Received: {msg}")
|
|
|
|
def on_error(err):
|
|
print(f"Error: {err}")
|
|
|
|
# Create WebSocket client for Crypto
|
|
client = SocketClient(
|
|
token=token,
|
|
product=Product.CRYPTO,
|
|
environment=Environment.FREE,
|
|
on_message=on_message,
|
|
on_error=on_error
|
|
)
|
|
|
|
try:
|
|
await client.connect()
|
|
|
|
# Wait a bit for connection and auth
|
|
await asyncio.sleep(2)
|
|
|
|
# Subscribe
|
|
print("Subscribing...")
|
|
await client.subscribe(symbols=["BTCUSDT$BA"], data_types=["quote", "tick"])
|
|
|
|
# Run for 10 seconds
|
|
await asyncio.sleep(10)
|
|
|
|
except Exception as e:
|
|
print(f"Main Error: {e}")
|
|
finally:
|
|
await client.close()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|