写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
技术特征
技术含义
K线形态策略代码
结果
解说
身怀六甲,又称母子线、孕线,因其K线形态好像一个怀胎的孕妇而得名。身怀六甲由实体一长一短的两根K线组成,其中第二根较短的K线实体完全被第一根较长的K线实体包容。当第二根K线是十字线时,称为十字胎。
技术特征
1)既可以出现在上涨趋势中,也可以出现在下跌趋势中。
2)由两根K线组成,第一根较长的K线实体完全包容第二根较短的K线实体。
3)两根K线可以是一阴一阳,也可以是两根阳线或阴线。
4)第二根K线可以是小阴线、小阳线,也可以是十字线。
技术含义
1)大幅上涨后出现身怀六甲是见顶信号,后市看淡。
2)出现大幅下跌后是见底信号。
3)十字胎和身怀六甲的技术含义相同,是身怀六甲中信号最强的K线组合,这是因为单根的十字线本身就具有见顶和见底的意味。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:身怀六甲和十字胎
识别:
1. 第二根K线实体完全被第一根较长的K线实体包容,第二根K线是小阴线、小阳线
2. 第二根K线是十字线
前置条件:计算时间区间 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['second_yeah'] = 0
df.loc[df['body_length']/df['closePrice'].shift(1)<0.015,'second_yeah'] = 1
df['sort_type'] = 0
# 阳 阳
df.loc[(df['type']==1) & (df['type'].shift(1)==1),'sort_type'] = 1
# 阳 阴
df.loc[(df['type'] == 1) & (df['type'].shift(1) == -1), 'sort_type'] = 2
# 阴 阳
df.loc[(df['type'] == -1) & (df['type'].shift(1) == 1), 'sort_type'] = 3
# 阴 阴
df.loc[(df['type'] == -1) & (df['type'].shift(1) == -1), 'sort_type'] = 4
df['signal'] = 0
df['signal_name'] = ''
df.loc[(df['sort_type']==1) & (df['second_yeah']==1) & (df['closePrice']<df['closePrice'].shift(1)) & (df['openPrice']>df['openPrice'].shift(1)),'signal'] = 1
df.loc[(df['sort_type']==1) & (df['second_yeah']==1) & (df['closePrice']<df['closePrice'].shift(1)) & (df['openPrice']>df['openPrice'].shift(1)),'signal_name'] = '阳 阳'
df.loc[(df['sort_type']==2) & (df['second_yeah']==1) & (df['closePrice']<df['openPrice'].shift(1)) & (df['openPrice']>df['closePrice'].shift(1)),'signal'] = 1
df.loc[(df['sort_type']==2) & (df['second_yeah']==1) & (df['closePrice']<df['openPrice'].shift(1)) & (df['openPrice']>df['closePrice'].shift(1)),'signal_name'] = '阳 阴'
df.loc[(df['sort_type']==3) & (df['second_yeah']==1) & (df['openPrice']<df['closePrice'].shift(1)) & (df['closePrice']>df['openPrice'].shift(1)),'signal'] = 1
df.loc[(df['sort_type']==3) & (df['second_yeah']==1) & (df['openPrice']<df['closePrice'].shift(1)) & (df['closePrice']>df['openPrice'].shift(1)),'signal_name'] = '阴 阳'
df.loc[(df['sort_type']==4) & (df['second_yeah']==1) & (df['openPrice']<df['openPrice'].shift(1)) & (df['closePrice']>df['closePrice'].shift(1)),'signal'] = 1
df.loc[(df['sort_type']==4) & (df['second_yeah']==1) & (df['openPrice']<df['openPrice'].shift(1)) & (df['closePrice']>df['closePrice'].shift(1)),'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':[2],
'temp':len(df.loc[df['signal']==1])
}
return line_data