写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
解说
向下加速度线是指下跌过程中,股价或指数跌幅越来越大的K线组合。
技术特征
1)出现在下跌行情中。
2)下跌开始时跌速比较缓慢,然后下跌速度越来越快,连续收出中阴线或大阴线。
技术含义
向下加速度线是见底信号。
K线形态策略代码
def excute_strategy(daily_file_path):
'''
名称:向下加速度线
识别:股价或指数跌幅越来越大的K线组合
自定义:
1. 连续检测5根K线,都为阴线
2. 每连续两根K线,后一根开盘价低于前一根开盘价,收盘价低于前一根收盘价
3. 连续5根,从前到后,跌幅越来越大
4. 第四根为中阴线或长阴线,第五根为长阴线
前置条件:计算时间区间 2021-01-01 到 2022-01-01
:param daily_file_path: 股票日数据文件路径
:return:
'''
import pandas as pd
import os
start_date_str = '2011-01-01'
end_date_str = '2012-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['big_body_yeah'] = 0
df.loc[(df['type']==-1) & (df['body_length']/df['closePrice'].shift(1)>0.04),'big_body_yeah'] = 1
df['median_body_yeah'] = 0
df.loc[(df['type']==-1) & (df['body_length']/df['closePrice'].shift(1)>0.02),'median_body_yeah'] = 1
df['five_negative_yeah'] = 0
df.loc[(df['type']==-1) & (df['type'].shift(1)==-1) & (df['type'].shift(2)==-1) & (df['type'].shift(3)==-1) & (df['type'].shift(4)==-1),'five_negative_yeah'] = 1
df['close_pct'] = df['closePrice'] - df['closePrice'].shift(1)
df['five_pct_yeah'] = 0
df.loc[(df['close_pct']<df['close_pct'].shift(1)) & (df['close_pct'].shift(1)<df['close_pct'].shift(2)) & (df['close_pct'].shift(2)<df['close_pct'].shift(3)) & (df['close_pct'].shift(3)<df['close_pct'].shift(4)),'five_pct_yeah'] = 1
df['five_fall_yeah'] = 0
df.loc[(df['openPrice']<df['openPrice'].shift(1)) & (df['closePrice']<df['closePrice'].shift(1)) & (df['openPrice'].shift(1)<df['openPrice'].shift(2)) & (df['closePrice'].shift(1)<df['closePrice'].shift(2)) & (df['openPrice'].shift(2)<df['openPrice'].shift(3)) & (df['closePrice'].shift(2)<df['closePrice'].shift(3)) & (df['openPrice'].shift(3)<df['openPrice'].shift(4)) & (df['closePrice'].shift(3)<df['closePrice'].shift(4)),'five_fall_yeah'] = 1
df['signal'] = 0
df['signal_name'] = ''
df.loc[(df['five_negative_yeah']==1) & (df['five_pct_yeah']==1) & (df['big_body_yeah']==1) & (df['median_body_yeah'].shift(1)==1) & (df['five_fall_yeah']==1),'signal'] = 1
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':[5],
'temp':len(df.loc[df['signal']==1])
}
return line_data