写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
低档盘旋形又称下档盘旋形,是在下跌趋势中先收出若干横盘的小阴线和小阳线,然后再收出一根跳空向下阴线的K线组合形态。
技术特征
1)出现在下跌趋势中。
2)由若干横盘的小阴线、小阳线和一根跳空向下的阴线组成。
技术含义
低档盘旋形是卖出信号,后市看跌。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:低档盘旋形
识别:
1. 若干横盘的小阴线和小阳线
2. 再收出一根跳空向下阴线
自定义:
1. 若干 =》至少4根小K线
2. 横盘 =》第一根小K线实体向上向下延展分别扩大一倍作为上边界和下边界,后续小K线实体不超出边界
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '2013-01-01'
end_date_str = '2014-01-01'
df = pd.read_csv(daily_file_path,encoding='utf-8')
# 删除停牌的数据
df = df.loc[df['openPrice'] > 0].copy()
df['o_date'] = df['tradeDate']
df['o_date'] = pd.to_datetime(df['o_date'])
df = df.loc[(df['o_date'] >= start_date_str) & (df['o_date']<=end_date_str)].copy()
# 保存未复权收盘价数据
df['close'] = df['closePrice']
# 计算前复权数据
df['openPrice'] = df['openPrice'] * df['accumAdjFactor']
df['closePrice'] = df['closePrice'] * df['accumAdjFactor']
df['highestPrice'] = df['highestPrice'] * df['accumAdjFactor']
df['lowestPrice'] = df['lowestPrice'] * df['accumAdjFactor']
# 开始计算
df['type'] = 0
df.loc[df['closePrice'] > df['openPrice'], 'type'] = 1
df.loc[df['closePrice'] < df['openPrice'], 'type'] = -1
df['body_length'] = abs(df['closePrice']-df['openPrice'])
df['small_yeah'] = 0
df.loc[(df['body_length']/df['closePrice'].shift(1)>0.005) & (df['body_length']/df['closePrice'].shift(1)<0.015),'small_yeah'] = 1
df['gap_down_yeah'] = 0
df.loc[(df['type']==-1) & (df['type'].shift(1)==1) & (df['openPrice']<df['openPrice'].shift(1)),'gap_down_yeah'] = 1
df.loc[(df['type']==-1) & (df['type'].shift(1)==-1) & (df['openPrice']<df['closePrice'].shift(1)),'gap_down_yeah'] = 1
df['target_yeah'] = 0
df.loc[df['gap_down_yeah']==1,'target_yeah'] = 1
df.loc[df['small_yeah']==1,'target_yeah'] = 1
df.reset_index(inplace=True)
df['i_row'] = [i for i in range(len(df))]
df['ext_0'] = df['target_yeah'] - df['target_yeah'].shift(1)
df['ext_1'] = df['target_yeah'] - df['target_yeah'].shift(-1)
df_s = df.loc[df['ext_0']==1].copy()
df_e = df.loc[df['ext_1']==1].copy()
df_clear = df.loc[(df['ext_0']==1) & (df['ext_1']==1)].copy()
df_gap = df.loc[df['gap_down_yeah']==1].copy()
s_row_list = df_s['i_row'].values.tolist()
e_row_list = df_e['i_row'].values.tolist()
if len(s_row_list)>0 and len(e_row_list)>0 and s_row_list[-1]>e_row_list[-1]:
e_row_list.append(len(df)-1)
clear_row_list = df_clear['i_row'].values.tolist()
gap_row_list = df_gap['i_row'].values.tolist()
two_row_list = s_row_list + e_row_list
two_row_list.sort()
start_yeah = False
s_list = []
e_list = []
for item in two_row_list:
if item in clear_row_list:
continue
if start_yeah:
if item in s_row_list:
s_list.append(item)
if item in e_row_list:
e_list.append(item)
pass
else:
if item in s_row_list:
s_list.append(item)
start_yeah = True
pass
s_list00 = []
e_list00 = []
for s,e in zip(s_list,e_list):
if e-s < 5:
continue
temp_g = []
for g in gap_row_list:
if g>e:
break
if g>=s and g<=e:
temp_g.append(g)
pass
if len(temp_g)<=0:
continue
for i,g0 in enumerate(temp_g):
if i == 0:
if g0-s<5:
continue
s_list00.append(s)
e_list00.append(g0)
elif i == len(temp_g)-1:
if e-g0 < 5:
continue
s_list00.append(g0+1)
e_list00.append(e)
else:
if temp_g[i+1]-g0 < 5:
continue
s_list00.append(g0)
e_list00.append(temp_g[i+1])
pass
pass
df['signal'] = 0
df['signal_name'] = ''
for s,e in zip(s_list00,e_list00):
up_point = df.iloc[s]['highestPrice'] + df.iloc[s]['body_length']
down_point = df.iloc[s]['lowestPrice'] - df.iloc[s]['body_length']
enter_yeah = True
for i in range(s,e):
if df.iloc[i]['highestPrice'] > up_point or df.iloc[i]['lowestPrice']<down_point:
enter_yeah = False
break
if enter_yeah:
df.loc[(df['i_row']>=s) & (df['i_row']<=e),'signal'] = 1
df.loc[(df['i_row']>=s) & (df['i_row']<=e),'signal_name'] = str(e-s)
pass
file_name = os.path.basename(daily_file_path)
title_str = file_name.split('.')[0]
line_data = {
'title_str':title_str,
'whole_header':['日期','收','开','高','低'],
'whole_df':df,
'whole_pd_header':['tradeDate','closePrice','openPrice','highestPrice','lowestPrice'],
'start_date_str':start_date_str,
'end_date_str':end_date_str,
'signal_type':'duration_detail',
'duration_len':[],
'temp':len(df.loc[df['signal']==1])
}
return line_data