写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解读
黄昏之星是三根K线的组合,第一根是阳线,第三根是深入第一根阳线实体的阴线,第二根是在阳线和阴线上方的小阴线或小阳线。第二根K线是十字线时又叫黄昏十字星,是黄昏之星的特殊形态。黄昏之星、黄昏十字星的阴线和阳线之间也可以是两根小K线。
技术特征
1)出现在上涨趋势中。
2)由三根K线组成,第一根是阳线,第二根是高开小阴线、小阳线或十字线,第三根是阴线。
3)第三根阴线实体深入到第一根阳线实体之内。
技术含义
黄昏之星是见顶反转信号,后市看跌,卖出。
黄昏之星和早晨之星的形态完全相同,只是方向和技术含义相反。黄昏之星的见顶信号强于早晨之星的见底信号,交易者见到后应及时减仓或清仓。
和早晨之星一样,黄昏之星的三根K线也完美地体现了道氏理论定义下的短期趋势逆转过程:第一根阳线是后高高于前高,第二根K线股价滞张整理,第三根阴线是后低低于前低。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:黄昏之星和黄昏十字星
识别:
1. 由三根K线组成,第一根是阳线,第二根是高开小阴线、小阳线或十字线
2. 第三根是阴线,第三根K线实体深入到第一根K线实体之内
3. 中间的小阴线、小阳线或十字线,可以是连续两根
自定义:
1. 第一根为长阳线,第三根实体两端相对第一根实体两端都上下不超过0.5%
2. 中间部分最低价在第一根实体三分之二以上,即两者重合部分不超过第一根实体上三分之二
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '2019-01-01'
end_date_str = '2020-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_position_yeah'] = 0
df.loc[(df['type']==1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'big_position_yeah'] = 1
df['big_negative_yeah'] = 0
df.loc[(df['type']==-1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'big_negative_yeah'] = 1
df['small_yeah'] = 0
df.loc[df['body_length']/df['closePrice'].shift(1)<0.015,'small_yeah'] = 1
df['sort_three_yeah'] = 0
df['sort_four_yeah'] = 0
df.loc[(df['big_position_yeah']==1) & (df['small_yeah'].shift(-1)==1) & (df['big_negative_yeah'].shift(-2)==1),'sort_three_yeah'] = 1
df.loc[(df['big_position_yeah']==1) & (df['small_yeah'].shift(-1)==1) &(df['small_yeah'].shift(-2)==1) & (df['big_negative_yeah'].shift(-3)==1),'sort_four_yeah'] = 1
horizonal_len = 0.01
df['three_horizonal_yeah'] = 0
df.loc[(df['sort_three_yeah']==1) & (abs(df['closePrice']-df['openPrice'].shift(-2))/df['closePrice']<horizonal_len),'three_horizonal_yeah'] = 1
df['four_horizonal_yeah'] = 0
df.loc[(df['sort_four_yeah']==1) & (abs(df['closePrice']-df['openPrice'].shift(-3))/df['closePrice']<horizonal_len),'four_horizonal_yeah'] = 1
df['signal'] = 0
df['signal_name'] = ''
df.loc[(df['three_horizonal_yeah']==1) & (df['closePrice']-df['body_length']*0.66 < df['lowestPrice'].shift(-1)),'signal'] = 1
df.loc[(df['three_horizonal_yeah']==1) & (df['closePrice']-df['body_length']*0.66 < df['lowestPrice'].shift(-1)),'signal_name'] = '三'
df.loc[(df['four_horizonal_yeah']==1) & (df['closePrice']-df['body_length']*0.66 < df['lowestPrice'].shift(-1)) & (df['closePrice']-df['body_length']*0.66 < df['lowestPrice'].shift(-2)),'signal'] = 2
df.loc[(df['four_horizonal_yeah']==1) & (df['closePrice']-df['body_length']*0.66 < df['lowestPrice'].shift(-1)) & (df['closePrice']-df['body_length']*0.66 < df['lowestPrice'].shift(-2)),'signal_name'] = '四'
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':[-1,-2],
'temp':len(df.loc[(df['signal']==1) & (df['signal']==2)])
}
return line_data