fix pivot indicator: filter out zero lows and highs

This commit is contained in:
c9s 2022-06-10 15:17:06 +08:00
parent e60e2177f9
commit fba0a20cda
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 11 additions and 7 deletions

View File

@ -21,7 +21,7 @@ type Pivot struct {
EndTime time.Time EndTime time.Time
UpdateCallbacks []func(valueLow, valueHigh float64) updateCallbacks []func(valueLow, valueHigh float64)
} }
func (inc *Pivot) LastLow() float64 { func (inc *Pivot) LastLow() float64 {
@ -38,7 +38,7 @@ func (inc *Pivot) LastHigh() float64 {
return inc.Highs[len(inc.Highs)-1] return inc.Highs[len(inc.Highs)-1]
} }
func (inc *Pivot) calculateAndUpdate(klines []types.KLine) { func (inc *Pivot) Update(klines []types.KLine) {
if len(klines) < inc.Window { if len(klines) < inc.Window {
return return
} }
@ -59,8 +59,12 @@ func (inc *Pivot) calculateAndUpdate(klines []types.KLine) {
return return
} }
if l > 0.0 {
inc.Lows.Push(l) inc.Lows.Push(l)
}
if h > 0.0 {
inc.Highs.Push(h) inc.Highs.Push(h)
}
if len(inc.Lows) > MaxNumOfVOL { if len(inc.Lows) > MaxNumOfVOL {
inc.Lows = inc.Lows[MaxNumOfVOLTruncateSize-1:] inc.Lows = inc.Lows[MaxNumOfVOLTruncateSize-1:]
@ -80,7 +84,7 @@ func (inc *Pivot) handleKLineWindowUpdate(interval types.Interval, window types.
return return
} }
inc.calculateAndUpdate(window) inc.Update(window)
} }
func (inc *Pivot) Bind(updater KLineWindowUpdater) { func (inc *Pivot) Bind(updater KLineWindowUpdater) {

View File

@ -5,11 +5,11 @@ package indicator
import () import ()
func (inc *Pivot) OnUpdate(cb func(valueLow float64, valueHigh float64)) { func (inc *Pivot) OnUpdate(cb func(valueLow float64, valueHigh float64)) {
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) inc.updateCallbacks = append(inc.updateCallbacks, cb)
} }
func (inc *Pivot) EmitUpdate(valueLow float64, valueHigh float64) { func (inc *Pivot) EmitUpdate(valueLow float64, valueHigh float64) {
for _, cb := range inc.UpdateCallbacks { for _, cb := range inc.updateCallbacks {
cb(valueLow, valueHigh) cb(valueLow, valueHigh)
} }
} }