fix trader 可能没有login

This commit is contained in:
zhiyong 2025-04-30 13:26:55 +08:00
parent ba62c6b0b8
commit cc85330f29
3 changed files with 44 additions and 6 deletions

View File

@ -40,6 +40,14 @@ class SimulationTrader:
return sim_logger
def is_logged_in(self):
"""检查交易系统是否已经登录
Returns:
bool: True表示已登录模拟交易系统总是返回已登录状态
"""
return True
def login(self):
self.logger.info("模拟交易:登录成功")
return True

View File

@ -65,7 +65,14 @@ def get_trader(use_sim_trader=False):
return SimulationTrader()
# 否则返回真实交易实例
return XtTrader()
trader = XtTrader()
# 检查交易实例是否已登录,如果未登录则进行登录
if not trader.is_logged_in():
logger.info("创建新的XtTrader实例并登录")
trader.login()
return trader
# 配置日志
def setup_logger():
@ -137,7 +144,9 @@ def update_strategy_position(strategy_name, code, direction, amount):
try:
# 获取交易实例持仓情况(不论真实还是模拟)
actual_positions = get_trader().get_positions()
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
actual_positions = current_trader.get_positions()
code_position = next((pos for pos in actual_positions if pos.get('stock_code') == code), None)
# 记录实际持仓总量
@ -172,6 +181,7 @@ def update_pending_orders():
try:
# 获取今日委托
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
today_entrusts = current_trader.get_today_entrust()
# 更新委托状态
@ -380,6 +390,7 @@ def buy():
logger.info(f"Executing buy order: code={code}, price={price}, amount={amount}, strategy_name={strategy_name}")
try:
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
result = execute_with_timeout(current_trader.buy, Config.TRADE_TIMEOUT, code, price, amount)
if result is None:
# 超时时使用模拟交易
@ -481,6 +492,7 @@ def sell():
logger.info(f"Executing sell order: code={code}, price={price}, amount={amount}, strategy_name={strategy_name}")
try:
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
result = execute_with_timeout(current_trader.sell, Config.TRADE_TIMEOUT, code, price, amount)
if result is None:
# 超时时使用模拟交易
@ -539,7 +551,9 @@ def sell():
def cancel(entrust_no):
logger.info(f"Received cancel request for entrust_no={entrust_no}")
try:
result = get_trader().cancel(entrust_no)
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
result = current_trader.cancel(entrust_no)
logger.info(f"Cancel result: {result}")
# 如果取消成功从pending_orders中移除该订单
@ -560,7 +574,9 @@ def get_balance():
"""Get the balance of the account."""
logger.info("Received balance request")
try:
balance = get_trader().get_balance()
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
balance = current_trader.get_balance()
logger.info(f"Balance: {balance}")
response = {"success": True, "data": balance}
@ -586,6 +602,7 @@ def get_positions():
# 获取真实账户持仓,用于计算可交易量
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
real_positions = current_trader.get_positions()
real_positions_map = {}
for pos in real_positions:
@ -621,6 +638,7 @@ def get_positions():
# 否则返回原始持仓
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
positions = current_trader.get_positions()
logger.info(f"Positions: {positions}")
@ -636,7 +654,9 @@ def get_today_trades():
"""Get the today's trades of the account."""
logger.info("Received today trades request")
try:
trades = get_trader().get_today_trades()
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
trades = current_trader.get_today_trades()
logger.info(f"Today trades: {trades}")
response = {"success": True, "data": trades}
@ -651,7 +671,9 @@ def get_today_entrust():
"""Get the today's entrust of the account."""
logger.info("Received today entrust request")
try:
entrust = get_trader().get_today_entrust()
current_trader = get_trader()
# get_trader 已经确保交易实例是登录状态的,无需再次检查
entrust = current_trader.get_today_entrust()
logger.info(f"Today entrust: {entrust}")
response = {"success": True, "data": entrust}

View File

@ -68,6 +68,14 @@ class XtTrader:
self.connected = False
self.subscribed = False
def is_logged_in(self):
"""检查交易系统是否已经登录
Returns:
bool: True表示已登录False表示未登录
"""
return self.started and self.connected and self.subscribed
def login(self):
if not self.started:
self.xt_trader.start()