写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
塔形顶是在上涨过程中收出一根大阳线或中阳线,接着再阳线顶部收出几根小阴线和小阳线,最后收出一根大阴线或中阴线。塔形顶因其形状像一个塔顶而得名。
技术特征
1)出现在上涨趋势中。
2)先是一根大阳线或中阳线,接着在阳线上方收出一连串小阴线、小阳线,最后出现一根大阴线或中阴线。
技术含义
塔形顶是见顶反转信号,后市看跌,卖出。
塔形顶是和塔形底相对的K线形态,表明多方已是强弩之末,无力继续推高股价。大阳线或中阳线之后出现的小阴线和小阳线横盘整理,逐步将多方仅剩的一点能量耗尽,最后一根大阴线或中阴线是空方拉开了大反攻的序幕,也确认了趋势由上涨转为下跌。交易者可在塔形顶收出大阴线或中阴线的当日或次日减仓或清仓。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:塔形顶
识别:上涨过程中收出一根大阳线或中阳线,接着在阳线顶部收出几根小阳线或小阴线,最后收出一根大阴线或中阴线
自定义:
1. 阳线顶部=》最低价在阳线实体底部三分之一以上
2. 最后一根阴线的收盘价不得低于第一根实体顶部的五分之一
3. 最后一根阴线的开盘价不得高于第一根实体底部的三分之一
4. 几根=》至少5根
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '2014-01-01'
end_date_str = '2015-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['m_body_type'] = 0
df.loc[(df['type']==1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'m_body_type'] = 2
df.loc[(df['type']==-1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'m_body_type'] = 4
df['small_type'] = 0
df.loc[df['body_length']/df['closePrice'].shift(1)<0.015,'small_type'] = 1
df.reset_index(inplace=True)
df['i_row'] = [i for i in range(0,len(df))]
df_m_n = df.loc[df['m_body_type']==2].copy()
df_m_p = df.loc[df['m_body_type']==4].copy()
i_row_n = df_m_n['i_row'].values.tolist()
i_row_p = df_m_p['i_row'].values.tolist()
i_row_two = i_row_n + i_row_p
i_row_two.sort()
n_list = []
p_list = []
for i in range(0,len(i_row_two)-1):
if i_row_two[i] in i_row_n and i_row_two[i+1] in i_row_p:
n_list.append(i_row_two[i])
p_list.append(i_row_two[i+1])
pass
df['signal'] = 0
df['signal_name'] = ''
for n,p in zip(n_list,p_list):
if p-n < 5:
continue
enter_yeah = True
for i in range(n+1,p):
if df.iloc[i]['small_type']!=1:
enter_yeah = False
break
pass
if enter_yeah:
final_yeah = False
target_body_length = df.iloc[n]['body_length']
if (df.iloc[n]['openPrice'] + target_body_length*0.1) > df.iloc[p]['closePrice']:
continue
if (df.iloc[n]['closePrice'] - target_body_length*0.1) < df.iloc[p]['openPrice']:
continue
for m_i in range(n+1,p):
if (df.iloc[n]['closePrice'] - target_body_length*0.33) > df.iloc[m_i]['lowestPrice']:
final_yeah = True
break
if final_yeah:
continue
df.loc[(df['i_row']>=n) & (df['i_row']<=p),'signal'] = 1
df.loc[(df['i_row']>=n) & (df['i_row']<=p),'signal_name'] = str(p-n)
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