bbgo/pkg/indicator/v2/nr.go

71 lines
1.3 KiB
Go
Raw Normal View History

2024-08-31 08:47:20 +00:00
package indicatorv2
import (
"git.qtrade.icu/lychiyu/bbgo/pkg/types"
)
type NRStrean struct {
*types.Float64Series
nrCount int
kLines []types.KLine
strictMode bool
NrKLine types.KLine
}
func NR(source KLineSubscription, nrCount int, strictMode bool) *NRStrean {
s := &NRStrean{
nrCount: nrCount,
Float64Series: types.NewFloat64Series(),
}
source.AddSubscriber(func(k types.KLine) {
s.calculateAndPush(k)
})
return s
}
func (s *NRStrean) calculateAndPush(k types.KLine) {
s.kLines = append(s.kLines, k)
if len(s.kLines) < s.nrCount {
return
}
nr := s.kLines[len(s.kLines)-1]
preNr := s.kLines[len(s.kLines)-2]
isNR := true
if preNr.High < nr.High || preNr.Low > nr.Low {
isNR = false
return
}
for i := len(s.kLines) - s.nrCount; i < len(s.kLines); i++ {
// 这种是所有的kline都要高于nr
//if s.CalKLines[i].High > nr.High || s.CalKLines[i].Low < nr.Low {
// isNR = false
// break
//}
if s.strictMode {
if s.kLines[i].High-s.kLines[i].Low < nr.High-nr.Low {
isNR = false
break
}
} else {
if (s.kLines[i].High-s.kLines[i].Low)/s.kLines[i].Low < (nr.High-nr.Low)/nr.Low {
isNR = false
break
}
}
}
if isNR {
s.NrKLine = nr
s.PushAndEmit(nr.High.Float64())
}
return
}