写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
下降三部曲又称降势三鹤,出现在下跌途中,由两根中阴线或大阴线中间夹着三根没有突破第一根阴线开盘价的三根小阳线组成。
技术特征
1)出现在下跌趋势中。
2)由五根大小不等的K线组合而成。
3)在拉出一根中阴线或大阴线后,接着出现三根小阳线,但都没有突破第一根阳线的开盘价。
4)最后一根大阴线或中阴线全部或大部分覆盖了前面三根小阳线。
技术含义
下降三部曲是卖出信号,后市看跌。
下降三部曲的出现,表明多方虽然努力反抗,但微弱的反弹很快就在空方的打击下夭折了,因此后市继续看跌。遇到这种走势,交易者只可卖出,不可买进。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:下降三部曲(降势三鹤)
识别:
1. 由两根中阴线或大阴线中间夹着三根没有突破第一根阴线开盘价的三根小阳线组成
2. 最后一根大阴线或中阴线全部或大部分覆盖了前面三根小阳线
自定义:
1. 全部或大部分覆盖 =》实体上下向外延展实体的10%就可以全覆盖
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '2021-01-01'
end_date_str = '2022-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['big_yeah'] = 0
df.loc[(df['type']==-1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'big_yeah'] = 1
df['small_yeah'] = 0
df.loc[(df['type']==1) & (df['body_length']/df['closePrice'].shift(1)>0.005) & (df['body_length']/df['closePrice'].shift(1)<0.015),'small_yeah'] = 1
df['five_yeah'] = 0
df.loc[(df['big_yeah']==1) & (df['small_yeah'].shift(-1)==1) & (df['small_yeah'].shift(-2)==1) & (df['small_yeah'].shift(-3)==1) & (df['big_yeah'].shift(-4)==1),'five_yeah'] = 1
df['pre_two_yeah'] = 0
df.loc[(df['five_yeah']==1) & (df['openPrice']>df['closePrice'].shift(-1)) & (df['openPrice']>df['closePrice'].shift(-2)) & (df['openPrice']>df['closePrice'].shift(-3)),'pre_two_yeah'] = 1
df['last_two_yeah'] = 0
df['last_two_yeah0'] = 0
df.loc[(df['pre_two_yeah']==1) & (df['openPrice'].shift(-4)+df['body_length'].shift(-4)*0.1>df['closePrice'].shift(-1)) & (df['openPrice'].shift(-4)+df['body_length'].shift(-4)*0.1>df['closePrice'].shift(-2)) & (df['openPrice'].shift(-4)+df['body_length'].shift(-4)*0.1>df['closePrice'].shift(-3)),'last_two_yeah'] = 1
df.loc[(df['last_two_yeah']==1) & (df['closePrice'].shift(-4)-df['body_length'].shift(-4)*0.1<df['closePrice'].shift(-1)) & (df['closePrice'].shift(-4)-df['body_length'].shift(-4)*0.1<df['closePrice'].shift(-2)) & (df['closePrice'].shift(-4)-df['body_length'].shift(-4)*0.1<df['closePrice'].shift(-3)),'last_two_yeah0'] = 1
df['signal'] = 0
df['signal_name'] = ''
df.loc[df['last_two_yeah0'] == 1, 'signal'] = 1
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',
'duration_len':[-3],
'temp':len(df.loc[df['signal']==1])
}
return line_data