indicator: add v2 stochastic oscillator

This commit is contained in:
c9s 2023-06-01 14:43:29 +08:00
parent 1535572b43
commit 4f07a44b61
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 61 additions and 0 deletions

61
pkg/indicator/v2_stoch.go Normal file
View File

@ -0,0 +1,61 @@
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)
}
d = s.K.Tail(s.dPeriod).Mean()
s.K.Push(k)
s.D.Push(d)
s.EmitUpdate(k, d)
})
return s
}