-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcross.go
More file actions
38 lines (35 loc) · 852 Bytes
/
cross.go
File metadata and controls
38 lines (35 loc) · 852 Bytes
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
package num
// 趋势变化
const (
Unchanged = 0 // 趋势不变
BreakThrough = 1 // break through 突破
FallDrastically = -1 // fall drastically 跌破
)
// LinerTrend 线性趋势
type LinerTrend struct {
X int // 索引
State int // 状态
}
// Cross 上穿和下穿
//
// a 上穿或者下穿b的状态集合
func Cross[E Number](a, b []E) []LinerTrend {
length := len(a)
list := make([]LinerTrend, length)
count := 0
for i := 1; i < length; i++ {
front := i - 1
current := i
if a[front] < b[front] && a[current] > b[current] {
// a 上穿 b
list[count] = LinerTrend{X: current, State: BreakThrough}
count++
} else if a[front] > b[front] && a[current] < b[current] {
// a 下穿 b
list[count] = LinerTrend{X: current, State: FallDrastically}
count++
}
}
list = list[:count]
return list
}