写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
长十字线是上下影线都很长的同价位线。至于影线多长才算是长十字线,市场上没有一个统一的标准,交易者凭感觉很容易就能区分。
出现长十字线是转势信号。上影线越长,表示卖盘越重;下影线越长,表明买盘越强。
长十字线的收盘价和开盘价相同,表明多空双方最终握手言和,谁也没能以绝对优势压倒对方。但长长的上下影线告诉交易者,平静的开盘和收盘之间曾经经历了一场极其残酷的拉锯战,双方的阵地都曾经易手,甚至有可能数度出现攻防互换。无论是战争还是交易,平衡从来都是短暂的,僵局马上就会打破。谁会取得最终的胜利呢?在此之前,如果其中一方一直在乘胜追击,那么现在突然遭遇如此激烈的抵抗,很显然多空双方的力量对比正在发生变化。因此,长十字线出现在高价位或低价位区域,常常意味着趋势即将出现反转。
技术特征
1)开盘价和收盘价相同。
2)上下影线很长。
3)上涨和下跌过程中都会出现。
技术含义
1)在股价已有较大涨幅后出现,为见顶信号。
2)在股价有较大跌幅后出现长十字线为见底信号。
3)在上涨途中出现长十字线,继续看涨。持仓不动。
4)在下跌途中出现长十字线,继续看跌。持币观望。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:长十字线
识别:上下影线都很长, 同价位
自定义:
1. 影线很长=》超过上一交易日价格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'] = ''
long_len = 0.02
df.loc[(df['body_length']==0) & (df['top_shadow_length']/df['closePrice'].shift(1)>=long_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)>=long_len) & (df['bottom_shadow_length']/df['closePrice'].shift(1)>=long_len),'ext_0'] = (df['top_shadow_length']/df['closePrice'].shift(1))*100
df.loc[(df['body_length']==0) & (df['top_shadow_length']/df['closePrice'].shift(1)>=long_len) & (df['bottom_shadow_length']/df['closePrice'].shift(1)>=long_len),'ext_1'] = (df['bottom_shadow_length']/df['closePrice'].shift(1))*100
df = df.round({'ext_0':5,'ext_1':5})
df['signal_name'] = df['ext_0'].astype('str') + ';' + df['ext_1'].astype('str')
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