157 lines
4.8 KiB
Python
157 lines
4.8 KiB
Python
|
|
"""
|
||
|
|
QTrade Socket服务器启动脚本 - 简化版
|
||
|
|
专注于同步消息处理和网格策略
|
||
|
|
"""
|
||
|
|
|
||
|
|
import time
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
|
||
|
|
# 添加项目根目录到Python路径
|
||
|
|
sys.path.append(
|
||
|
|
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
|
|
)
|
||
|
|
|
||
|
|
from src.strategy.grid_strategy import GridStrategy
|
||
|
|
from src.network_model.simple_socket_server import SimpleQTradeSocketServerManager
|
||
|
|
|
||
|
|
# 全局策略实例
|
||
|
|
strategy = GridStrategy(
|
||
|
|
upper_price=8.0,
|
||
|
|
lower_price=1.0,
|
||
|
|
grid_step=0.04,
|
||
|
|
quantity_per_grid=1200,
|
||
|
|
trend="up_and_down",
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def handle_message(message):
|
||
|
|
"""同步消息处理器 - 包含网格策略"""
|
||
|
|
try:
|
||
|
|
# 处理字符串消息
|
||
|
|
if isinstance(message, str):
|
||
|
|
if message == "ping":
|
||
|
|
return {"type": "response", "data": "pong", "request_id": None}
|
||
|
|
elif message == "status":
|
||
|
|
return {
|
||
|
|
"type": "server_status",
|
||
|
|
"status": "online",
|
||
|
|
"strategy": f"GridStrategy({strategy.upper_price}-{strategy.lower_price})",
|
||
|
|
"timestamp": time.time(),
|
||
|
|
"request_id": None,
|
||
|
|
}
|
||
|
|
else:
|
||
|
|
return {
|
||
|
|
"type": "response",
|
||
|
|
"data": f"Unknown command: {message}",
|
||
|
|
"request_id": None,
|
||
|
|
}
|
||
|
|
|
||
|
|
# 处理字典消息
|
||
|
|
elif isinstance(message, dict):
|
||
|
|
request_id = message.get("request_id")
|
||
|
|
msg_type = message.get("type", "unknown")
|
||
|
|
|
||
|
|
# K线数据更新 - 使用网格策略
|
||
|
|
if msg_type == "update_kline":
|
||
|
|
data = message.get("data", {})
|
||
|
|
close_price = data.get("close", 0)
|
||
|
|
|
||
|
|
if close_price <= 0:
|
||
|
|
return {
|
||
|
|
"type": "error",
|
||
|
|
"error": "Invalid close price",
|
||
|
|
"request_id": request_id,
|
||
|
|
}
|
||
|
|
|
||
|
|
# 使用网格策略计算信号
|
||
|
|
signal = strategy.determine_signal(close_price)
|
||
|
|
|
||
|
|
return {
|
||
|
|
"type": "trade_signal",
|
||
|
|
"signal": signal,
|
||
|
|
"request_id": request_id,
|
||
|
|
}
|
||
|
|
|
||
|
|
# 获取服务器状态
|
||
|
|
elif msg_type == "get_server_status":
|
||
|
|
return {
|
||
|
|
"type": "server_status",
|
||
|
|
"status": "online",
|
||
|
|
"version": "2.0.0-simplified",
|
||
|
|
"protocol": "socket",
|
||
|
|
"strategy": {
|
||
|
|
"type": "GridStrategy",
|
||
|
|
"upper_price": strategy.upper_price,
|
||
|
|
"lower_price": strategy.lower_price,
|
||
|
|
"grid_step": strategy.grid_step,
|
||
|
|
"quantity_per_grid": strategy.quantity_per_grid,
|
||
|
|
},
|
||
|
|
"timestamp": time.time(),
|
||
|
|
"request_id": request_id,
|
||
|
|
}
|
||
|
|
|
||
|
|
# 测试同步消息
|
||
|
|
elif msg_type == "test_sync":
|
||
|
|
return {
|
||
|
|
"type": "sync_response",
|
||
|
|
"message": "Sync message processed successfully",
|
||
|
|
"received_data": message.get("data"),
|
||
|
|
"timestamp": time.time(),
|
||
|
|
"request_id": request_id,
|
||
|
|
}
|
||
|
|
|
||
|
|
# 未知消息类型
|
||
|
|
else:
|
||
|
|
return {
|
||
|
|
"type": "error",
|
||
|
|
"error": f"Unknown message type: {msg_type}",
|
||
|
|
"request_id": request_id,
|
||
|
|
}
|
||
|
|
|
||
|
|
# 其他类型消息
|
||
|
|
else:
|
||
|
|
return {
|
||
|
|
"type": "error",
|
||
|
|
"error": "Unsupported message type",
|
||
|
|
"request_id": None,
|
||
|
|
}
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
return {
|
||
|
|
"type": "error",
|
||
|
|
"error": f"Message processing failed: {str(e)}",
|
||
|
|
"request_id": message.get("request_id")
|
||
|
|
if isinstance(message, dict)
|
||
|
|
else None,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""主函数 - 简化版"""
|
||
|
|
print("🎯 QTrade Socket Server - 极简版 (单客户端同步 + 网格策略)")
|
||
|
|
print("=" * 60)
|
||
|
|
print(
|
||
|
|
f"📊 网格策略配置: {strategy.upper_price}-{strategy.lower_price}, 步长: {strategy.grid_step}"
|
||
|
|
)
|
||
|
|
print("🎯 监听地址: 127.0.0.1:8081 (单客户端)")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
# 创建服务器
|
||
|
|
server = SimpleQTradeSocketServerManager(host="127.0.0.1", port=8081)
|
||
|
|
server.set_message_handler(handle_message)
|
||
|
|
|
||
|
|
# 启动服务器
|
||
|
|
try:
|
||
|
|
server.start()
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
print("\n⚠️ 收到中断信号")
|
||
|
|
server.stop()
|
||
|
|
except Exception as e:
|
||
|
|
print(f"\n❌ 服务器错误: {e}")
|
||
|
|
server.stop()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|