-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_position_manager.py
More file actions
92 lines (70 loc) · 2.89 KB
/
test_position_manager.py
File metadata and controls
92 lines (70 loc) · 2.89 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
#!/usr/bin/env python3
"""
测试持仓管理器的缓存和数据获取功能
"""
import asyncio
import logging
from position_manager import PositionManager
# 设置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
async def test_position_manager():
"""测试持仓管理器"""
try:
from hyperliquid.info import Info
from hyperliquid.utils import constants
except ImportError:
print("❌ 未找到 hyperliquid-python-sdk")
return
# 测试地址(示例)
test_address = "0x5d2f4460ac3514ada79f5d9838916e508ab39bb7"
# 创建持仓管理器
manager = PositionManager(Info, constants)
print("\n" + "=" * 80)
print("测试1: 首次获取账户数据(应该从API获取)")
print("=" * 80)
account_data_1 = await manager.get_account_data_async(test_address)
if account_data_1:
print(f"✅ 账户价值: ${account_data_1['account_value']:,.2f}")
print(f"✅ 持仓价值: ${account_data_1['total_position_value']:,.2f}")
print(f"✅ 持仓数量: {len(account_data_1['positions'])}")
print(f"✅ 挂单数量: {len(account_data_1.get('open_orders', []))}")
# 显示PnL
pnl_summary = account_data_1.get('pnl_summary', {})
print(f"✅ Total PnL: ${pnl_summary.get('total_pnl', 0):,.2f}")
print("\n" + "=" * 80)
print("测试2: 再次获取(应该使用缓存)")
print("=" * 80)
account_data_2 = await manager.get_account_data_async(test_address)
if account_data_2:
print(f"✅ 使用缓存数据")
print(f" 账户价值: ${account_data_2['account_value']:,.2f}")
print("\n" + "=" * 80)
print("测试3: 获取持仓前三")
print("=" * 80)
top_positions = await manager.get_top_positions(test_address, top_n=3)
if top_positions:
for idx, pos in enumerate(top_positions, 1):
print(f"{idx}. {pos['coin']} | ${pos['position_value']:,.2f} | PnL: ${pos['unrealized_pnl']:,.2f}")
else:
print("无持仓")
print("\n" + "=" * 80)
print("测试4: 获取挂单前三")
print("=" * 80)
top_orders = await manager.get_top_open_orders(test_address, top_n=3)
if top_orders:
for idx, order in enumerate(top_orders, 1):
print(f"{idx}. {order['coin']} | {order['side']} @ ${order['price']:,.4f} | 价值: ${order['order_value']:,.2f}")
else:
print("无挂单")
print("\n" + "=" * 80)
print("测试5: 强制刷新")
print("=" * 80)
account_data_3 = await manager.get_account_data_async(test_address, force_refresh=True)
if account_data_3:
print(f"✅ 强制刷新完成")
print(f" 账户价值: ${account_data_3['account_value']:,.2f}")
if __name__ == "__main__":
asyncio.run(test_position_manager())