-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2024 手写K线组合模式识别 .py
More file actions
134 lines (80 loc) · 3.99 KB
/
Copy path2024 手写K线组合模式识别 .py
File metadata and controls
134 lines (80 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
###############################################################################
#
# 手写量化交易的利器 2 K线组合模式识别 - talib
#
################################################################################
# K线组合模式识别方法,如晨线,昏线,锤子线等K线组合;
## 晨星也称为是早晨之星,它是一种三日的K线组合形态,第一日是阴线,第二日是价格振幅较小,第三日是出现阳线;
## 一般出现下跌趋势之后,意味着价格的上升回调,并且通常结合交易量的指标;
nums = talib.CDLMORNINGSTAR(df['open'],df['high'],df['low'],df['close'])
print(nums)
##为了将识别的结果进行可视化,通过索引在原K线图中进行标注,因为是一种买入信号,所以需要获取的返回结果是100的索引位置,其中在识别到的
###K线的最高价上面通过箭头来标注。
fig = plt.figure()
ax = fig.add_subplot(111)
#绘制K线图
mpf.candlestick_ohlc(ax,df['open'],df['high'],df['low'],df['close'],width=0.6,colorup='red',colordown='green')
# 标注识别K线组合的位置
index = nums[nums==100].index.values
for i in index:
ax.annotate(s=' ',xy=(i,df['high'][i]),xytext=(i,df['high'][i]+0.5),arrowprops={'arrowstyle':' ->'})
plt.show()
##需要注意的是,图中标注点是该组合形态的最后一根K线的位置上,即出现反转的阳线上;
########### 第二个组合形态 昏星 高位股
## evening star 第一日阳线,第二日价格振幅较小,第三日阴线,它通常出现在一段上升趋势中,预示着顶部的价格反转;
nums = talib.CDLEVENINGSTAR(df['open'],df['high'],df['low'],df['close'])
#可视化
fig = plt.figure()
ax = fig.add_subplot(111)
# 绘制K线图
mpf.candlestick2_ohlc(ax,df['open'],df['high'],df['low'],df['close'],width=0.6,colorup='red',colordown='green')
# 标注K线组合的位置
index = nums[nums == -100].index.values
print(index)
for i in index:
ax.annotate(s=' ',xy=(i,df['high'][i]),xytext=(i,df['high'][i] + 0.15),arrowprops={'arrowstyle':'->'})
plt.show()
###### 第三 锤子线 hammer 是一种单日的K线形态,形态就像一把锤子,通常发生在一段下跌趋势之后,预示着价格见底。通常其形态实体较短;
### 下影线很长;
nums = talib.CDLHAMMER(df['open'],df['high'],df['low'],df['close'])
#可视化
fig = plt.figure()
ax = fig.add_subplot(111)
#绘制K线图
mpf.candlestick2_ohcl(ax,df['open'],df['high'],df['low'],df['close'],width=0.6,colorup='red',colordown='green')
# 标注识别K线组合的位置
index = nums[nums==100].index.values
for i in index:
ax.annotate(s=' ',xy=(i,df['high'][i]),xytext=(i,df['high'][i] + 0.5),
arrowprops={'arrowstyle':'->'})
plt.legend()
plt.show()
##### 第四个形态上吊线
## 上吊线通常出现在价格区间的顶端,hanging man 上吊线
# 识别上吊线的K线
nums = talib.HANGINGMAN(df['open'],df['high'],df['low'],df['close'])
# 可视化
fig = plt.figure()
ax = fig.add_subplot(111)
# 绘制K线
mpf.candlestick2_ohlc(ax,df['open'],df['high'],df['low'],df['close'],width=0.6,colorup='red',colordown='green')
# 标注识别K线组合的位置
index = nums[nums== -100].index.values
for i in index:
ax.annotate(s=' ',xy=(i,df['high'][i]),xytext=(i,df['high'][i] + 0.2),
arrowprops={'arrowstyle':'->'})
plt.legend()
plt.show()
#######第五个形态组合 捉腰带线 belt hold
##捉腰带线是一种两日K先组合形态,通常出现在一段下跌趋势中,第一日为阴线,第二日为阳线,阳线的开盘价为当日最低价,收盘价接近最高价;
### 预示着价格的上涨;
# 可视化
fig=plt.figure()
ax = fig.add_subplot(111)
# 绘制K线图
mpf.candlestick2_ohlc(ax,df['open'],df['high'],df['low'],df['close'],width=0.6,colorup='red',colordown='green')
# 标注K线组合的位置
index = nums[nums==100].index.values
for i in index:
ax.annotate(s=" ",xy=(i,df['high'][i]),xytext=(i,df['high'][i] + 0.5),arrowprops={'arrowstyle':'->'})
plt.show()