-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
149 lines (132 loc) · 3.21 KB
/
common.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package talib
import (
"math"
)
func (kline Kline) isYin() bool {
return kline.Close < kline.Open
}
func (kline Kline) isYang() bool {
return kline.Close > kline.Open
}
// k1 contain of k2
func contain(k1, k2 Kline) bool {
if k1.isYang() && k2.isYang() {
if k1.Open < k2.Open && k1.Close > k2.Close {
return true
}
}
if k1.isYin() && k2.isYin() {
if k1.Open > k2.Open && k1.Close < k2.Close {
return true
}
}
return false
}
// split klines by days
func split(klines []Kline, num int) []Kline {
if len(klines) <= num {
return klines
}
return klines[:num]
}
// reverse the klines
func reverse(klines []Kline) []Kline {
for from, to := 0, len(klines)-1; from < to; from, to = from+1, to-1 {
klines[from], klines[to] = klines[to], klines[from]
}
return klines
}
// 实体比较小,一般无上影线(或者特别短),但是下影线特别长,一般是实体的两倍
func (kline Kline) isHammerLine() bool {
Entity := math.Abs(kline.Close - kline.Open)
var UpperShadow float64
var LowerShadow float64
if kline.isYang() {
UpperShadow = kline.High - kline.Close
LowerShadow = kline.Open - kline.Low
if UpperShadow < 0.1*Entity && LowerShadow > 2*Entity {
return true
}
} else if kline.isYin() {
UpperShadow = kline.High - kline.Open
LowerShadow = kline.Close - kline.Low
if UpperShadow < 0.1*Entity && LowerShadow > 2*Entity {
return true
}
}
return false
}
//上影线较长,长度为实体2倍以上, 无下影线,在下跌趋势底部,预示着趋势反转。
func (kline Kline) isinvertedHammer() bool {
Entity := math.Abs(kline.Close - kline.Open)
var UpperShadow float64
var LowerShadow float64
if kline.isYang() {
UpperShadow = kline.High - kline.Close
LowerShadow = kline.Open - kline.Low
if UpperShadow > 2*Entity && LowerShadow < 0.1*Entity {
return true
}
} else if kline.isYin() {
UpperShadow = kline.High - kline.Open
LowerShadow = kline.Close - kline.Low
if UpperShadow > 2*Entity && LowerShadow < 0.1*Entity {
return true
}
}
return false
}
//十字星
func (kline Kline) isStar() bool {
return kline.isMinimum()
}
//长? 短? 略低于? 如何定义? 暂时按照股票的标准
//极小
func (kline Kline) isMinimum() bool {
Entity := math.Abs(kline.Close - kline.Open)
if Entity/kline.Open < 0.006 {
return true
}
return false
}
//小
func (kline Kline) isSmall() bool {
Entity := math.Abs(kline.Close - kline.Open)
Amplitude := Entity / kline.Open
if Amplitude <= 0.015 && Amplitude >= 0.006 {
return true
}
return false
}
//中
func (kline Kline) isMiddle() bool {
Entity := math.Abs(kline.Close - kline.Open)
Amplitude := Entity / kline.Open
if Amplitude <= 0.035 && Amplitude > 0.015 {
return true
}
return false
}
//大
func (kline Kline) isBig() bool {
Entity := math.Abs(kline.Close - kline.Open)
Amplitude := Entity / kline.Open
if Amplitude > 0.035 {
return true
}
return false
}
//附近? 略低于? 略高于?
// 0 : 不在附近 -1 :略低于 1 : 略高于
// -1, 1 都代表在附近
func isNear(k1, k2 float64) int {
Entity := math.Abs(k1 - k2)
Amplitude := Entity / k1
if k1 >= k2 && Amplitude <= 0.006 {
return 1
}
if k1 < k2 && Amplitude <= 0.006 {
return -1
}
return 0
}