-
Notifications
You must be signed in to change notification settings - Fork 0
/
ichimokuCloud.go
155 lines (141 loc) · 3.51 KB
/
ichimokuCloud.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
150
151
152
153
154
155
package shingo
import (
"fmt"
"math"
)
// AppendIchimokuCloud appends ichimoku cloud indicator to candlestick
func (cs *Candlesticks) AppendIchimokuCloud(arg IndicatorInputArg) (err error) {
limit := arg.Limit
tenkan := arg.IchimokuCloudTenkan
kijun := arg.IchimokuCloudKijun
// sb := arg.IchimokuCloudSenkouB
// chik := arg.IchimokuCloudChikou
cs.mux.Lock()
defer cs.mux.Unlock()
if limit < 1 {
limit = cs.Total()
}
max := math.Max(float64(tenkan), float64(kijun))
cl := cs.Total()
startCalcIdx := int(math.Max(float64(cl)-max-float64(limit), 0.0))
startIndicatorIdx := int(math.Max(float64(cl-limit), 0.0))
var tenkanHIdx, kijunHIdx int = -1, -1
var tenkanHVal, kijunHVal float64
var tenkanLIdx, kijunLIdx int = -1, -1
var tenkanLVal, kijunLVal float64 = math.Inf(+1), math.Inf(+1)
for i := startCalcIdx; i < cl; i++ {
v := cs.ItemAtIndex(i)
if v.High > tenkanHVal {
tenkanHVal = v.High
tenkanHIdx = i
}
if v.Low < tenkanLVal {
tenkanLVal = v.Low
tenkanLIdx = i
}
if v.High > kijunHVal {
kijunHVal = v.High
kijunHIdx = i
}
if v.Low < kijunLVal {
kijunLVal = v.Low
kijunLIdx = i
}
// not reaching enough periods to determine tenkan
if i-startCalcIdx < tenkan-1 {
continue
}
// append tenkan
if v.High > tenkanHVal {
tenkanHVal = v.High
tenkanHIdx = i
}
if v.High > kijunHVal {
kijunHVal = v.High
kijunHIdx = i
}
if v.Low < tenkanLVal {
tenkanLVal = v.Low
tenkanLIdx = i
}
if v.Low < kijunLVal {
kijunLVal = v.Low
kijunLIdx = i
}
// make sure the highest/lowest value
// is within the range
if tenkanHIdx < i-tenkan+1 || tenkanLIdx < i-tenkan+1 {
// we have a problem. we'd need to re-calculate the highest value and index
tenkanHVal = 0
tenkanLVal = math.Inf(+1)
for j := (i - tenkan + 1); j <= i; j++ {
vj := cs.ItemAtIndex(j)
if vj.High > tenkanHVal {
tenkanHVal = vj.High
tenkanHIdx = j
}
if vj.Low < tenkanLVal {
tenkanLVal = vj.Low
tenkanLIdx = j
}
}
}
tenkanVal := (tenkanHVal + tenkanLVal) / 2.0
if i-startCalcIdx < kijun-1 {
// can't append kijun value yet just append tenkan and move on
if i < startIndicatorIdx {
continue
}
v.setIchimokuCloud(kijun, tenkan, 0, tenkanVal)
continue
}
if kijunHIdx < i-kijun+1 || kijunLIdx < i-kijun+1 {
kijunHVal = 0
kijunLVal = math.Inf(+1)
for j := (i - kijun + 1); j < i; j++ {
vj := cs.ItemAtIndex(j)
if vj.High > kijunHVal {
kijunHVal = vj.High
kijunHIdx = j
}
if vj.Low < kijunLVal {
kijunLVal = vj.Low
kijunLIdx = j
}
}
}
kijunVal := (kijunHVal + kijunLVal) / 2.0
if i < startIndicatorIdx {
continue
}
v.setIchimokuCloud(kijun, tenkan, kijunVal, tenkanVal)
}
return
}
// GetIchimokuCloud gets ichimoku cloud value for this candlestick and given kijun,tenkan values
func (c *Candlestick) GetIchimokuCloud(k, t int) *IchimokuCloudDelta {
if c.Indicators == nil {
return nil
}
key := fmt.Sprintf("%d,%d", t, k)
if c.Indicators.IchimokuClouds == nil {
return nil
}
return c.Indicators.IchimokuClouds[key]
}
func (c *Candlestick) setIchimokuCloud(k, t int, kv, tv float64) {
if c.Indicators == nil {
c.Indicators = &Indicators{}
}
key := fmt.Sprintf("%d,%d", t, k)
if c.Indicators.IchimokuClouds == nil {
c.Indicators.IchimokuClouds = make(map[string]*IchimokuCloudDelta)
}
if c.Indicators.IchimokuClouds[key] == nil {
c.Indicators.IchimokuClouds[key] = &IchimokuCloudDelta{
Tenkan: tv,
Kijun: kv,
SenkouA: (tv + kv) / 2,
}
}
}