Backtrader 量化回测实践(2)—— K线16主要形态定义(上)
K线图形中的趋势线和价格走势能够反映市场的整体趋势,比如是否处于上涨或下跌趋势中。
用Backtrader做策略的时候,需要考虑K线形态,作为分析依据。K线的常用形态搜集整理如下:
1、光头光脚大阳线(极端强势);
2、光头光脚大阴线(极端弱势);
3、光头阳线(高价位强势线,先跌后涨型);
4、光头阴线(低价位弱势线,下跌抵抗型);
5、光脚阳线(高价位强势线,上升阻力型);
6、光脚阴线(低价位弱势线,先涨后跌型);
7、大阳线(较为强烈的买势信号,反转试探型);
8、大阴线(较为强烈的卖势信号,弹升试探型);
9、十字线(阳线,阴线);
10、T字线(阳线,阴线);
11、倒T字线(阳线,阴线);
12、一字线(阳线涨停,阴线跌停)。
以上一共16个,应该是常见的K线主要形态。
在策略中需要通过程序定义K线的形态,根据网上的介绍和定义,用dataframe分析。
取一个股票的数据导入到dataframe中,通过定义找到相应的形态并mplfinance绘图展示。
如果有错误,不妥之处欢迎留言,改进完善。
0.绘图
# signal是符合形态的日期列表,df是全量股票数据
# 只展示两个符合条件的图示
def mpfplot(signal,df) :
counter = 0
show_num = 2
for i in signal:
if counter < show_num : # 以信号日期为中心前后10天的数据
begin_date = i + datetime.timedelta(days=-10)
end_date = i + datetime.timedelta(days=10)
# 按时间段绘图
df_candle = df.loc[(df.index>=begin_date.strftime("%Y-%m-%d")) & (df.index<=end_date.strftime("%Y-%m-%d"))]
mpf.plot(df_candle , type='candle', title='Signal date: ' +i.strftime("%Y-%m-%d") ,figsize=(6,2))
mpf.show()
counter += 1
counter = 0
for i in signal:
if counter < show_num :
begin_date = i + datetime.timedelta(days=-10)
end_date = i + datetime.timedelta(days=10)
df_candle = df.loc[(df.index>=begin_date.strftime("%Y-%m-%d")) & (df.index<=end_date.strftime("%Y-%m-%d"))]
# 按时间段数据
print(df_candle[['open','high','low','close']])
counter += 1
1. 光头光脚大阳线
(1)代码
#1、光头光脚大阳线(极端强势)
# 开盘就是最低价,收盘就是最高价,收盘价大于开盘价
signal = []
for t_date in df.index :
if df.loc[t_date,'open']==df.loc[t_date,'low'] and df.loc[t_date,'close']==df.loc[t_date,'high'] and df.loc[t_date,'close']>df.loc[t_date,'open']:
signal.append(t_date)
signal = []
for t_date in df.index :
if df.loc[t_date,'open']==df.loc[t_date,'low'] and df.loc[t_date,'close']==df.loc[t_date,'high'] and df.loc[t_date,'close']>df.loc[t_date,'open']:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2001-06-15 38.52 39.10 38.45 38.54
2001-06-18 38.50 38.88 37.60 37.66
2001-06-19 37.66 37.99 37.43 37.72
2001-06-20 37.72 37.90 37.11 37.19
2001-06-21 37.19 37.79 37.19 37.56
2001-06-22 36.80 38.20 36.80 38.00
2001-06-25 38.03 38.68 38.03 38.68
2001-06-26 38.78 38.84 38.50 38.80
2001-06-27 38.80 39.61 38.60 39.60
2001-06-28 39.60 39.95 38.65 39.29
2001-06-29 39.40 39.98 39.01 39.24
2001-07-02 39.10 39.15 38.37 38.52
2001-07-03 38.50 38.85 38.40 38.71
2001-07-04 38.71 38.90 38.56 38.71
2001-07-05 38.71 38.96 38.41 38.53
open high low close
trade_date
2003-11-03 10.08 10.18 9.97 10.12
2003-11-04 10.19 10.60 10.00 10.31
2003-11-05 10.34 10.52 10.25 10.46
2003-11-06 10.55 10.60 10.30 10.40
2003-11-07 10.39 10.39 10.01 10.18
2003-11-10 10.10 10.29 10.05 10.28
2003-11-11 10.25 10.48 10.25 10.48
2003-11-12 10.47 10.48 10.11 10.14
2003-11-13 10.13 10.20 10.01 10.09
2003-11-14 10.06 10.15 9.97 10.08
2003-11-17 10.03 10.12 9.98 10.08
2003-11-18 10.03 10.03 9.80 9.90
2003-11-19 9.80 10.00 9.76 9.94
2003-11-20 10.00 10.22 9.90 10.17
2003-11-21 10.18 10.24 9.98 10.01
2.光头光脚大阴线
(1)代码
#2、光头光脚大阴线(极端弱势)
# 开盘就是最高价,收盘就是最低价,开盘价大于收盘价
signal = []
for t_date in df.index :
if df.loc[t_date,'open']==df.loc[t_date,'high'] and df.loc[t_date,'close']==df.loc[t_date,'low'] and df.loc[t_date,'open']>df.loc[t_date,'close']:
signal.append(t_date)
signal = []
for t_date in df.index :
if df.loc[t_date,'open']==df.loc[t_date,'low'] and df.loc[t_date,'close']==df.loc[t_date,'high'] and df.loc[t_date,'close']>df.loc[t_date,'open']:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2004-06-14 6.14 6.18 5.99 6.02
2004-06-15 6.03 6.20 6.00 6.18
2004-06-16 6.18 6.28 6.08 6.17
2004-06-17 6.15 6.17 6.04 6.09
2004-06-18 6.06 6.08 6.00 6.07
2004-06-21 6.08 6.16 6.02 6.11
2004-06-22 6.11 6.21 6.09 6.19
2004-06-23 6.19 6.22 6.12 6.15
2004-06-24 6.15 6.15 6.09 6.09
2004-06-25 6.09 6.16 6.02 6.05
2004-06-28 6.05 6.18 5.95 6.08
2004-06-29 6.08 6.22 6.02 6.14
2004-06-30 6.14 6.17 6.10 6.12
2004-07-01 6.10 6.33 6.08 6.29
2004-07-02 6.32 6.36 6.21 6.31
open high low close
trade_date
2005-01-10 6.70 6.82 6.66 6.82
2005-01-11 6.86 6.88 6.75 6.80
2005-01-12 6.80 6.84 6.70 6.78
2005-01-13 6.77 6.81 6.68 6.75
2005-01-14 6.76 6.88 6.74 6.79
2005-01-17 6.77 6.77 6.55 6.61
2005-01-18 6.58 6.70 6.57 6.69
2005-01-19 6.72 6.74 6.65 6.73
2005-01-20 6.70 6.70 6.60 6.60
2005-01-21 6.59 6.83 6.45 6.80
2005-01-24 6.88 6.97 6.80 6.84
2005-01-25 6.85 7.00 6.77 7.00
2005-01-26 7.00 7.08 6.90 6.98
2005-01-27 6.97 6.97 6.80 6.83
2005-01-28 6.83 6.98 6.79 6.95
3. 阳线十字星
(1)代码
#3、阳线十字星
# 收盘价大于开盘价,K线体 / 上下影线的长度占比 < 1/3
signal = []
body_perc = 1 / 3 # K线体比上下影线,可调
shadow_perc = 1 / 4 # 上下影线比例,上下均衡,可调
for t_date in df.index :
k_body = df.loc[t_date,'close'] - df.loc[t_date,'open']
k_shadow_up = df.loc[t_date,'high'] - df.loc[t_date,'close']
k_shadow_down = df.loc[t_date,'open'] - df.loc[t_date,'low']
k_shadow = abs(k_shadow_up) + abs(k_shadow_down)
#上下影线的长度 占比 1/3 ,可以调整
# 无影线的情况不考虑 ,避免T字形,要求上影线和下影线都要有一定长度,至少要总影线长度的1/4
if k_shadow != 0 and k_shadow_up/k_shadow > shadow_perc and k_shadow_down/k_shadow > shadow_perc:
if df.loc[t_date,'close']>df.loc[t_date,'open'] and k_body/k_shadow < body_perc:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2000-01-04 29.50 30.38 29.30 30.10
2000-01-05 30.30 31.60 30.17 30.38
2000-01-06 30.30 31.88 30.10 31.60
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
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
4.阴线十字星
(1)代码
#4、阴线十字星
# 收盘价小于开盘价,K线体 / 上下影线的长度占比 < 1/3
signal = []
body_perc = 1 / 3 # K线体比上下影线,可调
shadow_perc = 1 / 4 # 上下影线比例,上下均衡,可调
for t_date in df.index :
k_body = abs(df.loc[t_date,'close'] - df.loc[t_date,'open'])
k_shadow_up = df.loc[t_date,'high'] - df.loc[t_date,'open']
k_shadow_down = df.loc[t_date,'close'] - df.loc[t_date,'low']
k_shadow = abs(k_shadow_up) + abs(k_shadow_down)
#上下影线的长度 占比 1/3 ,可以调整
# 无影线的情况不考虑 ,避免T字形,要求上影线和下影线都要有一定长度,至少要总影线长度的1/4
if k_shadow != 0 and k_shadow_up/k_shadow > body_perc and k_shadow_down/k_shadow > body_perc:
if df.loc[t_date,'open']>df.loc[t_date,'close'] and k_body/k_shadow < body_perc:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2000-01-04 29.50 30.38 29.30 30.10
2000-01-05 30.30 31.60 30.17 30.38
2000-01-06 30.30 31.88 30.10 31.60
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
open high low close
trade_date
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
2000-04-03 30.15 30.97 30.00 30.20
2000-04-04 30.01 30.15 29.60 29.80
2000-04-05 29.80 30.30 29.80 30.00
2000-04-06 30.05 30.50 30.00 30.36
2000-04-07 30.40 30.50 30.10 30.19
5.阳线T字
(1)代码
#5、阳线T字
# 收盘价等于最高价,收盘价大于开盘价,K线体 / 下影线的长度占比 < 1/4
signal = []
body_perc = 1 / 3 # K线体比上下影线,可调
shadow_perc = 1 / 4 # 上下影线比例,上下均衡,可调
for t_date in df.index :
k_body = df.loc[t_date,'close'] - df.loc[t_date,'open']
k_shadow_down = df.loc[t_date,'open'] - df.loc[t_date,'low']
# 下影线的长度 占比 1/4 ,可以调整
# 无影线的情况不考虑
if k_shadow_down != 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_body/k_shadow_down < shadow_perc:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2008-04-14 24.17 24.17 22.60 22.80
2008-04-15 22.60 23.80 21.89 23.70
2008-04-16 23.70 25.00 23.20 24.45
2008-04-17 24.18 25.35 23.86 24.23
2008-04-18 24.18 24.81 23.17 23.45
2008-04-21 25.48 25.48 23.64 24.06
2008-04-22 23.71 24.78 22.70 24.55
2008-04-23 24.70 26.32 23.98 26.11
2008-04-24 28.50 28.72 27.50 28.72
2008-04-25 28.50 29.30 28.03 28.39
2008-04-28 27.81 28.45 26.98 27.65
2008-04-29 27.38 27.70 26.90 27.02
2008-04-30 27.19 28.55 27.19 28.45
6.阴线T字
(1)代码
#6、阴线T字
# 开盘价等于最高价,开盘价大于收盘价,K线体 / 下影线的长度占比 < 1/4
signal = []
body_perc = 1 / 3 # K线体比上下影线,可调
shadow_perc = 1 / 4 # 上下影线比例,上下均衡,可调
for t_date in df.index :
k_body = abs(df.loc[t_date,'close'] - df.loc[t_date,'open'])
k_shadow_down = df.loc[t_date,'close'] - df.loc[t_date,'low']
# 下影线的长度 占比 1/4 ,可以调整
# 无影线的情况不考虑
if k_shadow_down != 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_body/k_shadow_down < shadow_perc:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2002-06-14 13.34 13.56 13.24 13.42
2002-06-17 13.25 13.42 13.25 13.36
2002-06-18 13.36 13.52 13.34 13.51
2002-06-19 13.60 13.65 13.30 13.32
2002-06-20 13.30 13.47 13.30 13.39
2002-06-21 13.41 14.00 13.35 13.80
2002-06-24 15.18 15.18 14.61 15.16
2002-06-25 15.18 15.18 14.60 14.63
2002-06-26 14.62 14.70 14.31 14.43
2002-06-27 14.45 14.80 14.43 14.58
2002-06-28 14.60 15.70 14.40 15.68
2002-07-01 15.68 15.90 15.30 15.42
2002-07-02 15.40 15.53 15.18 15.50
2002-07-03 15.55 15.78 15.33 15.46
2002-07-04 15.47 15.64 15.15 15.17
open high low close
trade_date
2002-06-25 15.18 15.18 14.60 14.63
2002-06-26 14.62 14.70 14.31 14.43
2002-06-27 14.45 14.80 14.43 14.58
2002-06-28 14.60 15.70 14.40 15.68
2002-07-01 15.68 15.90 15.30 15.42
2002-07-02 15.40 15.53 15.18 15.50
2002-07-03 15.55 15.78 15.33 15.46
2002-07-04 15.47 15.64 15.15 15.17
2002-07-05 15.17 15.17 14.95 15.13
2002-07-08 15.20 15.40 15.16 15.30
2002-07-09 15.45 15.50 15.23 15.28
2002-07-10 15.24 15.26 14.78 14.86
2002-07-11 14.83 14.96 14.70 14.83
2002-07-12 14.80 14.99 14.75 14.87
2002-07-15 14.85 14.85 14.70 14.72
7.阳线倒T字
(1)代码
#7、阳线倒T字
# 开盘价等于最低价,收盘价大于开盘价,K线体 / 上影线的长度占比 < 1/4
signal = []
body_perc = 1 / 3 # K线体比上下影线,可调
shadow_perc = 1 / 4 # 上下影线比例,上下均衡,可调
for t_date in df.index :
k_body = df.loc[t_date,'close'] - df.loc[t_date,'open']
k_shadow_up = df.loc[t_date,'high'] - df.loc[t_date,'close']
# 上影线的长度 占比 1/4 ,可以调整
# 无影线的情况不考虑
if k_shadow_up != 0 and df.loc[t_date,'open'] == df.loc[t_date,'low']:
if df.loc[t_date,'close']>df.loc[t_date,'open'] and k_body/k_shadow_down < shadow_perc:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
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
open high low close
trade_date
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
2000-04-03 30.15 30.97 30.00 30.20
2000-04-04 30.01 30.15 29.60 29.80
2000-04-05 29.80 30.30 29.80 30.00
2000-04-06 30.05 30.50 30.00 30.36
2000-04-07 30.40 30.50 30.10 30.19
2000-04-10 30.25 31.28 30.25 31.08
2000-04-11 31.10 31.28 30.63 30.84
2000-04-12 30.98 31.09 30.32 30.50
2000-04-13 30.50 30.94 30.48 30.73
2000-04-14 30.79 31.89 30.75 31.42
8.阴线倒T字
(1)代码
#8、阴线倒T字
# 收盘价等于最低价,开盘价大于收盘价,K线体 / 上影线的长度占比 < 1/4
signal = []
body_perc = 1 / 3 # K线体比上下影线,可调
shadow_perc = 1 / 4 # 上下影线比例,上下均衡,可调
for t_date in df.index :
k_body = abs(df.loc[t_date,'close'] - df.loc[t_date,'open'])
k_shadow_up = df.loc[t_date,'high'] - df.loc[t_date,'open']
# 上影线的长度 占比 1/4 ,可以调整
# 无影线的情况不考虑
if k_shadow_up != 0 and df.loc[t_date,'close'] == df.loc[t_date,'low']:
if df.loc[t_date,'open']>df.loc[t_date,'close'] and k_body/k_shadow_up < shadow_perc:
signal.append(t_date)
mpfplot(signal,df)
(2)图示
(3)数据
open high low close
trade_date
2021-01-22 295.08 299.91 292.58 295.56
2021-01-25 295.57 316.33 293.20 315.69
2021-01-26 315.69 315.71 305.10 305.78
2021-01-27 302.90 302.90 287.00 292.58
2021-01-28 288.23 292.00 283.00 286.00
2021-01-29 290.00 295.14 288.78 291.14
2021-02-01 293.01 299.87 292.29 292.29
2021-02-02 292.00 304.33 291.90 304.12
2021-02-03 303.80 309.00 301.12 308.75
2021-02-04 306.00 316.80 305.01 313.60
2021-02-05 314.01 330.00 314.00 319.76
2021-02-08 324.00 330.50 313.80 320.00
2021-02-09 322.00 330.00 317.86 329.50
2021-02-10 333.00 345.68 332.50 342.65