-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
73 lines (62 loc) · 1.63 KB
/
source.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
package quota
import (
"strconv"
)
// Source is a target field on candle.
type Source string
// Get Retrieves the value of the target field on the candle.
func (c *Candle) Get(source Source) (float64, bool) {
switch source {
// single sources
case SourceOpen:
return c.Open, true
case SourceHigh:
return c.High, true
case SourceLow:
return c.Low, true
case SourceClose:
return c.Close, true
case SourceVolume:
return c.Volume, true
// double sources
case SourceOpenHigh:
return (c.Open + c.High) / 2, true
case SourceOpenLow:
return (c.Open + c.Low) / 2, true
case SourceOpenClose:
return (c.Open + c.Close) / 2, true
case SourceHighLow:
return (c.High + c.Low) / 2, true
case SourceHighClose:
return (c.High + c.Close) / 2, true
case SourceLowClose:
return (c.Low + c.Close) / 2, true
// triple sources
case SourceOpenHighLow:
return (c.Open + c.High + c.Low) / 3, true
case SourceOpenHighClose:
return (c.Open + c.High + c.Low) / 3, true
case SourceOpenLowClose:
return (c.Open + c.Low + c.Close) / 3, true
case SourceHighLowClose:
return (c.High + c.Low + c.Close) / 3, true
// all together
case SourceOpenHighLowClose:
return (c.Open + c.High + c.Low + c.Close) / 4, true
}
if value, ok := c.Indicators[IndicatorTag(source)]; ok {
return value, true
}
if value, err := strconv.ParseFloat(string(source), 64); err == nil {
return value, true
}
return 0., false
}
// Get retrieves value of target field on all candles.
func (q *Quota) Get(source Source) []float64 {
result := make([]float64, len(*q))
for i := range result {
result[i], _ = (*q)[i].Get(source)
}
return result
}