修改get_today_entrust, get_today_trades, get_balance, cancel 这几个方法中的对模拟盘的使用
This commit is contained in:
parent
aa08773d5f
commit
456e1c0c52
@ -8,7 +8,7 @@ class Config:
|
|||||||
DEBUG = os.environ.get("DEBUG", "False").lower() == "true"
|
DEBUG = os.environ.get("DEBUG", "False").lower() == "true"
|
||||||
|
|
||||||
# Trading settings
|
# Trading settings
|
||||||
TRADE_TIMEOUT = int(os.environ.get("TRADE_TIMEOUT", 10)) # 交易超时时间(秒)
|
TRADE_TIMEOUT = int(os.environ.get("TRADE_TIMEOUT", 5)) # 交易超时时间(秒)
|
||||||
SIMULATION_ONLY = os.environ.get("SIMULATION_ONLY", "False").lower() == "true"
|
SIMULATION_ONLY = os.environ.get("SIMULATION_ONLY", "False").lower() == "true"
|
||||||
|
|
||||||
# Trading hours
|
# Trading hours
|
||||||
|
@ -390,47 +390,35 @@ def sell():
|
|||||||
def cancel(entrust_no):
|
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:
|
||||||
# 检查是否为模拟交易
|
# 不考虑是否为模拟交易,直接使用实盘
|
||||||
should_simulate, simulation_reason = should_use_simulation()
|
# 优先使用RealTraderManager
|
||||||
|
if Config.USE_REAL_TRADER_MANAGER:
|
||||||
|
rtm = get_real_trader_manager()
|
||||||
|
# 在RealTraderManager的待处理订单中查找
|
||||||
|
found_in_rtm = False
|
||||||
|
for order in rtm.get_pending_orders():
|
||||||
|
if str(order['order_id']) == str(entrust_no):
|
||||||
|
found_in_rtm = True
|
||||||
|
# 使用RealTraderManager中的trader进行撤单
|
||||||
|
result = rtm.trader.cancel(entrust_no)
|
||||||
|
logger.info(f"通过RealTraderManager撤单结果: {result}")
|
||||||
|
|
||||||
|
# 更新订单状态
|
||||||
|
rtm.check_pending_orders()
|
||||||
|
return jsonify({"success": True, "data": result, "simulation": False}), 200
|
||||||
|
|
||||||
|
# 如果RealTraderManager中未找到,执行下面的普通实盘撤单
|
||||||
|
if not found_in_rtm:
|
||||||
|
logger.info(f"在RealTraderManager中未找到订单{entrust_no},使用普通实盘撤单")
|
||||||
|
|
||||||
if should_simulate:
|
# 普通实盘撤单方式
|
||||||
# 模拟交易
|
real_trader = get_real_trader()
|
||||||
sim_trader = get_sim_trader()
|
result = real_trader.cancel(entrust_no)
|
||||||
result = sim_trader.cancel(entrust_no)
|
logger.info(f"普通实盘撤单结果: {result}")
|
||||||
logger.info(f"模拟交易撤单结果: {result}")
|
|
||||||
|
# 更新未完成委托状态
|
||||||
# 更新未完成委托状态
|
StrategyPositionManager.update_pending_orders(real_trader)
|
||||||
StrategyPositionManager.update_pending_orders(sim_trader)
|
return jsonify({"success": True, "data": result, "simulation": False}), 200
|
||||||
return jsonify({"success": True, "data": result, "simulation": True}), 200
|
|
||||||
else:
|
|
||||||
# 实盘交易,优先使用RealTraderManager
|
|
||||||
if Config.USE_REAL_TRADER_MANAGER:
|
|
||||||
rtm = get_real_trader_manager()
|
|
||||||
# 在RealTraderManager的待处理订单中查找
|
|
||||||
found_in_rtm = False
|
|
||||||
for order in rtm.get_pending_orders():
|
|
||||||
if str(order['order_id']) == str(entrust_no):
|
|
||||||
found_in_rtm = True
|
|
||||||
# 使用RealTraderManager中的trader进行撤单
|
|
||||||
result = rtm.trader.cancel(entrust_no)
|
|
||||||
logger.info(f"通过RealTraderManager撤单结果: {result}")
|
|
||||||
|
|
||||||
# 更新订单状态
|
|
||||||
rtm.check_pending_orders()
|
|
||||||
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}")
|
|
||||||
|
|
||||||
# 更新未完成委托状态
|
|
||||||
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)}")
|
||||||
@ -442,26 +430,16 @@ def get_balance():
|
|||||||
"""Get the balance of the account."""
|
"""Get the balance of the account."""
|
||||||
logger.info("Received balance request")
|
logger.info("Received balance request")
|
||||||
try:
|
try:
|
||||||
# 判断当前交易模式
|
# 直接使用实盘交易实例,不考虑模拟盘
|
||||||
should_simulate, _ = should_use_simulation()
|
trader = get_real_trader()
|
||||||
|
balance = execute_with_timeout(trader.get_balance, Config.TRADE_TIMEOUT)
|
||||||
|
|
||||||
if should_simulate:
|
if balance is None:
|
||||||
# 模拟交易
|
logger.error("获取实盘余额超时")
|
||||||
trader = get_sim_trader()
|
return jsonify({"success": False, "error": "获取余额超时,请稍后重试", "simulation": False}), 500
|
||||||
balance = trader.get_balance()
|
|
||||||
logger.info(f"模拟交易余额: {balance}")
|
|
||||||
return jsonify({"success": True, "data": balance, "simulation": True}), 200
|
|
||||||
else:
|
|
||||||
# 实盘交易,添加超时处理
|
|
||||||
trader = get_real_trader()
|
|
||||||
balance = execute_with_timeout(trader.get_balance, Config.TRADE_TIMEOUT)
|
|
||||||
|
|
||||||
if balance is None:
|
logger.info(f"实盘交易余额: {balance}")
|
||||||
logger.error("获取实盘余额超时")
|
return jsonify({"success": True, "data": balance, "simulation": False}), 200
|
||||||
return jsonify({"success": False, "error": "获取余额超时,请稍后重试", "simulation": False}), 500
|
|
||||||
|
|
||||||
logger.info(f"实盘交易余额: {balance}")
|
|
||||||
return jsonify({"success": True, "data": balance, "simulation": False}), 200
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing balance request: {str(e)}")
|
logger.error(f"Error processing balance request: {str(e)}")
|
||||||
abort(500, description="Internal server error")
|
abort(500, description="Internal server error")
|
||||||
@ -511,15 +489,12 @@ def get_today_trades():
|
|||||||
"""Get the today's trades of the account."""
|
"""Get the today's trades of the account."""
|
||||||
logger.info("Received today trades request")
|
logger.info("Received today trades request")
|
||||||
try:
|
try:
|
||||||
# 判断当前交易模式
|
# 直接使用实盘交易实例,不考虑模拟盘
|
||||||
should_simulate, _ = should_use_simulation()
|
trader = get_real_trader()
|
||||||
|
|
||||||
# 选择相应的交易实例
|
|
||||||
trader = get_sim_trader() if should_simulate else get_real_trader()
|
|
||||||
trades = trader.get_today_trades()
|
trades = trader.get_today_trades()
|
||||||
logger.info(f"今日成交: {trades}")
|
logger.info(f"今日成交: {trades}")
|
||||||
|
|
||||||
return jsonify({"success": True, "data": trades, "simulation": should_simulate}), 200
|
return jsonify({"success": True, "data": trades, "simulation": False}), 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing today trades request: {str(e)}")
|
logger.error(f"Error processing today trades request: {str(e)}")
|
||||||
abort(500, description="Internal server error")
|
abort(500, description="Internal server error")
|
||||||
@ -530,15 +505,12 @@ def get_today_entrust():
|
|||||||
"""Get the today's entrust of the account."""
|
"""Get the today's entrust of the account."""
|
||||||
logger.info("Received today entrust request")
|
logger.info("Received today entrust request")
|
||||||
try:
|
try:
|
||||||
# 判断当前交易模式
|
# 直接使用实盘交易实例,不考虑模拟盘
|
||||||
should_simulate, _ = should_use_simulation()
|
trader = get_real_trader()
|
||||||
|
|
||||||
# 选择相应的交易实例
|
|
||||||
trader = get_sim_trader() if should_simulate else get_real_trader()
|
|
||||||
entrust = trader.get_today_entrust()
|
entrust = trader.get_today_entrust()
|
||||||
logger.info(f"今日委托: {entrust}")
|
logger.info(f"今日委托: {entrust}")
|
||||||
|
|
||||||
return jsonify({"success": True, "data": entrust, "simulation": should_simulate}), 200
|
return jsonify({"success": True, "data": entrust, "simulation": False}), 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing today entrust request: {str(e)}")
|
logger.error(f"Error processing today entrust request: {str(e)}")
|
||||||
abort(500, description="Internal server error")
|
abort(500, description="Internal server error")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user