写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
下跌抵抗是指下降行情中,股价或指数收出若干连续跳空低开的阴线和阳线,其中阳线收盘价也低于前一根K线收盘价。
技术特征
1)出现在下跌途中。
2)由若干阴线和阳线组合而成,但阴线大大多于阳线。
3)连续跳空低开,即使是阳线,其收盘价也比前一根K线的收盘价低。
技术含义
下跌抵抗是卖出信号,后市看跌。
下跌抵抗表明空方占据了绝对优势,后市不容乐观,持筹的交易者要清仓,持币的交易者不应入场抢反弹。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:下跌抵抗
识别:收出若干连续跳空低开的阴线和阳线,其中阳线收盘价也低于前一根K线的收盘价
自定义:
1. 至少4根
前置条件:计算时间区间 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['gap_val'] = 0
df.loc[(df['type']==1) & (df['type'].shift(1)==1),'gap_val'] = df['closePrice'] - df['openPrice'].shift(1)
df.loc[(df['type']==1) & (df['type'].shift(1)==-1),'gap_val'] = df['closePrice'] - df['closePrice'].shift(1)
df.loc[(df['type']==-1) & (df['type'].shift(1)==1),'gap_val'] = df['openPrice'] - df['openPrice'].shift(1)
df.loc[(df['type']==-1) & (df['type'].shift(1)==-1),'gap_val'] = df['openPrice'] - df['closePrice'].shift(1)
df['gap_yeah'] = 0
df.loc[df['gap_val']<0,'gap_yeah'] = 1
df.reset_index(inplace=True)
df['i_row'] = [i for i in range(len(df))]
df['ext_0'] = df['gap_yeah'] - df['gap_yeah'].shift(1)
df['ext_1'] = df['gap_yeah'] - df['gap_yeah'].shift(-1)
df_s = df.loc[df['ext_0']==1].copy()
df_e = df.loc[df['ext_1']==1].copy()
df_clear = df.loc[(df['ext_0']==1) & (df['ext_1']==1)].copy()
s_row_list = df_s['i_row'].values.tolist()
e_row_list = df_e['i_row'].values.tolist()
clear_row_list = df_clear['i_row'].values.tolist()
two_row_list = s_row_list + e_row_list
two_row_list.sort()
s_list = []
e_list = []
start_yeah = False
for item in two_row_list:
if item in clear_row_list:
continue
if start_yeah:
if item in s_row_list:
s_list.append(item)
if item in e_row_list:
e_list.append(item)
else:
if item in s_row_list:
s_list.append(item)
start_yeah = True
pass
df['signal'] = 0
df['signal_name'] = ''
# 发现第一个合理的序列就继续下一对 s e
for s,e in zip(s_list,e_list):
if e-s < 4:
continue
df.loc[(df['i_row']>=s) & (df['i_row']<=e),'signal'] = 1
df.loc[(df['i_row']>=s) & (df['i_row']<=e),'signal_name'] = str(e-s)
pass
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_detail',
'duration_len':[],
'temp':len(df.loc[df['signal']==1])
}
return line_data