-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicator_stoch.go
50 lines (41 loc) · 1.56 KB
/
indicator_stoch.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
package indicators
import (
"github.com/amir-the-h/quota"
"github.com/markcheno/go-talib"
)
// Stoch is the stochastic indicator.
type Stoch struct {
Tag quota.IndicatorTag `mapstructure:"tag"`
KTag quota.IndicatorTag `mapstructure:"kTag"`
DTag quota.IndicatorTag `mapstructure:"dTag"`
InFastKPeriod int `mapstructure:"kLength"`
InSlowKPeriod int `mapstructure:"kSmoothing"`
InKMaType talib.MaType `mapstructure:"kMaType"`
InSlowDPeriod int `mapstructure:"dSmoothing"`
InDMaType talib.MaType `mapstructure:"dMaType"`
}
// Add will calculate and add Stoch into the candle or whole quota.
func (s *Stoch) Add(q *quota.Quota, c *quota.Candle) bool {
if c != nil {
candle, i := q.Find(c.OpenTime.Unix())
if candle == nil {
return false
}
quote := (*q)[:i+1]
k, d := talib.Stoch(quote.Get(quota.SourceHigh), quote.Get(quota.SourceLow), quote.Get(quota.SourceClose), s.InFastKPeriod, s.InSlowKPeriod, s.InKMaType, s.InSlowDPeriod, s.InDMaType)
c.AddIndicator(s.KTag, k[len(k)-1])
c.AddIndicator(s.DTag, d[len(d)-1])
return true
}
k, d := talib.Stoch(q.Get(quota.SourceHigh), q.Get(quota.SourceLow), q.Get(quota.SourceClose), s.InFastKPeriod, s.InSlowKPeriod, s.InKMaType, s.InSlowDPeriod, s.InDMaType)
err := q.AddIndicator(s.KTag, k)
if err != nil {
return false
}
err = q.AddIndicator(s.DTag, d)
return err == nil
}
// Is determine provided tag belongs to this quota.Indicator or not.
func (s *Stoch) Is(tag quota.IndicatorTag) bool {
return s.Tag == tag
}