主要观点总结
本文详细介绍了首板低开打板策略,包括策略概述、代码解读、选股函数、卖出函数、日期处理函数、过滤函数、每日初始股票池、筛选涨停股票、计算涨停数和连板数、计算股票相对位置等。通过逐段解读代码,帮助读者更好地理解该策略的实现过程和运作原理。
关键观点总结
关键观点1: 策略概述
首板低开打板策略是一种在股票市场中寻找首次涨停后低开机会的策略。其核心思想是利用股票首次涨停后的低开机会进行买入,并在特定时间点进行止盈或止损卖出。
关键观点2: 代码解读
通过对代码的逐段解读,可以更好地理解首板低开打板策略的实现过程。包括选股、卖出、日期处理、过滤、每日初始股票池、筛选涨停股票、计算涨停数和连板数、计算股票相对位置等函数的详细解读。
关键观点3: 策略风险
首板低开打板策略虽然在某些市场环境下可能表现不错,但也存在较高的市场风险和技术风险。在实际操作中,建议进行充分的回测和风险评估,谨慎实施。
正文
run_daily(sell, '11:28')
和
run_daily(sell, '14:50')
:在每天的11:28和14:50执行卖出操作。
选股函数
def buy(context):
# 基础信息
date = transform_date(context.previous_date, 'str')
current_data = get_current_data()
# 昨日涨停列表
initial_list = prepare_stock_list(date)
hl_list = get_hl_stock(initial_list, date)
if len(hl_list) != 0:
# 获取非连板涨停的股票
ccd = get_continue_count_df(hl_list, date, 10)
lb_list = list(ccd.index)
stock_list = [s for s in hl_list if s not in lb_list]
# 计算相对位置
rpd = get_relative_position_df(stock_list, date, 60)
rpd = rpd[rpd['rp'] <= 0.5]
stock_list = list(rpd.index)
在选股函数中,我们首先获取了昨日涨停的股票列表,并过滤掉连续涨停的股票。然后,我们计算这些股票的相对位置,并选择开盘价低于昨日收盘价一定比例的股票进行买入。具体步骤如下:
-
获取昨日涨停列表
:通过
prepare_stock_list
和
get_hl_stock
函数获取昨日涨停的股票列表。
-
过滤非连板涨停的股票
:通过
get_continue_count_df
函数计算连板数,并过滤掉连续涨停的股票。
-
计算相对位置
:通过
get_relative_position_df
函数计算股票的相对位置,并选择相对位置较低的股票。
-
选择低开的股票
:计算开盘价与昨日收盘价的比率,选择开盘价低于昨日收盘价一定比例的股票。
-
买入操作
:如果当前没有持仓,则按照等权重买入符合条件的股票。
卖出函数
def sell(context):
# 基础信息
date = transform_date(context.previous_date, 'str')
current_data = get_current_data()
# 根据时间执行不同的卖出策略
if str(context.current_dt)[-8:] == '11:28:00':
for s in list(context.portfolio.positions):
if ((context.portfolio.positions[s].closeable_amount != 0) and (current_data[s].last_price and (current_data[s].last_price > context.portfolio.positions[s].avg_cost)):
print('止盈卖出', [get_security_info(s, date).display_name, s])
print('———————————————————————————————————')
if str(context.current_dt)[-8:] == '14:50:00':
for s in list(context.portfolio.positions):
if ((context.portfolio.positions[s].closeable_amount != 0) and (current_data[s].last_price print('止损卖出'