跳转至

position_manager

持仓管理模块,负责跟踪和管理持仓 优化版本:统一止盈止损判断逻辑,支持模拟交易直接持仓调整

PositionManager

PositionManager()

持仓管理类,负责跟踪和管理持仓

初始化持仓管理器

check_qmt_connection_health

check_qmt_connection_health()

QMT 连接心跳检查 — 供 thread_monitor 的 heartbeat_check 回调使用。

返回 False 时 thread_monitor 会重启持仓监控线程(重启时 init 会 重新调用 connect(),但当前设计下重启线程不重建 PositionManager, 因此这里直接触发主动重连而非依赖线程重启)。

适用范围:仅在实盘 + 未启用 XtQuantManager 时实际探测。 模拟模式和 XtQuantManager 模式均返回 True(由各自模块负责健康检查)。

start_qmt_connect_async

start_qmt_connect_async(reason='manual')

对外提供的异步连接入口(用于模式切换或手动连接)。

increment_data_version

increment_data_version()

递增数据版本号(公开方法,供外部模块调用)

start_sync_thread

start_sync_thread()

启动定时同步线程

stop_sync_thread

stop_sync_thread()

停止定时同步线程

get_all_positions

get_all_positions()

获取所有持仓

get_position

get_position(stock_code)

获取指定股票的持仓 - 修复版本:基于get_all_positions从QMT接口获取最新持仓 🔑 关键修复:使用字典映射避免字段索引依赖

update_position

update_position(stock_code, volume, cost_price, current_price=None, profit_ratio=None, market_value=None, available=None, open_date=None, profit_triggered=None, highest_price=None, stop_loss_price=None, stock_name=None, base_cost_price=None)

更新持仓信息 - 最小修改版本:仅将位置索引改为字典访问

remove_position

remove_position(stock_code)

删除持仓记录

参数: stock_code (str): 股票代码

返回: bool: 是否删除成功

get_data_version_info

get_data_version_info()

获取持仓数据版本信息

mark_data_consumed

mark_data_consumed()

标记持仓数据已被消费

update_all_positions_highest_price

update_all_positions_highest_price()

更新所有持仓的最高价

update_all_positions_price

update_all_positions_price()

更新所有持仓的最新价格

get_account_info

get_account_info()

获取账户信息

calculate_stop_loss_price

calculate_stop_loss_price(cost_price, highest_price, profit_triggered)

计算止损价格 - 统一的止损价格计算逻辑

注意:当profit_triggered=True时,实际计算的是动态止盈价格, 这个价格在首次止盈后作为新的"止损位"来保护已获得的收益

参数: cost_price (float): 成本价 highest_price (float): 历史最高价 profit_triggered (bool): 是否已经触发首次止盈

返回: float: 止损价格

check_add_position_signal

check_add_position_signal(stock_code)

检查补仓信号 - 使用web页面现有参数 启用开关:stopLossBuyEnabled 补仓阈值:stopLossBuy(通过BUY_GRID_LEVELS[1]获取)

修复说明: - 增加止损排除逻辑: 如果亏损达到止损阈值,不执行补仓 - 确保补仓阈值小于止损阈值,避免冲突

参数: stock_code (str): 股票代码

返回: tuple: (信号类型, 详细信息) - ('add_position', {...}) 或 (None, None)

check_trading_signals

check_trading_signals(stock_code, current_price=None)

检查交易信号 - 修复字段映射错乱版本

参数: stock_code (str): 股票代码 current_price (float, optional): 当前价格,如果提供则跳过行情查询以避免重复调用

返回: tuple: (信号类型, 详细信息) - ('stop_loss'/'take_profit_half'/'take_profit_full', {...}) 或 (None, None)

validate_trading_signal

validate_trading_signal(stock_code, signal_type, signal_info, return_reason=False)

交易信号最后验证 - 防止异常信号执行

参数: stock_code (str): 股票代码 signal_type (str): 信号类型 signal_info (dict): 信号详细信息

返回: bool: 是否通过验证 return_reason=True 时返回 (是否通过, 状态, 原因),状态为 passed/blocked/failed

simulate_buy_position

simulate_buy_position(stock_code, buy_volume, buy_price, strategy='simu')

模拟交易:买入股票,支持成本价加权平均计算

参数: stock_code (str): 股票代码 buy_volume (int): 买入数量 buy_price (float): 买入价格 strategy (str): 策略标识

返回: bool: 是否操作成功

simulate_sell_position

simulate_sell_position(stock_code, sell_volume, sell_price, sell_type='partial')

模拟交易:直接调整持仓数据 - 优化版本

参数: stock_code (str): 股票代码 sell_volume (int): 卖出数量 sell_price (float): 卖出价格 sell_type (str): 卖出类型,'partial'(部分卖出)或'full'(全部卖出)

返回: bool: 是否操作成功

initialize_all_positions_data

initialize_all_positions_data()

初始化所有持仓数据 - 重新计算"买后最高"和"动态止损" 复用现有逻辑,支持实盘和模拟交易

mark_profit_triggered

mark_profit_triggered(stock_code)

标记股票已触发首次止盈

start_position_monitor_thread

start_position_monitor_thread()

启动持仓监控线程

stop_position_monitor_thread

stop_position_monitor_thread()

停止持仓监控线程

get_all_positions_with_all_fields

get_all_positions_with_all_fields()

获取所有持仓的所有字段(包括内存数据库中的所有字段)

get_pending_signals

get_pending_signals()

获取待处理的信号 - 增加时效性检查

mark_signal_processed

mark_signal_processed(stock_code)

标记信号已处理 - 增加状态跟踪

clear_all_signals

clear_all_signals(reason='')

清除所有待处理信号

set_stop_profit_enabled

set_stop_profit_enabled(stock_code, enabled)

设置个股「动态止盈止损」开关(内存库 + SQLite 双写)。

对齐 GridTradingManager.set_session_enabled 的开关操作范式:只更新单列, 不触碰 update_position 核心路径。写入后自增数据版本触发前端刷新。

返回 {'success': bool, 'error': str}。

track_order

track_order(stock_code, order_id, signal_type, signal_info)

跟踪新提交的委托单

参数: stock_code (str): 股票代码 order_id (str): 委托单ID signal_type (str): 信号类型 signal_info (dict): 信号详细信息

check_pending_orders_timeout

check_pending_orders_timeout()

检查所有待处理委托单是否超时 在持仓监控线程中定期调用

init_grid_manager

init_grid_manager(trading_executor)

初始化网格交易管理器

双重检查保护: 防止多线程并发调用时重复创建 GridTradingManager 实例。 场景: Web API 延迟初始化 + Flask 多线程模式下,两个请求可能同时 判断 grid_manager is None 并都进入创建流程。

get_position_manager

get_position_manager()

获取PositionManager单例