TA-Lib学习研究笔记(九)——Pattern Recognition (1)
0.程序代码
形态识别的函数的应用,通过使用A股实际的数据,验证形态识别函数,用K线显示出现标志的形态走势,由于入口参数基本上是open, high, low, close ,统一设置程序代码,只更换函数即可。
发现有的形态在一个股票上从未出现,选择多个股票参与计算测试。
函数返回值integer (values are -100, 0 or 100) ,0 是无形态信号。100 和 -100 是有正向或负向的形态信号。
后不再赘述。
# mpf绘图函数
def mpf_show(integer):# 默认是0 ,有标志的值是 -100 ,选择非0值,即出现形态位置的时间position = integer[integer!=0]# 如果没有数据,不绘图if len(position) != 0:print('position Pattern date:')print(position)# 出现形态的时间,前10天到后10天的期间K线for i in position.index: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=i.strftime("%Y-%m-%d") ,figsize=(6,2))mpf.show()for i in position.index: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','pct_chg']]) #选择股票代码做测试
global_cursor.execute(sql_string)
results = global_cursor.fetchall()for r in results :print('stock code:',r[0])df = get_data(r[0], start_date, end_date) #只需要替换此处talib的Pattern Recognition不同函数即可integer = tlb.CDL2CROWS(df['open'], df['high'], df['low'], df['close'])#integer = tlb.CDL3BLACKCROWS(df['open'], df['high'], df['low'], df['close'])mpf_show(integer)
1.CDL2CROWS - Two Crows
函数名:CDL2CROWS
名称:Two Crows 两只乌鸦
简介:三日K线模式,第一天长阳,第二天高开收阴,第三天再次高开继续收阴,收盘比前一日收盘价低,预示股价下跌。
语法:
integer = CDL2CROWS(open, high, low, close)
stock code: 000001.SZ
position Pattern date:
trade_date
2001-04-17 -100
2014-11-04 -100
dtype: int32
open high low close pct_chg
trade_date
2001-04-09 16.31 16.40 16.15 16.27 -0.25
2001-04-10 16.27 16.55 16.27 16.40 0.80
2001-04-11 16.70 16.70 16.38 16.49 0.55
2001-04-12 16.50 16.66 16.33 16.44 -0.30
2001-04-13 16.48 16.97 16.40 16.85 2.49
2001-04-16 17.00 17.11 16.80 16.90 0.30
2001-04-17 16.98 17.00 16.66 16.76 -0.83
2001-04-18 16.65 16.85 16.57 16.59 -1.01
2001-04-19 16.59 16.60 16.28 16.39 -1.21
2001-04-20 16.39 16.48 16.18 16.20 -1.16
2001-04-23 16.20 16.25 16.08 16.13 -0.43
2001-04-24 16.15 16.32 16.13 16.15 0.12
2001-04-25 16.16 16.30 15.68 15.72 -2.66
2001-04-26 15.70 15.80 15.50 15.53 -1.21
2001-04-27 15.51 15.82 15.51 15.65 0.77open high low close pct_chg
trade_date
2014-10-27 10.10 10.13 9.98 10.02 -1.57
2014-10-28 10.05 10.19 10.05 10.17 1.50
2014-10-29 10.19 10.38 10.14 10.30 1.28
2014-10-30 10.31 10.55 10.21 10.43 1.26
2014-10-31 10.50 11.35 10.46 11.03 5.75
2014-11-03 11.16 11.26 10.98 11.11 0.73
2014-11-04 11.15 11.18 10.78 10.85 -2.34
2014-11-05 10.85 10.95 10.66 10.82 -0.28
2014-11-06 10.82 10.90 10.74 10.84 0.18
2014-11-07 10.81 11.38 10.77 10.91 0.65
2014-11-10 11.05 11.25 10.86 11.14 2.11
2014-11-11 11.11 11.64 11.07 11.27 1.17
2014-11-12 11.19 11.21 10.99 11.20 -0.62
2014-11-13 11.18 11.36 11.03 11.05 -1.34
2014-11-14 11.00 11.01 10.80 10.93 -1.09
2.CDL3BLACKCROWS
函数名:CDL3BLACKCROWS
名称:Three Black Crows 三只乌鸦
简介:三日K线模式,连续三根阴线,每日收盘价都下跌且接近最低价,
每日开盘价都在上根K线实体内,预示股价下跌。
语法:
integer = CDL3BLACKCROWS(open, high, low, close)
stock code: 000004.SZ
position Pattern date:
trade_date
2008-04-18 -100
2021-06-24 -100
dtype: int32
open high low close pct_chg
trade_date
2008-04-08 7.90 8.06 7.77 8.00 1.27
2008-04-09 8.01 8.05 7.72 7.80 -2.50
2008-04-10 7.70 8.02 7.70 8.00 2.56
2008-04-11 8.15 8.40 7.92 8.40 5.00
2008-04-14 8.60 8.60 8.02 8.38 -0.24
2008-04-15 8.35 8.38 7.96 8.38 0.00
2008-04-16 8.48 8.50 7.96 7.96 -5.01
2008-04-17 8.19 8.20 7.56 7.56 -5.03
2008-04-18 7.58 7.67 7.18 7.18 -5.03
2008-04-21 7.18 7.18 6.82 6.82 -5.01
2008-04-22 6.51 6.65 6.48 6.48 -4.99
2008-04-23 6.48 6.77 6.16 6.50 0.31
2008-04-24 6.83 6.83 6.69 6.83 5.08
2008-04-25 6.81 7.17 6.70 6.90 1.02
2008-04-28 6.88 7.15 6.56 7.03 1.88open high low close pct_chg
trade_date
2021-06-15 23.99 24.20 21.58 21.58 -10.0083
2021-06-16 20.30 21.30 19.43 20.15 -6.6265
2021-06-17 19.70 21.50 19.20 20.92 3.8213
2021-06-18 20.20 20.68 19.55 19.78 -5.4493
2021-06-21 19.50 21.40 19.18 20.77 5.0051
2021-06-22 20.30 20.94 20.01 20.18 -2.8406
2021-06-23 20.26 20.40 19.66 19.80 -1.8831
2021-06-24 20.10 20.20 18.82 18.86 -4.7475
2021-06-25 18.86 19.10 17.90 18.11 -3.9767
2021-06-28 17.99 18.75 17.88 18.44 1.8222
2021-06-29 18.22 19.13 18.22 18.81 2.0065
2021-06-30 18.44 18.83 18.31 18.56 -1.3291
2021-07-01 18.50 18.80 17.58 17.73 -4.4720
2021-07-02 17.63 18.25 17.60 17.98 1.4100
3.CDL3BLACKCROWS
函数名:CDL3BLACKCROWS
名称:Three Black Crows 三只乌鸦
简介:三日K线模式,连续三根阴线,每日收盘价都下跌且接近最低价,
每日开盘价都在上根K线实体内,预示股价下跌。
语法:
integer = CDL3INSIDE(open, high, low, close)
stock code: 000001.SZ
position Pattern date:
trade_date
2000-04-26 100
2000-08-17 100
open high low close pct_chg
trade_date
2000-04-17 19.52 20.02 18.95 19.18 -1.59
2000-04-18 19.24 19.45 18.97 19.10 -0.42
2000-04-19 19.51 19.68 19.10 19.15 0.26
2000-04-20 19.18 19.33 18.90 19.02 -0.68
2000-04-21 19.02 19.10 18.78 18.81 -1.10
2000-04-24 18.81 18.94 18.50 18.57 -1.28
2000-04-25 18.58 18.92 18.57 18.69 0.65
2000-04-26 18.80 19.80 18.65 19.13 2.35
2000-04-27 19.30 19.40 18.71 18.76 -1.93
2000-04-28 18.74 19.20 18.65 19.06 1.60
open high low close pct_chg
trade_date
2000-08-07 18.31 18.60 18.20 18.30 0.27
2000-08-08 18.35 18.40 18.10 18.21 -0.49
2000-08-09 18.20 18.58 18.15 18.35 0.77
2000-08-10 18.35 18.43 18.20 18.22 -0.71
2000-08-11 18.23 18.45 18.20 18.29 0.38
2000-08-14 18.35 18.85 18.32 18.51 1.20
2000-08-15 18.50 18.59 18.28 18.30 -1.13
2000-08-16 18.40 18.59 18.24 18.42 0.66
2000-08-17 18.42 18.95 18.40 18.58 0.87
2000-08-18 18.60 18.70 18.45 18.48 -0.54
2000-08-21 18.50 18.70 18.38 18.40 -0.43
2000-08-22 18.40 18.50 18.10 18.16 -1.30
2000-08-23 18.10 18.30 17.71 18.09 -0.39
2000-08-24 18.08 18.39 17.95 18.10 0.06
2000-08-25 18.15 18.20 18.00 18.10 0.00
4. CDL3LINESTRIKE - Three-Line Strike
函数名:CDL3LINESTRIKE
名称: Three-Line Strike 三线打击
简介:四日K线模式,前三根阳线,每日收盘价都比前一日高,
开盘价在前一日实体内,第四日市场高开,收盘价低于第一日开盘价,预示股价下跌。
语法:
integer = CDL3LINESTRIKE(open, high, low, close)
stock code: 000001.SZ
position Pattern date:
trade_date
2001-12-05 100
2003-07-18 100
open high low close pct_chg
trade_date
2001-11-26 13.50 13.50 13.37 13.39 -1.11
2001-11-27 13.39 13.48 13.35 13.43 0.30
2001-11-28 13.48 13.75 13.44 13.50 0.52
2001-11-29 13.50 13.55 13.35 13.39 -0.81
2001-11-30 13.35 13.50 13.30 13.42 0.22
2001-12-03 13.42 13.62 13.40 13.50 0.60
2001-12-04 13.50 13.65 13.43 13.59 0.67
2001-12-05 13.60 13.61 13.22 13.24 -2.58
2001-12-06 13.24 13.39 13.14 13.26 0.15
2001-12-07 13.26 13.39 13.13 13.19 -0.53
2001-12-10 13.15 13.18 13.04 13.14 -0.38
2001-12-11 13.16 13.25 13.00 13.06 -0.61
2001-12-12 13.05 13.05 12.80 12.84 -1.68
2001-12-13 12.80 12.94 12.70 12.72 -0.93
2001-12-14 12.70 12.85 12.63 12.71 -0.08
open high low close pct_chg
trade_date
2003-07-08 11.18 11.32 11.15 11.30 1.25
2003-07-09 11.31 11.36 11.11 11.16 -1.24
2003-07-10 11.20 11.48 11.13 11.39 2.06
2003-07-11 11.40 11.43 11.28 11.31 -0.70
2003-07-14 11.28 11.32 11.10 11.14 -1.50
2003-07-15 11.14 11.28 11.13 11.19 0.45
2003-07-16 11.19 11.30 11.19 11.24 0.45
2003-07-17 11.24 11.37 11.10 11.31 0.62
2003-07-18 11.32 11.33 10.88 10.91 -3.54
2003-07-21 10.72 10.88 10.57 10.66 -2.29
2003-07-22 10.71 10.84 10.47 10.56 -0.94
2003-07-23 10.60 10.78 10.57 10.75 1.80
2003-07-24 10.74 10.75 10.54 10.58 -1.58
2003-07-25 10.60 10.75 10.48 10.50 -0.76
2003-07-28 10.50 10.50 10.32 10.39 -1.05
5.CDL3OUTSIDE
函数名:CDL3OUTSIDE
名称:Three Outside Up/Down 三外部上涨和下跌
简介:三日K线模式,与三内部上涨和下跌类似,K线为阴阳阳,但第一日与第二日的K线形态相反,
以三外部上涨为例,第一日K线在第二日K线内部,预示着股价上涨。
语法:
integer = CDL3OUTSIDE(open, high, low, close)
注意:返回值 一个100 一个 -100
stock code: 000001.SZ
position Pattern date:
trade_date
2000-01-07 100
2000-01-12 -100
open high low close pct_chg
trade_date
2000-01-04 17.50 18.55 17.20 18.29 4.81
2000-01-05 18.35 18.85 18.00 18.06 -1.26
2000-01-06 18.02 19.05 17.75 18.78 3.99
2000-01-07 19.00 19.77 18.90 19.54 4.05
2000-01-10 19.79 20.48 19.77 20.14 3.07
2000-01-11 20.15 20.20 18.80 19.00 -5.66
2000-01-12 19.00 19.20 18.13 18.24 -4.00
2000-01-13 18.20 18.40 17.90 18.14 -0.55
2000-01-14 18.14 18.39 17.75 17.88 -1.43
2000-01-17 17.90 18.10 17.50 18.08 1.12
open high low close pct_chg
trade_date
2000-01-04 17.50 18.55 17.20 18.29 4.81
2000-01-05 18.35 18.85 18.00 18.06 -1.26
2000-01-06 18.02 19.05 17.75 18.78 3.99
2000-01-07 19.00 19.77 18.90 19.54 4.05
2000-01-10 19.79 20.48 19.77 20.14 3.07
2000-01-11 20.15 20.20 18.80 19.00 -5.66
2000-01-12 19.00 19.20 18.13 18.24 -4.00
2000-01-13 18.20 18.40 17.90 18.14 -0.55
2000-01-14 18.14 18.39 17.75 17.88 -1.43
2000-01-17 17.90 18.10 17.50 18.08 1.12
2000-01-18 18.00 18.08 17.58 17.69 -2.16
2000-01-19 17.50 17.85 17.50 17.79 0.57
2000-01-20 17.80 17.90 17.55 17.81 0.11
2000-01-21 17.80 18.26 17.66 17.96 0.84
6.CDL3STARSINSOUTH
函数名:CDL3STARSINSOUTH
名称:Three Stars In The South 南方三星
简介:三日K线模式,与大敌当前相反,三日K线皆阴,第一日有长下影线,
第二日与第一日类似,K线整体小于第一日,第三日无下影线实体信号,
成交价格都在第一日振幅之内,预示下跌趋势反转,股价上升。
语法:
integer = CDL3STARSINSOUTH(open, high, low, close)
指标符合条件的很少。
stock code: 000669.SZ
position Pattern date:
trade_date
2014-05-13 100
dtype: int32
open high low close pct_chg
trade_date
2014-05-05 26.50 26.65 25.80 26.62 0.53
2014-05-06 26.62 27.43 26.42 27.15 1.99
2014-05-07 27.20 27.20 26.20 26.33 -3.02
2014-05-08 26.35 26.46 26.00 26.31 -0.08
2014-05-09 26.66 26.66 25.00 26.11 -0.76
2014-05-12 26.12 26.26 25.30 26.08 -0.11
2014-05-13 26.00 26.06 25.51 25.55 -2.03
2014-05-14 25.58 26.40 25.54 26.31 2.97
2014-05-15 26.30 26.30 25.76 26.00 -1.18
2014-05-16 25.92 26.60 25.71 26.60 2.31
2014-05-19 26.50 26.61 26.08 26.19 -1.54
2014-05-20 26.58 27.45 26.19 26.53 1.30
2014-05-21 26.56 26.56 25.95 26.20 -1.24
2014-05-22 27.10 28.70 27.00 27.13 3.55
2014-05-23 27.11 27.11 26.30 26.58 -2.03
7.CDL3WHITESOLDIERS
函数名:CDL3WHITESOLDIERS
名称:Three Advancing White Soldiers 三个白兵
简介:三日K线模式,三日K线皆阳,
每日收盘价变高且接近最高价,开盘价在前一日实体上半部,预示股价上升。
语法:
integer = CDL3WHITESOLDIERS(open, high, low, close)
stock code: 000001.SZ
position Pattern date:
trade_date
2007-04-12 100
2017-07-07 100
dtype: int32
open high low close pct_chg
trade_date
2007-04-02 18.98 19.60 18.95 19.28 2.12
2007-04-03 19.31 19.68 19.17 19.35 0.36
2007-04-04 19.40 20.20 19.40 19.89 2.79
2007-04-05 19.89 20.07 19.56 19.96 0.35
2007-04-06 19.66 19.90 19.35 19.57 -1.95
2007-04-09 19.57 19.98 19.40 19.72 0.77
2007-04-10 19.72 20.30 19.63 20.29 2.89
2007-04-11 20.33 21.30 20.33 21.29 4.93
2007-04-12 21.40 22.35 21.30 22.35 4.98
2007-04-13 22.70 23.47 22.70 23.47 5.01
2007-04-16 23.98 24.47 22.90 23.73 1.11
2007-04-17 23.55 23.73 22.54 22.95 -3.29
2007-04-18 22.88 23.40 22.53 22.99 0.17
2007-04-19 22.70 23.06 21.84 21.84 -5.00
2007-04-20 21.38 22.86 21.38 22.60 3.48open high low close pct_chg
trade_date
2017-06-27 9.30 9.39 9.27 9.36 0.65
2017-06-28 9.35 9.49 9.33 9.43 0.75
2017-06-29 9.43 9.45 9.37 9.43 0.00
2017-06-30 9.40 9.43 9.31 9.39 -0.42
2017-07-03 9.40 9.43 9.34 9.40 0.11
2017-07-04 9.40 9.41 9.30 9.34 -0.64
2017-07-05 9.29 9.38 9.27 9.37 0.32
2017-07-06 9.36 9.41 9.31 9.40 0.32
2017-07-07 9.37 9.48 9.34 9.47 0.74
2017-07-10 9.45 9.66 9.44 9.59 1.27
2017-07-11 9.61 10.46 9.61 10.25 6.88
2017-07-12 10.27 10.58 10.20 10.34 0.88
2017-07-13 10.30 10.90 10.24 10.90 5.42
2017-07-14 10.81 10.94 10.66 10.90 0.00
2017-07-17 10.95 11.33 10.72 10.81 -0.83
8.CDLABANDONEDBABY
函数名:CDLABANDONEDBABY
名称:Abandoned Baby 弃婴
简介:三日K线模式,第二日价格跳空且收十字星(开盘价与收盘价接近,
最高价最低价相差不大),预示趋势反转,发生在顶部下跌,底部上涨。
语法:
注意:多一个参数
integer = CDLABANDONEDBABY(open, high, low, close, penetration=0)
指标符合条件的少
stock code: 000410.SZ
position Pattern date:
trade_date
2016-12-09 -100
dtype: int32
open high low close pct_chg
trade_date
2016-11-29 14.96 14.99 14.72 14.74 -1.73
2016-11-30 14.75 14.94 14.66 14.79 0.34
2016-12-01 14.77 14.92 14.72 14.92 0.88
2016-12-02 14.89 14.90 14.62 14.67 -1.68
2016-12-05 14.49 14.82 14.41 14.62 -0.34
2016-12-06 14.72 14.75 14.47 14.57 -0.34
2016-12-07 14.50 14.67 14.50 14.66 0.62
2016-12-08 14.68 14.93 14.68 14.71 0.34
2016-12-09 14.65 14.65 14.41 14.48 -1.56
2016-12-12 14.46 14.52 13.64 13.66 -5.66
2016-12-13 13.61 13.68 13.42 13.62 -0.29
2016-12-14 13.62 13.79 13.42 13.63 0.07
2016-12-15 13.56 13.84 13.53 13.71 0.59
2016-12-16 13.72 13.85 13.65 13.77 0.44
2016-12-19 13.70 13.73 13.60 13.65 -0.87
9.CDLADVANCEBLOCK
函数名:CDLADVANCEBLOCK
名称:Advance Block 大敌当前
简介:三日K线模式,三日都收阳,每日收盘价都比前一日高,
开盘价都在前一日实体以内,实体变短,上影线变长。
语法:
integer = CDLADVANCEBLOCK(open, high, low, close)
stock code: 000001.SZ
position Pattern date:
trade_date
2002-04-08 -100
2002-07-19 -100
open high low close pct_chg
trade_date
2002-03-29 11.35 11.40 10.98 11.03 -2.39
2002-04-01 11.03 11.06 10.81 10.98 -0.45
2002-04-02 10.98 11.04 10.67 10.69 -2.64
2002-04-03 10.68 10.88 10.57 10.78 0.84
2002-04-04 10.81 11.15 10.73 11.11 3.06
2002-04-05 11.15 11.25 11.03 11.15 0.36
2002-04-08 11.20 11.35 11.11 11.27 1.08
2002-04-09 11.25 11.30 10.90 11.15 -1.06
2002-04-10 11.15 11.20 10.93 11.12 -0.27
2002-04-11 11.12 11.15 10.98 11.02 -0.90
2002-04-12 11.00 11.13 10.91 11.11 0.82
2002-04-15 11.05 11.13 10.83 10.89 -1.98
2002-04-16 10.80 10.93 10.74 10.78 -1.01
2002-04-17 10.76 10.86 10.73 10.75 -0.28open high low close pct_chg
trade_date
2002-07-09 14.90 15.11 14.79 14.90 0.13
2002-07-10 14.93 15.00 14.25 14.39 -3.42
2002-07-11 14.30 14.46 14.17 14.30 -0.63
2002-07-12 14.30 14.38 14.20 14.22 -0.56
2002-07-15 14.19 14.20 13.95 14.02 -1.41
2002-07-16 14.00 14.38 13.90 13.98 -0.29
2002-07-17 14.00 14.44 13.95 14.42 3.15
2002-07-18 14.45 15.00 14.35 14.93 3.54
2002-07-19 14.93 15.08 14.76 14.99 0.40
2002-07-22 14.98 15.00 14.48 14.50 -3.27
2002-07-23 14.34 15.30 14.30 15.06 4.95
2002-07-24 15.06 15.33 14.89 15.07 0.07
2002-07-25 15.08 15.15 14.69 14.79 -1.86
2002-07-26 14.80 15.05 14.65 14.79 0.00
2002-07-29 14.77 15.07 14.72 15.00 1.42
10.
函数名:CDLBELTHOLD
名称:Belt-hold 捉腰带线
简介:两日K线模式,下跌趋势中,第一日阴线,
第二日开盘价为最低价,阳线,收盘价接近最高价,预示价格上涨。
语法:
integer = CDLBELTHOLD(open, high, low, close)
stock code: 000001.SZ
position Pattern date:
trade_date
2000-01-24 -100
2000-02-16 -100
open high low close pct_chg
trade_date
2000-01-14 18.14 18.39 17.75 17.88 -1.43
2000-01-17 17.90 18.10 17.50 18.08 1.12
2000-01-18 18.00 18.08 17.58 17.69 -2.16
2000-01-19 17.50 17.85 17.50 17.79 0.57
2000-01-20 17.80 17.90 17.55 17.81 0.11
2000-01-21 17.80 18.26 17.66 17.96 0.84
2000-01-24 17.91 17.95 17.20 17.50 -2.56
2000-01-25 17.50 18.29 17.50 17.73 1.31
2000-01-26 17.72 17.88 17.50 17.64 -0.51
2000-01-27 17.65 18.17 17.55 17.89 1.42
2000-01-28 18.00 18.58 17.66 18.50 3.41open high low close pct_chg
trade_date
2000-02-14 19.20 20.38 18.70 20.37 10.11
2000-02-15 20.50 21.01 19.10 19.46 -4.47
2000-02-16 19.30 19.30 18.56 18.83 -3.24
2000-02-17 18.83 19.69 18.23 18.80 -0.16
2000-02-18 18.79 18.79 18.15 18.41 -2.07
2000-02-21 18.41 19.50 18.00 18.91 2.72
2000-02-22 19.00 19.25 18.20 18.25 -3.49
2000-02-23 18.20 18.58 18.00 18.10 -0.82
2000-02-24 18.05 18.30 18.00 18.18 0.44
2000-02-25 18.18 18.20 18.00 18.09 -0.50