-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
v2_stoch.go
62 lines (48 loc) · 1.81 KB
/
v2_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
51
52
53
54
55
56
57
58
59
60
61
62
package indicator
import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)
// Stochastic Oscillator
// - https://www.investopedia.com/terms/s/stochasticoscillator.asp
//
// The Stochastic Oscillator is a technical analysis indicator that is used to identify potential overbought or oversold conditions
// in a security's price. It is calculated by taking the current closing price of the security and comparing it to the high and low prices
// over a specified period of time. This comparison is then plotted as a line on the price chart, with values above 80 indicating overbought
// conditions and values below 20 indicating oversold conditions. The Stochastic Oscillator can be used by traders to identify potential
// entry and exit points for trades, or to confirm other technical analysis signals. It is typically used in conjunction with other indicators
// to provide a more comprehensive view of the security's price.
//go:generate callbackgen -type StochStream
type StochStream struct {
types.SeriesBase
K, D floats.Slice
window int
dPeriod int
highPrices, lowPrices *PriceStream
updateCallbacks []func(k, d float64)
}
// Stochastic Oscillator
func Stoch2(source KLineSubscription, window, dPeriod int) *StochStream {
highPrices := HighPrices(source)
lowPrices := LowPrices(source)
s := &StochStream{
window: window,
dPeriod: dPeriod,
highPrices: highPrices,
lowPrices: lowPrices,
}
source.AddSubscriber(func(kLine types.KLine) {
lowest := s.lowPrices.slice.Tail(s.window).Min()
highest := s.highPrices.slice.Tail(s.window).Max()
var k float64 = 50.0
var d float64 = 0.0
if highest != lowest {
k = 100.0 * (kLine.Close.Float64() - lowest) / (highest - lowest)
}
s.K.Push(k)
d = s.K.Tail(s.dPeriod).Mean()
s.D.Push(d)
s.EmitUpdate(k, d)
})
return s
}