使用RealTraderManager 处理实盘交易
This commit is contained in:
parent
456e1c0c52
commit
1b462a3044
@ -30,8 +30,7 @@ class Config:
|
|||||||
XT_ACCOUNT = os.environ.get("XT_ACCOUNT", "80391818")
|
XT_ACCOUNT = os.environ.get("XT_ACCOUNT", "80391818")
|
||||||
XT_PATH = os.environ.get("XT_PATH", r'C:\\江海证券QMT实盘_交易\\userdata_mini')
|
XT_PATH = os.environ.get("XT_PATH", r'C:\\江海证券QMT实盘_交易\\userdata_mini')
|
||||||
|
|
||||||
# 新增RealTraderManager配置
|
# RealTraderManager配置
|
||||||
USE_REAL_TRADER_MANAGER = os.environ.get("USE_REAL_TRADER_MANAGER", "True").lower() == "true"
|
|
||||||
RTM_ORDER_TIMEOUT = int(os.environ.get("RTM_ORDER_TIMEOUT", 60)) # 订单超时时间(秒)
|
RTM_ORDER_TIMEOUT = int(os.environ.get("RTM_ORDER_TIMEOUT", 60)) # 订单超时时间(秒)
|
||||||
RTM_MAX_RETRIES = int(os.environ.get("RTM_MAX_RETRIES", 3)) # 最大重试次数
|
RTM_MAX_RETRIES = int(os.environ.get("RTM_MAX_RETRIES", 3)) # 最大重试次数
|
||||||
RTM_USE_MARKET_ORDER = os.environ.get("RTM_USE_MARKET_ORDER", "True").lower() == "true" # 是否使用市价单进行补单
|
RTM_USE_MARKET_ORDER = os.environ.get("RTM_USE_MARKET_ORDER", "True").lower() == "true" # 是否使用市价单进行补单
|
||||||
|
@ -391,34 +391,31 @@ def cancel(entrust_no):
|
|||||||
logger.info(f"Received cancel request for entrust_no={entrust_no}")
|
logger.info(f"Received cancel request for entrust_no={entrust_no}")
|
||||||
try:
|
try:
|
||||||
# 不考虑是否为模拟交易,直接使用实盘
|
# 不考虑是否为模拟交易,直接使用实盘
|
||||||
# 优先使用RealTraderManager
|
# 使用RealTraderManager
|
||||||
if Config.USE_REAL_TRADER_MANAGER:
|
rtm = get_real_trader_manager()
|
||||||
rtm = get_real_trader_manager()
|
# 在RealTraderManager的待处理订单中查找
|
||||||
# 在RealTraderManager的待处理订单中查找
|
found_in_rtm = False
|
||||||
found_in_rtm = False
|
for order in rtm.get_pending_orders():
|
||||||
for order in rtm.get_pending_orders():
|
if str(order['order_id']) == str(entrust_no):
|
||||||
if str(order['order_id']) == str(entrust_no):
|
found_in_rtm = True
|
||||||
found_in_rtm = True
|
# 使用RealTraderManager中的trader进行撤单
|
||||||
# 使用RealTraderManager中的trader进行撤单
|
result = rtm.trader.cancel(entrust_no)
|
||||||
result = rtm.trader.cancel(entrust_no)
|
logger.info(f"通过RealTraderManager撤单结果: {result}")
|
||||||
logger.info(f"通过RealTraderManager撤单结果: {result}")
|
|
||||||
|
# 更新订单状态
|
||||||
# 更新订单状态
|
rtm.check_pending_orders()
|
||||||
rtm.check_pending_orders()
|
return jsonify({"success": True, "data": result, "simulation": False}), 200
|
||||||
return jsonify({"success": True, "data": result, "simulation": False}), 200
|
|
||||||
|
# 如果RealTraderManager中未找到,使用普通实盘撤单
|
||||||
|
if not found_in_rtm:
|
||||||
|
logger.info(f"在RealTraderManager中未找到订单{entrust_no},使用普通实盘撤单")
|
||||||
|
real_trader = get_real_trader()
|
||||||
|
result = real_trader.cancel(entrust_no)
|
||||||
|
logger.info(f"普通实盘撤单结果: {result}")
|
||||||
|
|
||||||
# 如果RealTraderManager中未找到,执行下面的普通实盘撤单
|
# 更新未完成委托状态
|
||||||
if not found_in_rtm:
|
StrategyPositionManager.update_pending_orders(real_trader)
|
||||||
logger.info(f"在RealTraderManager中未找到订单{entrust_no},使用普通实盘撤单")
|
return jsonify({"success": True, "data": result, "simulation": False}), 200
|
||||||
|
|
||||||
# 普通实盘撤单方式
|
|
||||||
real_trader = get_real_trader()
|
|
||||||
result = real_trader.cancel(entrust_no)
|
|
||||||
logger.info(f"普通实盘撤单结果: {result}")
|
|
||||||
|
|
||||||
# 更新未完成委托状态
|
|
||||||
StrategyPositionManager.update_pending_orders(real_trader)
|
|
||||||
return jsonify({"success": True, "data": result, "simulation": False}), 200
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing cancel request: {str(e)}")
|
logger.error(f"Error processing cancel request: {str(e)}")
|
||||||
@ -524,9 +521,9 @@ def clear_strategy(strategy_name):
|
|||||||
# 判断当前交易模式
|
# 判断当前交易模式
|
||||||
should_simulate, _ = should_use_simulation()
|
should_simulate, _ = should_use_simulation()
|
||||||
|
|
||||||
# 如果是实盘模式且启用了RealTraderManager
|
# 如果是实盘模式,使用RealTraderManager
|
||||||
if not should_simulate and Config.USE_REAL_TRADER_MANAGER:
|
if not should_simulate:
|
||||||
# 先尝试清除RealTraderManager中的策略目标
|
# 清除RealTraderManager中的策略目标
|
||||||
rtm = get_real_trader_manager()
|
rtm = get_real_trader_manager()
|
||||||
if strategy_name in rtm.strategy_targets:
|
if strategy_name in rtm.strategy_targets:
|
||||||
with _instance_lock: # 使用锁保护操作
|
with _instance_lock: # 使用锁保护操作
|
||||||
@ -607,8 +604,8 @@ def get_order_status():
|
|||||||
# 判断当前交易模式
|
# 判断当前交易模式
|
||||||
should_simulate, _ = should_use_simulation()
|
should_simulate, _ = should_use_simulation()
|
||||||
|
|
||||||
if not should_simulate and Config.USE_REAL_TRADER_MANAGER:
|
if not should_simulate:
|
||||||
# 实盘 + RealTraderManager模式
|
# 实盘模式,使用RealTraderManager
|
||||||
try:
|
try:
|
||||||
rtm = get_real_trader_manager()
|
rtm = get_real_trader_manager()
|
||||||
pending_orders = rtm.get_pending_orders()
|
pending_orders = rtm.get_pending_orders()
|
||||||
@ -620,18 +617,26 @@ def get_order_status():
|
|||||||
logger.error(f"从RealTraderManager获取订单状态时出错: {str(e)}")
|
logger.error(f"从RealTraderManager获取订单状态时出错: {str(e)}")
|
||||||
# 发生错误时,回退到使用普通交易实例
|
# 发生错误时,回退到使用普通交易实例
|
||||||
logger.info("回退到使用普通交易实例获取订单状态")
|
logger.info("回退到使用普通交易实例获取订单状态")
|
||||||
|
|
||||||
# 模拟交易或实盘但未使用RealTraderManager
|
trader = get_real_trader()
|
||||||
trader = get_sim_trader() if should_simulate else get_real_trader()
|
try:
|
||||||
try:
|
entrusts = execute_with_timeout(trader.get_today_entrust, Config.TRADE_TIMEOUT)
|
||||||
entrusts = execute_with_timeout(trader.get_today_entrust, Config.TRADE_TIMEOUT)
|
if entrusts is None:
|
||||||
if entrusts is None:
|
logger.error("获取今日委托超时")
|
||||||
logger.error("获取今日委托超时")
|
return jsonify({"success": False, "error": "获取今日委托超时", "simulation": False}), 500
|
||||||
return jsonify({"success": False, "error": "获取今日委托超时", "simulation": should_simulate}), 500
|
return jsonify({"success": True, "data": entrusts, "simulation": False}), 200
|
||||||
return jsonify({"success": True, "data": entrusts, "simulation": should_simulate}), 200
|
except Exception as e:
|
||||||
except Exception as e:
|
logger.error(f"获取今日委托时出错: {str(e)}")
|
||||||
logger.error(f"获取今日委托时出错: {str(e)}")
|
return jsonify({"success": False, "error": f"获取今日委托时出错: {str(e)}", "simulation": False}), 500
|
||||||
return jsonify({"success": False, "error": f"获取今日委托时出错: {str(e)}", "simulation": should_simulate}), 500
|
else:
|
||||||
|
# 模拟交易模式
|
||||||
|
trader = get_sim_trader()
|
||||||
|
try:
|
||||||
|
entrusts = trader.get_today_entrust()
|
||||||
|
return jsonify({"success": True, "data": entrusts, "simulation": True}), 200
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取今日委托时出错: {str(e)}")
|
||||||
|
return jsonify({"success": False, "error": f"获取今日委托时出错: {str(e)}", "simulation": True}), 500
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"处理订单状态请求时出错: {str(e)}")
|
logger.error(f"处理订单状态请求时出错: {str(e)}")
|
||||||
abort(500, description="Internal server error")
|
abort(500, description="Internal server error")
|
||||||
@ -647,14 +652,11 @@ def get_strategy_targets():
|
|||||||
# 获取查询参数
|
# 获取查询参数
|
||||||
strategy_name = request.args.get("strategy_name")
|
strategy_name = request.args.get("strategy_name")
|
||||||
|
|
||||||
# 检查是否是实盘模式且使用RealTraderManager
|
# 检查是否是实盘模式
|
||||||
should_simulate, _ = should_use_simulation()
|
should_simulate, _ = should_use_simulation()
|
||||||
|
|
||||||
if should_simulate:
|
if should_simulate:
|
||||||
return jsonify({"success": False, "error": "模拟交易模式下不支持目标持仓", "simulation": True}), 400
|
return jsonify({"success": False, "error": "模拟交易模式下不支持目标持仓", "simulation": True}), 400
|
||||||
|
|
||||||
if not Config.USE_REAL_TRADER_MANAGER:
|
|
||||||
return jsonify({"success": False, "error": "RealTraderManager未启用,无法获取目标持仓", "simulation": False}), 400
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rtm = get_real_trader_manager()
|
rtm = get_real_trader_manager()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user