From 196f91d622fc0273f48ec466f8a98c96ce5c4b2d Mon Sep 17 00:00:00 2001 From: summer Date: Tue, 8 Jul 2025 20:25:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task/buy_stocks_func_task.py | 8 ++++---- task/process_pending_buy_task.py | 27 ++++++++++++--------------- task/process_pending_sells_task.py | 15 +++++++-------- test.py | 4 ++-- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/task/buy_stocks_func_task.py b/task/buy_stocks_func_task.py index 896ef4e..9a8b5ee 100644 --- a/task/buy_stocks_func_task.py +++ b/task/buy_stocks_func_task.py @@ -24,7 +24,9 @@ class buy_stocks_func_task(base_task): # 获取当前持仓列表 self.hold_list = self.strategy.state.get_hold_list() self.index_stocks = self.get_config('index_stocks') + # 调仓的目标股票列表排序 self.target_list = DataHelper.get_stock_list(context, self.index_stocks, self.stock_num) + # 限制目标股票列表长度为 stock_num self.temp_target_list = self.target_list[:self.stock_num] # 昨日涨停股票列表 self.yesterday_HL_list: List[str] = self.strategy.state.get_yesterday_high_list() @@ -51,7 +53,7 @@ class buy_stocks_func_task(base_task): # 通过持仓监控器注册卖出请求,而不是直接卖出 self.sell_request(stock, pos, 'rebalance') # self.strategy.state.set_sell_request(stock, pos, 'rebalance') - self.sell_request(stock, pos, 'rebalance') + # self.sell_request(stock, pos, 'rebalance') else: self.log.info(f"调仓决策:继续持有股票 {stock}") @@ -60,12 +62,10 @@ class buy_stocks_func_task(base_task): if buy_targets: for stock in buy_targets: pos = context.portfolio.positions.get(stock) - # self.strategy.state.set_buy_request(stock, pos, 'buy') self.buys_request(stock, pos, 'buy') - self.log.info(f"调仓决策:将 {len(buy_targets)} 只股票加入待买入队列: {buy_targets}") + self.log.info(f"调仓决策:将 {stock} 加入待买入队列") def handle(self, context: Any): - self.strategy.set_target_list(self.temp_target_list) pass def end(self, context: Any): diff --git a/task/process_pending_buy_task.py b/task/process_pending_buy_task.py index e0647c3..3d061d8 100644 --- a/task/process_pending_buy_task.py +++ b/task/process_pending_buy_task.py @@ -17,17 +17,18 @@ class process_pending_buy_task(base_task): self.today_trade_switch = self.get_config('today_trade_switch') self.stock_num = self.get_config('stock_num') self.buy_requests_list = self.strategy.state.get_buy_requests() + self.hold_list = [position.security for position in list(context.portfolio.positions.values())] + self.buy_symbol_money_up_limit = self.get_config('buy_symbol_money_up_limit') + self.buy_symbol_money_total = self.get_config('buy_symbol_money_total') def run(self, context: Any): # 如果在特殊月份(1月/4月)或没有待买入股票,直接返回 if self.today_trade_switch or not self.buy_requests_list: + self.log.info("今日非交易日或没有待买入股票,跳过处理") return self.log.info("开始处理待买入股票队列") current_data = DataHelper.get_current_data() - - # 更新当前持仓列表 - self.hold_list = [position.security for position in list(context.portfolio.positions.values())] position_count = len(self.hold_list) target_num = int(self.stock_num) @@ -40,9 +41,14 @@ class process_pending_buy_task(base_task): # 计算可买入的股票数量和分配资金 buy_count = min(len(self.buy_requests_list), target_num - position_count) if buy_count <= 0: + self.log.info("没有可买入的股票,或持仓已满") return # 计算每只股票可分配的资金 + if context.portfolio.cash < self.buy_symbol_money_total: + self.log.warning(f"可用资金 {context.portfolio.cash:.2f} 元不足,无法满足总资金要求 {self.buy_symbol_money_total} 元") + return + try: value = context.portfolio.cash / buy_count except ZeroDivisionError: @@ -50,15 +56,15 @@ class process_pending_buy_task(base_task): return # 确保每只股票有足够的买入金额 - if value < 5000: # 假设最小买入金额为5000元 + if value < self.buy_symbol_money_up_limit: # 假设最小买入金额为5000元 self.log.warning(f"每只股票分配资金不足: {value:.2f}元,取消本次买入") return # 逐个买入股票 - success_count = 0 for idx, stock in enumerate(self.buy_requests_list[:buy_count]): # 跳过已持仓的股票 if stock in self.hold_list: + self.log.warning(f"股票 {stock} 已在持仓中,跳过买入") continue # 检查是否可交易 @@ -73,21 +79,12 @@ class process_pending_buy_task(base_task): # 执行买入 if self.strategy.open_position(stock, value): self.log.info(f"成功买入股票 {stock},分配资金 {value:.2f}") - # self.position_manager.get_not_buy_again().append(stock) - success_count += 1 + # 删除购买列表 # 如果持仓已满,停止买入 if len(context.portfolio.positions) >= target_num: break - # 清理已处理的股票 - # self.position_manager.pending_buys = self.position_manager.pending_buys[buy_count:] - - # self.log.info(f"本次共成功买入 {success_count} 只股票,待买入队列还剩 {len(self.position_manager.pending_buys)} 只") - - # 输出最新持仓状态 - # self.position_manager.log_status(context, [p.security for p in list(context.portfolio.positions.values())]) - def handle(self, context: Any): pass diff --git a/task/process_pending_sells_task.py b/task/process_pending_sells_task.py index c33728c..dbffaeb 100644 --- a/task/process_pending_sells_task.py +++ b/task/process_pending_sells_task.py @@ -13,36 +13,38 @@ class process_pending_sells_task(base_task): def __init__(self, strategy: trade_strategy): super().__init__(strategy) - - def config(self, context: Any): self.sell_request_list = self.strategy.state.get_sell_requests() def run(self, context: Any): # 如果没有待卖出股票,直接返回 if len(self.sell_request_list) == 0: + self.log.info("没有待卖出股票,跳过处理") return self.log.info("开始处理待卖出股票队列") current_data = DataHelper.get_current_data() for stock in self.sell_request_list: + self.log.info(f"处理待卖出股票: {stock}") if stock not in context.portfolio.positions: # 股票已不在持仓中,可能已通过其他方式卖出 + self.log.warning(f" 股票 {stock} 不在当前持仓中,跳过处理") continue position = context.portfolio.positions[stock] if position.closeable_amount <= 0: # 没有可卖出数量 + self.log.warning(f" 股票 {stock} 没有可卖出数量,跳过处理") continue # 检查是否可以交易 if current_data[stock].paused: - self.log.warning(f"股票 {stock} 已暂停交易,跳过卖出") + self.log.warning(f" 股票 {stock} 已暂停交易,跳过卖出") continue if current_data[stock].low_limit >= current_data[stock].last_price: - self.log.warning(f"股票 {stock} 已触及跌停,跳过卖出") + self.log.warning(f" 股票 {stock} 已触及跌停,跳过卖出") continue # 尝试卖出 @@ -53,10 +55,7 @@ class process_pending_sells_task(base_task): # self.position_monitor.mark_as_successful(stock) self.log.info(f"成功卖出股票 {stock}") else: - self.log.warning(f"卖出股票 {stock} 失败,已累计尝试 {self.position_monitor.pending_sells[stock]['attempts']} 次") - - # 更新日志状态 - self.position_monitor.log_status() + self.log.warning(f"卖出股票 {stock} 失败") def handle(self, context: Any): pass diff --git a/test.py b/test.py index 14f29eb..2063a08 100644 --- a/test.py +++ b/test.py @@ -1338,7 +1338,7 @@ class buy_stocks_func_task(base_task): # 重置当天已买入记录 # self.position_manager.reset_not_buy_again() # 取目标持仓数以内的股票作为调仓目标 - self.log.info(f"每周调仓目标股票: {self.temp_target_list}") + # self.log.info(f"每周调仓目标股票: {self.temp_target_list}") # 遍历当前持仓,若股票不在目标列表,则执行卖出操作 for stock in self.hold_list: @@ -1348,7 +1348,7 @@ class buy_stocks_func_task(base_task): # 通过持仓监控器注册卖出请求,而不是直接卖出 self.sell_request(stock, pos, 'rebalance') # self.strategy.state.set_sell_request(stock, pos, 'rebalance') - self.sell_request(stock, pos, 'rebalance') + # self.sell_request(stock, pos, 'rebalance') else: self.log.info(f"调仓决策:继续持有股票 {stock}")