Backtrader 量化回测实践(2)—— 16个主要K线形态定义(下)
K线图形中的趋势线和价格走势能够反映市场的整体趋势,比如是否处于上涨或下跌趋势中。
用Backtrader做策略的时候,需要考虑K线形态,作为分析依据。K线的常用形态搜集整理如下:
1、光头光脚大阳线(极端强势);
2、光头光脚大阴线(极端弱势);
3、光头阳线(高价位强势线,先跌后涨型);
4、光头阴线(低价位弱势线,下跌抵抗型);
5、光脚阳线(高价位强势线,上升阻力型);
6、光脚阴线(低价位弱势线,先涨后跌型);
7、大阳线(较为强烈的买势信号,反转试探型);
8、大阴线(较为强烈的卖势信号,弹升试探型);
9、十字线(阳线,阴线);
10、T字线(阳线,阴线);
11、倒T字线(阳线,阴线);
12、一字线(阳线涨停,阴线跌停)。
以上一共16个,应该是常见的K线主要形态。
在策略中需要通过程序定义K线的形态,根据网上的介绍和定义,用dataframe分析。
取一个股票的数据导入到dataframe中,通过定义找到相应的形态并mplfinance绘图展示。
9.一字涨停
(1)代码
#9、一字涨停
# 开盘价等于最高价,收盘价等于开盘价,开盘价等于昨天的收盘价上涨10%
# 开盘价、收盘价、最高价、最低价都相等
signal = []
limit_up = 9.98 # 10%涨幅,如果是科创板、创业板、ST可调
for t_date in df.index :
# 第一天的数据不执行
if t_date > df.index[0] :
if df.loc[t_date,'pct_chg'] >= limit_up and df.loc[t_date,'open'] == df.loc[t_date,'low'] \
and df.loc[t_date,'open'] == df.loc[t_date,'close'] and df.loc[t_date,'open'] == df.loc[t_date,'high'] :
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2009-07-03 19.89 20.15 19.60 19.86
2009-07-06 20.01 20.78 19.88 20.70
2009-07-07 20.59 21.74 20.47 21.14
2009-07-08 20.90 21.23 20.60 20.63
2009-07-13 22.69 22.69 22.69 22.69
2009-07-14 24.96 24.96 23.28 23.63
2009-07-15 23.91 24.17 23.04 23.57
2009-07-16 23.70 24.57 23.28 24.14
2009-07-17 23.98 24.40 23.68 23.90
2009-07-20 23.91 24.22 23.62 23.95
2009-07-21 24.01 24.98 23.81 23.99
2009-07-22 24.01 24.18 23.74 24.07
2009-07-23 24.08 24.33 23.78 23.94
10.一字跌停
(1)代码
#10、一字跌停
# 开盘价等于最高价,收盘价等于开盘价,开盘价等于昨天的收盘价下跌10%
# 开盘价、收盘价、最高价、最低价都相等
signal = []
limit_down = -9.98 # 10%跌幅,如果是科创板、创业板、ST可调
for t_date in df.index :
# 第一天的数据不执行
if t_date > df.index[0] :
if df.loc[t_date,'pct_chg'] < limit_down and df.loc[t_date,'open'] == df.loc[t_date,'low'] \
and df.loc[t_date,'open'] == df.loc[t_date,'close'] and df.loc[t_date,'open'] == df.loc[t_date,'high']:
signal.append(t_date)
# 保存前一天的收盘价做参考
df_yesterday = df.loc[t_date,'close']
mpfplot(signal,df)
(2)图示
说明:2月3日之前是春节,所以第一个就是信号数据。
(3)数据
open high low close
trade_date
2020-02-03 113.54 113.54 113.54 113.54
2020-02-04 109.00 120.00 109.00 114.50
2020-02-05 115.90 119.80 114.10 117.51
2020-02-06 119.00 119.94 116.60 119.15
2020-02-07 119.25 120.93 117.61 120.58
2020-02-10 119.50 122.33 118.58 121.13
2020-02-11 121.15 125.50 121.15 124.79
2020-02-12 124.50 125.25 122.77 124.49
2020-02-13 124.88 126.66 123.37 124.14
11.光头阳线
(1)代码
#11、光头阳线(高价位强势线,先跌后涨型)
# 收盘价等于最高价,收盘价大于开盘价, head / foot > 1/6 and <3
signal = []
head_foot_perc =[1/6, 3] # K线头、足比,可调
for t_date in df.index :
k_head = df.loc[t_date,'high'] - df.loc[t_date,'open']
k_foot = df.loc[t_date,'open'] - df.loc[t_date,'low']
# 无影线的情况不考虑
if k_foot != 0 and df.loc[t_date,'close'] == df.loc[t_date,'high']:
if df.loc[t_date,'close']>df.loc[t_date,'open'] and k_head/k_foot > head_foot_perc[0] and k_head/k_foot < head_foot_perc[1]:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2000-01-07 31.88 33.10 31.86 32.71
2000-01-10 33.10 33.50 32.67 32.81
2000-01-11 32.86 33.06 31.25 31.44
2000-01-12 31.05 32.00 30.60 30.90
2000-01-13 30.80 31.00 30.48 30.50
2000-01-14 30.40 30.70 29.98 30.01
2000-01-17 29.99 30.66 29.61 30.66
2000-01-18 30.80 31.45 30.36 31.15
2000-01-19 31.30 33.28 31.30 32.58
2000-01-20 32.56 33.10 32.10 32.58
2000-01-21 32.60 32.99 32.22 32.65
2000-01-24 32.85 34.35 32.75 33.59
2000-01-25 34.00 34.20 32.65 32.70
2000-01-26 35.95 35.95 32.35 32.67
2000-01-27 32.70 32.88 31.52 31.95
open high low close
trade_date
2000-07-10 33.08 33.60 33.05 33.27
2000-07-11 33.30 33.58 33.05 33.10
2000-07-12 33.20 33.30 32.80 32.85
2000-07-13 32.86 33.00 32.60 32.80
2000-07-14 32.80 33.11 32.50 32.88
2000-07-17 32.88 33.10 32.60 32.61
2000-07-18 32.58 32.80 32.46 32.80
2000-07-19 32.95 33.50 32.95 33.00
2000-07-20 33.10 33.78 33.00 33.14
2000-07-21 33.18 33.50 32.70 32.76
2000-07-24 32.52 32.70 32.33 32.43
2000-07-25 32.42 33.00 32.20 32.50
2000-07-26 32.50 32.80 32.38 32.74
2000-07-27 33.00 33.20 32.50 32.61
2000-07-28 33.80 34.50 33.20 33.90
12.光头阴线
(1)代码
#12、光头阴线(低价位弱势线,下跌抵抗型)
# 收盘价等于最高价,收盘价大于开盘价, head / foot > 1/6 and < 3
signal = []
head_foot_perc =[1/6, 3] # K线头、足比,可调
for t_date in df.index :
k_head = df.loc[t_date,'high'] - df.loc[t_date,'close']
k_foot = df.loc[t_date,'close'] - df.loc[t_date,'low']
# head 和 foot 比例 在 1/6 到 2之间
# 无影线的情况不考虑
if k_foot != 0 and df.loc[t_date,'open'] == df.loc[t_date,'high']:
if df.loc[t_date,'open']>df.loc[t_date,'close'] and k_head/k_foot > head_foot_perc[0] and k_head/k_foot < head_foot_perc[1]:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2000-03-06 32.10 32.49 31.01 31.12
2000-03-07 31.00 32.50 30.60 32.07
2000-03-08 32.18 32.35 31.46 31.90
2000-03-09 32.00 32.20 31.33 31.59
2000-03-10 31.70 31.70 30.98 31.05
2000-03-13 31.00 32.00 30.71 31.31
2000-03-14 31.00 31.00 30.00 30.51
2000-03-15 30.56 30.80 30.18 30.21
2000-03-16 30.19 30.20 28.90 29.16
2000-03-17 29.00 29.60 28.88 29.57
2000-03-20 29.00 29.98 29.00 29.90
2000-03-21 29.90 30.78 29.90 30.14
2000-03-22 30.12 30.40 30.08 30.17
2000-03-23 30.16 30.28 29.80 30.02
2000-03-24 30.05 31.09 30.05 30.65
open high low close
trade_date
2000-05-29 30.70 31.05 30.40 31.00
2000-05-30 31.28 31.35 30.50 30.51
2000-05-31 30.50 30.60 30.10 30.22
2000-06-01 30.25 31.00 30.20 30.57
2000-06-02 31.50 32.08 30.80 31.00
2000-06-05 31.00 32.25 30.81 31.49
2000-06-06 31.90 33.05 31.70 32.41
2000-06-07 32.60 32.80 31.51 31.59
2000-06-08 31.50 31.50 31.00 31.15
2000-06-09 31.11 31.50 30.58 30.66
2000-06-12 30.61 30.98 30.25 30.64
2000-06-13 30.70 30.98 30.68 30.86
2000-06-14 31.00 31.70 30.00 31.03
2000-06-15 31.10 31.28 30.82 30.96
2000-06-16 30.96 31.00 30.60 30.65
13.光脚阳线
(1)代码
#13、光脚阳线(高价位强势线,上升阻力型)
# 开盘价等于最低价,收盘价大于开盘价, head / foot > 1/6 and < 3
signal = []
head_foot_perc =[1/6, 3] # K线头、足比,可调
for t_date in df.index :
k_head = df.loc[t_date,'high'] - df.loc[t_date,'close']
k_foot = df.loc[t_date,'close'] - df.loc[t_date,'low']
# head 和 foot 比例 在 1/6 到 2之间
# 无影线的情况不考虑
if k_head != 0 and k_foot !=0 and df.loc[t_date,'low'] == df.loc[t_date,'open']:
if df.loc[t_date,'close']>df.loc[t_date,'open'] and k_head/k_foot > head_foot_perc[0] and k_head/k_foot < head_foot_perc[1]:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2000-01-10 33.10 33.50 32.67 32.81
2000-01-11 32.86 33.06 31.25 31.44
2000-01-12 31.05 32.00 30.60 30.90
2000-01-13 30.80 31.00 30.48 30.50
2000-01-14 30.40 30.70 29.98 30.01
2000-01-17 29.99 30.66 29.61 30.66
2000-01-18 30.80 31.45 30.36 31.15
2000-01-19 31.30 33.28 31.30 32.58
2000-01-20 32.56 33.10 32.10 32.58
2000-01-21 32.60 32.99 32.22 32.65
2000-01-24 32.85 34.35 32.75 33.59
2000-01-25 34.00 34.20 32.65 32.70
2000-01-26 35.95 35.95 32.35 32.67
2000-01-27 32.70 32.88 31.52 31.95
2000-01-28 32.00 33.40 31.00 33.30
open high low close
trade_date
2000-03-13 31.00 32.00 30.71 31.31
2000-03-14 31.00 31.00 30.00 30.51
2000-03-15 30.56 30.80 30.18 30.21
2000-03-16 30.19 30.20 28.90 29.16
2000-03-17 29.00 29.60 28.88 29.57
2000-03-20 29.00 29.98 29.00 29.90
2000-03-21 29.90 30.78 29.90 30.14
2000-03-22 30.12 30.40 30.08 30.17
2000-03-23 30.16 30.28 29.80 30.02
2000-03-24 30.05 31.09 30.05 30.65
2000-03-27 30.80 31.17 30.28 30.82
2000-03-28 31.01 31.30 30.58 30.86
2000-03-29 30.95 31.03 30.00 30.56
2000-03-30 30.61 30.80 30.35 30.48
2000-03-31 30.50 30.52 30.00 30.15
14.光脚阴线
(1)代码
#14、光脚阴线(低价位弱势线,先涨后跌型)
# 收盘价等于最低价,收盘价小于开盘价, head / foot > 1/6 and < 3
signal = []
head_foot_perc =[1/6, 3] # K线头、足比,可调
for t_date in df.index :
k_head = df.loc[t_date,'high'] - df.loc[t_date,'open']
k_foot = df.loc[t_date,'open'] - df.loc[t_date,'low']
# head 和 foot 比例 在 1/6 到 2之间
# 无影线的情况不考虑
if k_head != 0 and k_foot !=0 and df.loc[t_date,'low'] == df.loc[t_date,'close']:
if df.loc[t_date,'open']>df.loc[t_date,'close'] and k_head/k_foot > head_foot_perc[0] and k_head/k_foot < head_foot_perc[1]:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2001-05-21 38.58 39.00 38.23 38.63
2001-05-22 38.63 38.85 38.50 38.57
2001-05-23 38.57 38.85 38.08 38.57
2001-05-24 38.58 38.62 38.03 38.16
2001-05-25 38.05 38.48 37.96 38.46
2001-05-28 38.46 38.65 37.98 38.11
2001-05-29 38.09 38.90 38.00 38.36
2001-05-30 38.36 39.00 38.25 39.00
2001-05-31 39.08 39.18 38.65 38.65
2001-06-01 38.67 39.15 38.67 39.06
2001-06-04 39.30 40.45 39.15 40.01
2001-06-05 40.10 40.30 39.60 39.70
2001-06-06 39.70 40.18 39.52 40.04
2001-06-07 40.04 40.25 39.50 39.54
2001-06-08 39.49 39.90 39.08 39.85
open high low close
trade_date
2002-07-22 15.05 15.05 14.62 14.63
2002-07-23 14.60 14.77 14.56 14.66
2002-07-24 14.66 14.87 14.61 14.68
2002-07-25 14.68 14.73 14.55 14.59
2002-07-26 14.60 14.63 14.42 14.44
2002-07-29 14.42 14.59 14.42 14.55
2002-07-30 14.59 14.67 14.50 14.56
2002-07-31 14.58 14.64 14.50 14.50
2002-08-01 14.50 14.88 14.40 14.68
2002-08-02 14.68 14.75 14.57 14.58
2002-08-05 14.58 14.95 14.50 14.87
2002-08-06 14.80 15.10 14.71 14.95
2002-08-07 15.00 15.05 14.80 14.93
2002-08-08 14.90 15.00 14.65 14.69
2002-08-09 14.69 14.69 14.40 14.49
15.大阳线
(1)代码
#15、大阳线
#大阳线是指阳线实体很长,上下没有影线或影线很短的K线
#个股涨幅大于5%以上,K线body占K线整体长度60%以上 ,可调。
#大阳线包括光头光脚大阳线、上影线大阳线、下影线大阳线、上下影线阳线
#如果是指数涨幅3%以上的K线称为大阳线,K线body占K线整体长度60%以上 ,可调。
# 收盘价大于开盘价,body /(body + head + foot)> 0.6
signal = []
k_body_perc = 0.6 # K线body占比
stock_pct_chg = 5.0 #股票按5%涨幅
index_pct_chg = 3.0 #指数按3%涨幅
for t_date in df.index :
k_body = df.loc[t_date,'close'] - df.loc[t_date,'open']
k_head = df.loc[t_date,'high'] - df.loc[t_date,'close']
k_foot = df.loc[t_date,'open'] - df.loc[t_date,'low']
if df.loc[t_date,'close']>df.loc[t_date,'open'] and df.loc[t_date,'pct_chg'] > stock_pct_chg :
if k_body / (k_body + k_head + k_foot) > k_body_perc :
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2000-02-14 33.80 36.60 33.80 36.22
2000-02-15 36.50 37.05 34.00 34.75
2000-02-16 34.50 34.70 33.10 33.24
2000-02-17 33.40 35.50 32.00 32.10
2000-02-18 32.00 33.68 31.68 33.09
2000-02-21 33.20 34.50 32.30 33.48
2000-02-22 33.46 33.95 32.00 32.14
2000-02-23 32.00 32.00 30.73 30.96
2000-02-24 31.50 31.60 30.96 31.57
open high low close
trade_date
2000-11-03 32.31 33.10 32.10 33.05
2000-11-06 33.16 33.58 33.05 33.26
2000-11-07 33.28 33.30 32.85 32.95
2000-11-08 32.84 34.45 32.84 34.19
2000-11-09 34.50 34.81 34.05 34.44
2000-11-10 34.48 35.08 34.11 34.64
2000-11-13 35.00 38.10 34.55 38.10
2000-11-14 39.50 40.58 38.74 39.89
2000-11-15 39.99 40.20 39.01 39.30
2000-11-16 39.38 41.60 39.30 41.02
2000-11-17 41.19 41.27 40.00 40.63
2000-11-20 40.60 43.05 40.25 42.52
2000-11-21 42.52 42.98 41.19 41.54
2000-11-22 41.40 41.80 40.85 41.30
2000-11-23 41.48 41.49 40.00 40.46
16.大阴线
(1)代码
#16、大阴线
#大阴线,又称长阴线,是指阴线实体很长,上下没有影线或影线很短的K线,与大阳线类似
#个股跌幅大于5%以上,K线body占K线整体长度60%以上 ,可调。
#大阴线包括光头光脚大阴线、上影线大阴线、下影线大阴线、上下影线阴线
#如果是指数跌幅3%以上的K线称为大阴线,K线body占K线整体长度60%以上 ,可调。
# 收盘价大于开盘价,body /(body + head + foot)> 0.6
signal = []
k_body_perc = 0.6 # K线body占比
stock_pct_chg = -5.0 #股票按5%跌幅
index_pct_chg = -3.0 #指数按3%跌幅
for t_date in df.index :
k_body = df.loc[t_date,'open'] - df.loc[t_date,'close']
k_head = df.loc[t_date,'high'] - df.loc[t_date,'open']
k_foot = df.loc[t_date,'close'] - df.loc[t_date,'low']
if df.loc[t_date,'open']>df.loc[t_date,'close'] and df.loc[t_date,'pct_chg'] < stock_pct_chg :
if k_body / (k_body + k_head + k_foot) > k_body_perc :
signal.append(t_date)
mpfplot(signal,df)
(2)图示
说明:9月3日有除权分红
(3)数据
open high low close
trade_date
2001-07-20 42.51 43.60 42.51 42.79
2001-07-23 42.81 43.00 42.18 42.40
2001-07-24 41.98 42.50 41.05 41.11
2001-07-25 41.10 41.40 40.60 40.73
2001-07-26 41.00 41.29 39.98 40.99
2001-07-27 40.70 41.00 39.90 40.32
2001-07-30 40.00 40.00 38.09 38.12
2001-07-31 38.13 39.00 37.95 38.00
2001-08-01 38.10 39.35 38.10 39.13
2001-08-02 39.13 39.13 37.40 38.00
2001-08-03 38.20 38.55 37.89 38.08
2001-08-06 38.00 38.20 36.40 36.97
2001-08-07 36.90 37.65 36.61 37.51
2001-08-08 37.51 37.78 36.91 37.15
2001-08-09 37.15 38.18 37.15 38.14
open high low close
trade_date
2001-08-17 38.20 38.94 37.62 38.92
2001-08-20 38.90 39.60 38.65 39.58
2001-08-21 39.58 39.77 39.10 39.57
2001-08-22 39.59 39.60 38.18 39.25
2001-08-23 39.80 39.80 38.50 39.43
2001-08-27 38.96 38.96 37.18 37.24
2001-08-28 37.27 38.10 36.90 38.03
2001-08-29 38.03 38.85 37.60 37.88
2001-08-30 38.00 38.00 37.15 37.39
2001-08-31 37.50 38.20 37.30 37.88
2001-09-03 38.00 38.15 36.86 36.90
2001-09-04 21.65 21.70 20.85 21.39
2001-09-05 21.30 21.79 20.80 21.34
2001-09-06 21.21 21.49 21.01 21.22