修改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"
|
||||
|
||||
# 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"
|
||||
|
||||
# Trading hours
|
||||
|
@ -390,20 +390,8 @@ def sell():
|
||||
def cancel(entrust_no):
|
||||
logger.info(f"Received cancel request for entrust_no={entrust_no}")
|
||||
try:
|
||||
# 检查是否为模拟交易
|
||||
should_simulate, simulation_reason = should_use_simulation()
|
||||
|
||||
if should_simulate:
|
||||
# 模拟交易
|
||||
sim_trader = get_sim_trader()
|
||||
result = sim_trader.cancel(entrust_no)
|
||||
logger.info(f"模拟交易撤单结果: {result}")
|
||||
|
||||
# 更新未完成委托状态
|
||||
StrategyPositionManager.update_pending_orders(sim_trader)
|
||||
return jsonify({"success": True, "data": result, "simulation": True}), 200
|
||||
else:
|
||||
# 实盘交易,优先使用RealTraderManager
|
||||
# 不考虑是否为模拟交易,直接使用实盘
|
||||
# 优先使用RealTraderManager
|
||||
if Config.USE_REAL_TRADER_MANAGER:
|
||||
rtm = get_real_trader_manager()
|
||||
# 在RealTraderManager的待处理订单中查找
|
||||
@ -442,17 +430,7 @@ def get_balance():
|
||||
"""Get the balance of the account."""
|
||||
logger.info("Received balance request")
|
||||
try:
|
||||
# 判断当前交易模式
|
||||
should_simulate, _ = should_use_simulation()
|
||||
|
||||
if should_simulate:
|
||||
# 模拟交易
|
||||
trader = get_sim_trader()
|
||||
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)
|
||||
|
||||
@ -511,15 +489,12 @@ def get_today_trades():
|
||||
"""Get the today's trades of the account."""
|
||||
logger.info("Received today trades request")
|
||||
try:
|
||||
# 判断当前交易模式
|
||||
should_simulate, _ = should_use_simulation()
|
||||
|
||||
# 选择相应的交易实例
|
||||
trader = get_sim_trader() if should_simulate else get_real_trader()
|
||||
# 直接使用实盘交易实例,不考虑模拟盘
|
||||
trader = get_real_trader()
|
||||
trades = trader.get_today_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:
|
||||
logger.error(f"Error processing today trades request: {str(e)}")
|
||||
abort(500, description="Internal server error")
|
||||
@ -530,15 +505,12 @@ def get_today_entrust():
|
||||
"""Get the today's entrust of the account."""
|
||||
logger.info("Received today entrust request")
|
||||
try:
|
||||
# 判断当前交易模式
|
||||
should_simulate, _ = should_use_simulation()
|
||||
|
||||
# 选择相应的交易实例
|
||||
trader = get_sim_trader() if should_simulate else get_real_trader()
|
||||
# 直接使用实盘交易实例,不考虑模拟盘
|
||||
trader = get_real_trader()
|
||||
entrust = trader.get_today_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:
|
||||
logger.error(f"Error processing today entrust request: {str(e)}")
|
||||
abort(500, description="Internal server error")
|
||||
|
Loading…
x
Reference in New Issue
Block a user