核心功能: - Socket长连接通信架构,性能提升13倍 - 完整的股票数据处理模块,支持xtquant数据源 - MySQL数据库集成,使用Peewee ORM - 网格交易策略实现 - 多客户端并发支持 项目结构: - src/data_process_model/ - 数据处理核心模块 - src/network_model/ - Socket和HTTP通信实现 - src/strategy/ - 交易策略模块 - example_and_demos/ - 示例和演示代码 - test/ - 测试文件 主要特性: - 支持CSV和MySQL双重数据存储 - 自动数据下载和处理流水线 - 实时Socket通信,毫秒级响应 - 完善的错误处理和日志记录 - 模块化设计,易于扩展 性能指标: - 平均延迟: 0.31ms (HTTP: 4.13ms) - 理论QPS: 3242 (HTTP: 242) - 支持50个并发客户端连接
113 lines
3.0 KiB
Python
113 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
简单的Socket服务器和客户端测试
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import time
|
|
import threading
|
|
|
|
# 添加项目根目录到Python路径
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from src.network_model.simple_socket_server import SimpleQTradeSocketServerManager
|
|
from src.network_model.simple_socket_client import SimpleQTradeSocketClient
|
|
|
|
|
|
def handle_message(message):
|
|
"""消息处理器"""
|
|
try:
|
|
if isinstance(message, str):
|
|
if message == "ping":
|
|
return "pong"
|
|
elif message == "status":
|
|
return {"status": "online", "version": "2.0.0"}
|
|
else:
|
|
return f"Echo: {message}"
|
|
|
|
# 处理价格数据
|
|
if isinstance(message, dict) and "type" in message:
|
|
if message["type"] == "kline":
|
|
# 模拟网格策略处理
|
|
price = message.get("price", 0)
|
|
return {
|
|
"type": "trade_signal",
|
|
"signal": {"action": "buy", "qty": 1200, "price": price},
|
|
"processed_price": price,
|
|
}
|
|
|
|
return {"status": "processed", "message": message}
|
|
except Exception as e:
|
|
return {"error": str(e)}
|
|
|
|
|
|
def start_server():
|
|
"""启动服务器"""
|
|
server = SimpleQTradeSocketServerManager(host="127.0.0.1", port=8081)
|
|
server.set_message_handler(handle_message)
|
|
|
|
try:
|
|
print("🚀 启动Socket服务器...")
|
|
server.start()
|
|
except Exception as e:
|
|
print(f"❌ 服务器启动失败: {e}")
|
|
|
|
|
|
def test_client():
|
|
"""测试客户端"""
|
|
time.sleep(1) # 等待服务器启动
|
|
|
|
client = SimpleQTradeSocketClient("127.0.0.1", 8081)
|
|
|
|
if not client.connect():
|
|
print("❌ 客户端连接失败")
|
|
return
|
|
|
|
print("✅ 客户端连接成功")
|
|
|
|
try:
|
|
# 测试1: Ping
|
|
print("\n=== Ping测试 ===")
|
|
ping_response = client.ping()
|
|
print(f"Ping响应: {ping_response}")
|
|
|
|
# 测试2: 状态
|
|
print("\n=== 状态测试 ===")
|
|
status = client.get_server_status()
|
|
print(f"状态响应: {status}")
|
|
|
|
# 测试3: K线数据
|
|
print("\n=== K线数据测试 ===")
|
|
kline_response = client.send_kline_update(5.0)
|
|
print(f"K线响应: {kline_response}")
|
|
|
|
# 测试4: 同步消息
|
|
print("\n=== 同步消息测试 ===")
|
|
sync_response = client.test_sync_message("测试消息")
|
|
print(f"同步响应: {sync_response}")
|
|
|
|
print("\n✅ 所有测试完成")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 测试出错: {e}")
|
|
finally:
|
|
client.disconnect()
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("🎯 QTrade Socket测试")
|
|
print("=" * 50)
|
|
|
|
# 在后台线程启动服务器
|
|
server_thread = threading.Thread(target=start_server, daemon=True)
|
|
server_thread.start()
|
|
|
|
# 运行客户端测试
|
|
test_client()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|