98 lines
2.7 KiB
Python
98 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
替代的test_server.py - 使用已映射的端口8001
|
|
"""
|
|
|
|
import traceback
|
|
import zmq
|
|
import time
|
|
import sys
|
|
import os
|
|
|
|
print('🚀 启动替代的RPC服务...')
|
|
|
|
# 使用已映射的端口
|
|
RPC_PORT = 8001 # 使用8001端口,这个端口应该已被映射
|
|
|
|
def run_strategy_backtest(strategy_code: str, symbol: str, interval: str, start: int, end: int, **kwargs):
|
|
"""简化的回测函数"""
|
|
try:
|
|
print(f"收到回测请求: {symbol} {start}-{end}")
|
|
|
|
# 这里简化处理,避免复杂的vn.py初始化
|
|
# 实际使用时需要完整的vn.py环境
|
|
|
|
# 模拟回测结果
|
|
result = {
|
|
"statistics": {
|
|
"total_return": 0.05,
|
|
"annual_return": 0.12,
|
|
"max_drawdown": -0.08,
|
|
"sharpe_ratio": 1.2,
|
|
"total_trades": 10,
|
|
"win_rate": 0.6
|
|
},
|
|
"result_df": [],
|
|
"trades": [],
|
|
"message": "✅ 回测成功(模拟结果)"
|
|
}
|
|
|
|
return result
|
|
|
|
except Exception as e:
|
|
return {
|
|
"error": str(e),
|
|
"traceback": traceback.format_exc()
|
|
}
|
|
|
|
# 创建ZMQ context
|
|
context = zmq.Context()
|
|
rep_socket = context.socket(zmq.REP)
|
|
|
|
try:
|
|
rep_socket.bind(f"tcp://0.0.0.0:{RPC_PORT}")
|
|
print(f'✅ RPC服务已启动,端口: {RPC_PORT}')
|
|
print(f' 外部访问: tcp://192.168.2.154:{RPC_PORT}')
|
|
print(' 等待请求...')
|
|
|
|
except Exception as e:
|
|
print(f'❌ 绑定端口失败: {e}')
|
|
print('尝试使用其他端口...')
|
|
|
|
# 尝试其他端口
|
|
for port in [8002, 8003, 8004]:
|
|
try:
|
|
rep_socket.bind(f"tcp://0.0.0.0:{port}")
|
|
RPC_PORT = port
|
|
print(f'✅ 成功绑定到端口: {RPC_PORT}')
|
|
break
|
|
except:
|
|
continue
|
|
|
|
# 处理请求
|
|
while True:
|
|
try:
|
|
# 接收请求
|
|
req = rep_socket.recv_pyobj()
|
|
print(f"收到请求: {req.get('function')}")
|
|
|
|
function_name = req.get("function")
|
|
args = req.get("args", [])
|
|
kwargs = req.get("kwargs", {})
|
|
|
|
if function_name == "run_strategy_backtest":
|
|
result = run_strategy_backtest(*args, **kwargs)
|
|
else:
|
|
result = {"error": f"未知函数: {function_name}"}
|
|
|
|
# 发送响应
|
|
rep_socket.send_pyobj(result)
|
|
print(f"请求处理完成")
|
|
|
|
except Exception as e:
|
|
error_result = {
|
|
"error": str(e),
|
|
"traceback": traceback.format_exc()
|
|
}
|
|
rep_socket.send_pyobj(error_result)
|
|
print(f"处理请求时出错: {e}") |