60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
from base.base_task import base_task
|
|
from kit.kit import DataHelper
|
|
from task.process_pending_sells_func_task import process_pending_sells_func_task
|
|
|
|
|
|
class check_high_volume_func_task(base_task):
|
|
"""
|
|
检查持仓股票当日成交量是否异常放量:
|
|
如果当日成交量大于过去 HV_duration 天内最大成交量的 HV_ratio 倍,则视为异常,执行卖出操作。
|
|
|
|
参数:
|
|
context: 聚宽平台传入的交易上下文对象
|
|
"""
|
|
|
|
def __init__(self, strategy, sub_task=None):
|
|
super().__init__(strategy, sub_task)
|
|
|
|
def init(self, context):
|
|
self.name = "check_high_volume_func_task"
|
|
self.remark = "检查高成交量任务"
|
|
self.memo = "检查是否有异常成交量的股票"
|
|
|
|
def begin(self, context):
|
|
self.HV_control = bool(self.config['HV_control'])
|
|
self.HV_duration = int(self.config['HV_duration'])
|
|
self.HV_ratio = float(self.config['HV_ratio'])
|
|
|
|
def run(self, context):
|
|
current_data = DataHelper.get_current_data()
|
|
for stock in list(context.portfolio.positions.keys()):
|
|
if current_data[stock].paused:
|
|
continue
|
|
if current_data[stock].last_price == current_data[stock].high_limit:
|
|
continue
|
|
if context.portfolio.positions[stock].closeable_amount == 0:
|
|
continue
|
|
df_volume = get_bars(
|
|
stock,
|
|
count=self.HV_duration,
|
|
unit='1d',
|
|
fields=['volume'],
|
|
include_now=True,
|
|
df=True
|
|
)
|
|
if df_volume is not None and not df_volume.empty:
|
|
if df_volume['volume'].iloc[-1] > self.HV_ratio * df_volume['volume'].max():
|
|
self.log.info(f"检测到股票 {stock} 出现异常放量,执行卖出操作。")
|
|
# 通过持仓监控器注册卖出请求,而不是直接卖出
|
|
self.position_monitor.register_sell_request(stock, 'high_volume')
|
|
|
|
# 处理所有待卖出请求
|
|
task = process_pending_sells_func_task(self.strategy, self.name)
|
|
task.process(context)
|
|
|
|
def handle(self, context):
|
|
pass
|
|
|
|
def end(self, context):
|
|
pass
|