easyquant/task/process_pending_sells_task.py

65 lines
2.2 KiB
Python
Raw Normal View History

2025-07-03 23:39:31 +08:00
from typing import Any
from kit.kit import DataHelper
from strategy import trade_strategy
from task.base_task import base_task
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:
2025-07-08 20:25:56 +08:00
self.log.info("没有待卖出股票,跳过处理")
2025-07-03 23:39:31 +08:00
return
self.log.info("开始处理待卖出股票队列")
current_data = DataHelper.get_current_data()
for stock in self.sell_request_list:
2025-07-08 20:25:56 +08:00
self.log.info(f"处理待卖出股票: {stock}")
2025-07-03 23:39:31 +08:00
if stock not in context.portfolio.positions:
# 股票已不在持仓中,可能已通过其他方式卖出
2025-07-08 20:25:56 +08:00
self.log.warning(f" 股票 {stock} 不在当前持仓中,跳过处理")
2025-07-03 23:39:31 +08:00
continue
position = context.portfolio.positions[stock]
if position.closeable_amount <= 0:
# 没有可卖出数量
2025-07-08 20:25:56 +08:00
self.log.warning(f" 股票 {stock} 没有可卖出数量,跳过处理")
2025-07-03 23:39:31 +08:00
continue
# 检查是否可以交易
if current_data[stock].paused:
2025-07-08 20:25:56 +08:00
self.log.warning(f" 股票 {stock} 已暂停交易,跳过卖出")
2025-07-03 23:39:31 +08:00
continue
if current_data[stock].low_limit >= current_data[stock].last_price:
2025-07-08 20:25:56 +08:00
self.log.warning(f" 股票 {stock} 已触及跌停,跳过卖出")
2025-07-03 23:39:31 +08:00
continue
# 尝试卖出
# self.position_monitor.increment_attempt(stock)
success = self.strategy.close_position(context, position)
if success:
# self.position_monitor.mark_as_successful(stock)
self.log.info(f"成功卖出股票 {stock}")
else:
2025-07-08 20:25:56 +08:00
self.log.warning(f"卖出股票 {stock} 失败")
2025-07-03 23:39:31 +08:00
def handle(self, context: Any):
pass
def end(self, context: Any):
pass