写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
T字线因为K线形状像英文字母“T”而得名,又称蜻蜓线。T字线是只有下影线没有上影线,或上影线非常短的同价位线。单日的T字线表明在多方打击下,空方已无力杀跌,股价或指数将会继续上升。
倒T字线因为K线形状像倒写的英文字母“T”而得名,倒T字线是只有上影线没有下影线,或下影线非常短的同价位线。单日的倒T字线表明在空方打击下,多方已无力将股价或指数推高,后市可能会下跌。
由于沪深股市实行涨跌停板制度,所以绝大数T字线和倒T字线都是以涨停或跌停收盘的。不论T字线和倒T字线是否是停板,大多都是主力操控股价的结果,因为只有主力才可以做到让股价以自己想要的价格开盘和收盘。在多空双方互不相让、激烈博弈的情况下,仅仅以巧合来解释T字线和倒T字线的成因,是很难让人信服的。
技术特征
1)T字线的开盘价、收盘价和最高价相同,呈“一”字形。
2)T字线的下影线较长,一般没有上影线,或上影线非常短,形成T字形状。
3)倒T字线的开盘价、收盘价和最低价相同,呈“一”字形。
4)倒T字线的上影线较长,一般没有下影线,或下影线非常短,形成倒T字形状。
技术含义
1)在股价或指数已有较大涨幅后出现T字线,是见顶信号。
2)在股价或指数已有较大跌幅后出现T字线,是见底信号。
3)在上涨途中出现T字线,后市继续看涨。是持仓信号。
4)在下跌途中出现T字线,后市继续看跌。持币观望。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:T字线和倒T字线
识别:
1. T字线只有下影线没有上影线,或上影线非常短的同价位线
2. 倒T字线只有上影线没有下影线,或下影线非常短的同价位线
自定义:
1. 影线很短=》不超过上一交易日价格 0.5%
2. 影线长 =》超过上一交易日价格 2%
前置条件:计算时间区间 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.loc[df['closePrice']>=df['openPrice'],'type'] = 1
df.loc[df['closePrice']<df['openPrice'],'type'] = -1
df['body_length'] = abs(df['closePrice'] - df['openPrice'])
df.loc[df['type']==1,'top_shadow_length'] = df['highestPrice'] - df['closePrice']
df.loc[df['type']==-1,'top_shadow_length'] = df['highestPrice'] - df['openPrice']
df.loc[df['type']==1,'bottom_shadow_length'] = df['openPrice'] - df['lowestPrice']
df.loc[df['type']==-1,'bottom_shadow_length'] = df['closePrice'] - df['lowestPrice']
df['signal'] = 0
df['signal_name'] = ''
short_len = 0.005
long_len = 0.02
# T
df.loc[(df['body_length']==0) & (df['top_shadow_length']/df['closePrice'].shift(1)<short_len) & (df['bottom_shadow_length']/df['closePrice'].shift(1)>long_len),'signal'] = 1
df.loc[(df['body_length']==0) & (df['top_shadow_length']/df['closePrice'].shift(1)<short_len) & (df['bottom_shadow_length']/df['closePrice'].shift(1)>long_len),'signal_name'] = 'T'
# 倒T
df.loc[(df['body_length'] == 0) & (df['bottom_shadow_length'] / df['closePrice'].shift(1) < short_len) & (
df['top_shadow_length'] / df['closePrice'].shift(1) > long_len), 'signal'] = 1
df.loc[(df['body_length'] == 0) & (df['bottom_shadow_length'] / df['closePrice'].shift(1) < short_len) & (
df['top_shadow_length'] / df['closePrice'].shift(1) > long_len), 'signal_name'] = '倒T'
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':'line'
}
return line_data