-
Notifications
You must be signed in to change notification settings - Fork 143
/
rule_cross.go
45 lines (38 loc) · 978 Bytes
/
rule_cross.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
package techan
// NewCrossUpIndicatorRule returns a new rule that is satisfied when the lower indicator has crossed above the upper
// indicator.
func NewCrossUpIndicatorRule(upper, lower Indicator) Rule {
return crossRule{
upper: upper,
lower: lower,
cmp: 1,
}
}
// NewCrossDownIndicatorRule returns a new rule that is satisfied when the upper indicator has crossed below the lower
// indicator.
func NewCrossDownIndicatorRule(upper, lower Indicator) Rule {
return crossRule{
upper: lower,
lower: upper,
cmp: -1,
}
}
type crossRule struct {
upper Indicator
lower Indicator
cmp int
}
func (cr crossRule) IsSatisfied(index int, record *TradingRecord) bool {
i := index
if i == 0 {
return false
}
if cmp := cr.lower.Calculate(i).Cmp(cr.upper.Calculate(i)); cmp == 0 || cmp == cr.cmp {
for ; i >= 0; i-- {
if cmp = cr.lower.Calculate(i).Cmp(cr.upper.Calculate(i)); cmp == 0 || cmp == -cr.cmp {
return true
}
}
}
return false
}