-
Notifications
You must be signed in to change notification settings - Fork 0
/
highest.go
78 lines (71 loc) · 1.49 KB
/
highest.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
package shingo
import (
"github.com/pkg/errors"
)
// AppendHighest generates highest value within that period of time up to that candlestick
func (cs *Candlesticks) AppendHighest(arg IndicatorInputArg) error {
p := arg.Period
if p < 1 {
return errors.New("Period must be larger than 0")
}
t := cs.Total()
l := arg.Limit
if l < 1 {
l = t
}
startIdx := t - 1 - l
if startIdx < 0 {
startIdx = 0
}
lastHighIdx := -1
for i := startIdx; i < t; i++ {
v := cs.ItemAtIndex(i)
var highest float64
if lastHighIdx >= 0 && lastHighIdx > i-p+1 {
// get from previous high
pv := cs.ItemAtIndex(i - 1)
if pv != nil {
lh := pv.GetHighest(p)
if lh == nil {
continue
}
lastHigh := *lh
if lastHigh > v.Close {
highest = lastHigh
} else {
highest = v.Close
lastHighIdx = i
}
}
}
if highest == 0.0 {
highest = v.Close
lastHighIdx = i
for j := i - p + 1; j < i; j++ {
g := cs.ItemAtIndex(j)
if g == nil {
continue
}
if g.Close > highest {
highest = g.Close
lastHighIdx = j
}
}
}
if v.Indicators == nil {
v.Indicators = &Indicators{}
}
if v.Indicators.Highest == nil {
v.Indicators.Highest = make(map[int]*float64)
}
v.Indicators.Highest[p] = &highest
}
return nil
}
// GetHighest gets highest value for given past periods
func (c *Candlestick) GetHighest(period int) *float64 {
if c.Indicators == nil || c.Indicators.Highest == nil {
return nil
}
return c.Indicators.Highest[period]
}