-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_async.py
More file actions
146 lines (108 loc) · 3.93 KB
/
Copy pathexamples_async.py
File metadata and controls
146 lines (108 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# encoding: utf-8
"""
BinanceQuant 异步 API 使用示例
"""
import asyncio
from envs import Envs
from spot import AsyncSpotClient
from futures import AsyncUmFuturesClient, AsyncCmFuturesClient
from utils import AsyncTaskManager, AsyncRedisClient
from spot.ws_async import AsyncSpotWebSocket
from futures.um_ws_async import AsyncUmFuturesWebSocket
from futures.cm_ws_async import AsyncCmFuturesWebSocket
async def spot_rest_example():
"""现货 REST API 异步示例"""
print("=== 现货 REST API 示例 ===")
client = AsyncSpotClient()
try:
# 获取服务器时间
time = await client.get_spot_server_timestamp()
print(f"Server time: {time}")
# 获取交易对信息
info = await client.get_spot_exchange_info()
print(f"Exchange info: {len(info.get('symbols', []))} symbols")
# 获取价格
ticker = await client.get_spot_ticker_price("BTCUSDT")
print(f"BTCUSDT price: {ticker}")
# 获取 K 线
klines = await client.get_spot_klines("BTCUSDT", "1m", limit=5)
print(f"Klines count: {len(klines)}")
finally:
await client.close()
async def futures_rest_example():
"""期货 REST API 异步示例"""
print("\n=== U 本位期货 REST API 示例 ===")
client = AsyncUmFuturesClient()
try:
# 获取服务器时间
time = await client.get_um_futures_server_timestamp()
print(f"Server time: {time}")
# 获取价格
ticker = await client.get_um_futures_ticker_price("BTCUSDT")
print(f"BTCUSDT price: {ticker}")
# 获取账户信息
account = await client.get_um_futures_account()
print(f"Account: {account}")
finally:
await client.close()
async def websocket_example():
"""WebSocket 异步示例"""
print("\n=== WebSocket 异步示例 ===")
# 初始化环境
Envs.initialize()
await AsyncRedisClient.connect(Envs.RedisUrl)
# 创建任务管理器
manager = AsyncTaskManager()
await manager.start()
# 创建 WebSocket 客户端
spot_ws = AsyncSpotWebSocket(manager)
um_ws = AsyncUmFuturesWebSocket(manager)
try:
# 订阅现货深度
await spot_ws.subscribe_partial_book_depth("BTCUSDT")
print("订阅现货深度: BTCUSDT")
# 订阅 U 本位期货深度
await um_ws.subscribe_partial_book_depth("BTCUSDT")
print("订阅 U 本位期货深度: BTCUSDT")
# 从 Redis 读取数据
await asyncio.sleep(2)
spot_data = await AsyncRedisClient.get("spot_partial_book_BTCUSDT")
um_data = await AsyncRedisClient.get("um_futures_partial_book_BTCUSDT")
if spot_data:
print(f"现货深度数据: {spot_data[:100]}...")
if um_data:
print(f"期货深度数据: {um_data[:100]}...")
# 运行 10 秒
print("\n运行 10 秒...")
await asyncio.sleep(10)
finally:
await manager.stop()
await spot_ws.close()
await um_ws.close()
await AsyncRedisClient.close()
async def concurrent_requests_example():
"""并发请求示例"""
print("\n=== 并发请求示例 ===")
client = AsyncSpotClient()
try:
# 并发获取多个交易对的价格
symbols = ["BTCUSDT", "ETHUSDT", "BNBUSDT", "ADAUSDT", "SOLUSDT"]
tasks = [client.get_spot_ticker_price(s) for s in symbols]
results = await asyncio.gather(*tasks)
for symbol, ticker in zip(symbols, results):
print(f"{symbol}: {ticker}")
finally:
await client.close()
async def main():
"""主函数"""
# 加载环境变量
Envs.load_from_env_file(".env")
# 运行各个示例
await spot_rest_example()
await futures_rest_example()
await concurrent_requests_example()
# WebSocket 示例(可选,需要 Redis)
# await websocket_example()
if __name__ == "__main__":
asyncio.run(main())