写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
镊子线是由两根一阴一阳的大K线夹着一根小K线的K线组合。如果第一根K线是阳线,第三根K线就是阴线,且三根K线的最高价基本处在同一水平位置上,经常出现在上涨趋势中。如果第一根K线是阴线,第三根K线就是阳线,且三根K线的最低价基本处在同一水平位置上,经常出现在下降趋势中。
镊子线的形状就像有人拿着镊子小心翼翼地夹着一块小东西。
技术特征
1)既可以出现在上涨趋势中,也可以出现在下跌趋势中。
2)由一阴一阳两根实体较长的K线和一根实体较短的K线组成,其中短K线夹在两根长K线中间。
3)出现在上涨趋势中的镊子线,第一根K线是阳线,第三根K线是阴线;出现在下跌趋势中的镊子线,第一根K线是阴线,第三根K线是阳线。
4)三根K线实体的最高价或最低价几乎在一个水平位置上。
技术含义
1)股价或指数大幅下跌后出现镊子线,止跌回升的可能性较大。
2)股价或紫苏大幅上涨后出现镊子线,见顶回落的可能性较大。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:镊子线
识别:
1. 第一根K线是阳线,第三根K线就是阴线,且三根K线的最高价基本处在同一水平位置上
2. 第一根K线是阴线,第三根K线就是阳线,且三根K线的最低价基本处在同一水平位置上
自定义:
1. 第一根和第三根K线定义为大阳线或大阴线,实体长度是前一交易日价格的4%以上
2. 中间K线是小阳线或小阴线
3. 同一水平位置=》以第一根为标杆,参差不超过0.5%
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '1992-01-01'
end_date_str = '1993-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
body_len = 0.04
df['body_length'] = abs(df['closePrice'] - df['openPrice'])
df['big_body_yeah'] = 0
df.loc[df['body_length']/df['closePrice'].shift(1)>=body_len,'big_body_yeah'] = 1
df['big_type'] = 0
df.loc[(df['type']==1) & (df['big_body_yeah']==1),'big_type'] = 1
df.loc[(df['type']==-1) & (df['big_body_yeah']==1),'big_type'] = -1
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['horizonal_yeah'] = 0
df.loc[(abs(df['highestPrice']-df['highestPrice'].shift(1))/df['highestPrice'].shift(2)<=0.005) & (abs(df['highestPrice'].shift(1)-df['highestPrice'].shift(2))/df['highestPrice'].shift(2)<=0.005),'horizonal_yeah'] = 1
df['signal'] = 0
df['signal_name'] = ''
df.loc[(df['big_type'].shift(2)==1) & (df['small_yeah'].shift(1)==1) & (df['big_type']==-1) & (df['horizonal_yeah']==1),'signal'] = 1
df.loc[(df['big_type'].shift(2)==1) & (df['small_yeah'].shift(1)==1) & (df['big_type']==-1) & (df['horizonal_yeah']==1),'signal_name'] = '阳 短 阴'
df.loc[(df['big_type'].shift(2)==-1) & (df['small_yeah'].shift(1)==1) & (df['big_type']==1) & (df['horizonal_yeah']==1),'signal'] = 1
df.loc[(df['big_type'].shift(2)==-1) & (df['small_yeah'].shift(1)==1) & (df['big_type']==1) & (df['horizonal_yeah']==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':[3],
'temp':len(df.loc[df['signal']==1])
}
return line_data