-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheikinAshi.go
67 lines (61 loc) · 1.47 KB
/
heikinAshi.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
package shingo
// AppendHeikinAshi appends heikin ashi values for each candlestick
func (cs *Candlesticks) AppendHeikinAshi(arg IndicatorInputArg) error {
limit := arg.Limit
cs.mux.Lock()
defer cs.mux.Unlock()
total := cs.Total()
if total < 1 {
return nil
}
if limit < 1 {
limit = total
}
startIdx := total - 1 - limit
if startIdx < 0 {
startIdx = 0
}
for i := startIdx; i < total; i++ {
p := cs.ItemAtIndex(i - 1)
v := cs.ItemAtIndex(i)
close := (v.Open + v.High + v.Low + v.Close) / 4
prev := p.GetHeikinAshi()
var open, high, low float64
if prev == nil {
open = v.Open
low = v.Low
high = v.High
} else {
open = (prev.Open + prev.Close) / 2
high = findHighestValue(v.High, open, close)
low = findLowestValue(v.Low, open, close)
}
v.setHeikinAshi(open, close, high, low)
}
return nil
}
// GetHeikinAshi provides heikin ashi value for this candlestick
func (c *Candlestick) GetHeikinAshi() *HeikinAshiDelta {
if c == nil || c.Indicators == nil {
return nil
}
return c.Indicators.HeikinAshi
}
func (c *Candlestick) setHeikinAshi(open, close, high, low float64) {
if c.Indicators == nil {
c.Indicators = &Indicators{}
}
if c.Indicators.HeikinAshi == nil {
c.Indicators.HeikinAshi = &HeikinAshiDelta{
Open: open,
Close: close,
High: high,
Low: low,
}
} else {
c.Indicators.HeikinAshi.Open = open
c.Indicators.HeikinAshi.Close = close
c.Indicators.HeikinAshi.High = high
c.Indicators.HeikinAshi.Low = low
}
}