indicator: rewrite pivotlow indicator

This commit is contained in:
c9s 2022-07-26 17:00:17 +08:00
parent 44c3e5a6f7
commit eeab328648
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 62 additions and 23 deletions

View File

@ -12,7 +12,9 @@ import (
//go:generate callbackgen -type PivotLow //go:generate callbackgen -type PivotLow
type PivotLow struct { type PivotLow struct {
types.IntervalWindow types.IntervalWindow
types.SeriesBase
Lows types.Float64Slice
Values types.Float64Slice Values types.Float64Slice
EndTime time.Time EndTime time.Time
@ -26,33 +28,55 @@ func (inc *PivotLow) Last() float64 {
return inc.Values[len(inc.Values)-1] return inc.Values[len(inc.Values)-1]
} }
func (inc *PivotLow) CalculateAndUpdate(klines []types.KLine) { func (inc *PivotLow) Update(value float64) {
if len(klines) < inc.Window { if len(inc.Lows) == 0 {
inc.SeriesBase.Series = inc
}
inc.Lows.Push(value)
if len(inc.Lows) < inc.Window {
return return
} }
var end = len(klines) - 1 low, err := calculatePivotLow(inc.Lows, inc.Window)
var lastKLine = klines[end]
// skip old data
if inc.EndTime != zeroTime && lastKLine.GetEndTime().Before(inc.EndTime) {
return
}
recentT := klines[end-(inc.Window-1) : end+1]
l, err := calculatePivotLow(recentT, inc.Window, KLineLowPriceMapper)
if err != nil { if err != nil {
log.WithError(err).Error("can not calculate pivots") log.WithError(err).Errorf("can not calculate pivot low")
return return
} }
if l > 0.0 { if low > 0.0 {
inc.Values.Push(l) inc.Values.Push(low)
}
}
func (inc *PivotLow) PushK(k types.KLine) {
if k.EndTime.Before(inc.EndTime) {
return
} }
inc.EndTime = klines[end].GetEndTime().Time() inc.Update(k.Low.Float64())
inc.EmitUpdate(l) inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last())
}
func (inc *PivotLow) LoadK(allKLines []types.KLine) {
for _, k := range allKLines {
inc.PushK(k)
}
}
func (inc *PivotLow) CalculateAndUpdate(allKLines []types.KLine) {
if len(inc.Values) == 0 {
for _, k := range allKLines {
inc.PushK(k)
}
inc.EmitUpdate(inc.Last())
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last())
}
} }
func (inc *PivotLow) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { func (inc *PivotLow) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
@ -67,15 +91,15 @@ func (inc *PivotLow) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
} }
func calculatePivotLow(klines []types.KLine, window int, valLow KLineValueMapper) (float64, error) { func calculatePivotLow(lows types.Float64Slice, window int) (float64, error) {
length := len(klines) length := len(lows)
if length == 0 || length < window { if length == 0 || length < window {
return 0., fmt.Errorf("insufficient elements for calculating with window = %d", window) return 0., fmt.Errorf("insufficient elements for calculating with window = %d", window)
} }
var lows types.Float64Slice var pv types.Float64Slice
for _, k := range klines { for _, low := range lows {
lows.Push(valLow(k)) pv.Push(low)
} }
pl := 0. pl := 0.

View File

@ -0,0 +1,15 @@
// Code generated by "callbackgen -type PivotLow"; DO NOT EDIT.
package indicator
import ()
func (inc *PivotLow) OnUpdate(cb func(value float64)) {
inc.updateCallbacks = append(inc.updateCallbacks, cb)
}
func (inc *PivotLow) EmitUpdate(value float64) {
for _, cb := range inc.updateCallbacks {
cb(value)
}
}