65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
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:
|
|
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} 已暂停交易,跳过卖出")
|
|
continue
|
|
|
|
if current_data[stock].low_limit >= current_data[stock].last_price:
|
|
self.log.warning(f" 股票 {stock} 已触及跌停,跳过卖出")
|
|
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:
|
|
self.log.warning(f"卖出股票 {stock} 失败")
|
|
|
|
def handle(self, context: Any):
|
|
pass
|
|
|
|
def end(self, context: Any):
|
|
pass
|