Skip to content

Commit

Permalink
indicator: rewrite pivotlow indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 26, 2022
1 parent 44c3e5a commit eeab328
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
64 changes: 44 additions & 20 deletions pkg/indicator/pivot_low.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
//go:generate callbackgen -type PivotLow
type PivotLow struct {
types.IntervalWindow
types.SeriesBase

Lows types.Float64Slice
Values types.Float64Slice
EndTime time.Time

Expand All @@ -26,33 +28,55 @@ func (inc *PivotLow) Last() float64 {
return inc.Values[len(inc.Values)-1]
}

func (inc *PivotLow) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
func (inc *PivotLow) Update(value float64) {
if len(inc.Lows) == 0 {
inc.SeriesBase.Series = inc
}

var end = len(klines) - 1
var lastKLine = klines[end]
inc.Lows.Push(value)

// skip old data
if inc.EndTime != zeroTime && lastKLine.GetEndTime().Before(inc.EndTime) {
if len(inc.Lows) < inc.Window {
return
}

recentT := klines[end-(inc.Window-1) : end+1]

l, err := calculatePivotLow(recentT, inc.Window, KLineLowPriceMapper)
low, err := calculatePivotLow(inc.Lows, inc.Window)
if err != nil {
log.WithError(err).Error("can not calculate pivots")
log.WithError(err).Errorf("can not calculate pivot low")
return
}

if l > 0.0 {
inc.Values.Push(l)
if low > 0.0 {
inc.Values.Push(low)
}
}

inc.EndTime = klines[end].GetEndTime().Time()
inc.EmitUpdate(l)
func (inc *PivotLow) PushK(k types.KLine) {
if k.EndTime.Before(inc.EndTime) {
return
}

inc.Update(k.Low.Float64())
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) {
Expand All @@ -67,15 +91,15 @@ func (inc *PivotLow) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func calculatePivotLow(klines []types.KLine, window int, valLow KLineValueMapper) (float64, error) {
length := len(klines)
func calculatePivotLow(lows types.Float64Slice, window int) (float64, error) {
length := len(lows)
if length == 0 || length < window {
return 0., fmt.Errorf("insufficient elements for calculating with window = %d", window)
}

var lows types.Float64Slice
for _, k := range klines {
lows.Push(valLow(k))
var pv types.Float64Slice
for _, low := range lows {
pv.Push(low)
}

pl := 0.
Expand Down
15 changes: 15 additions & 0 deletions pkg/indicator/pivotlow_callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eeab328

Please sign in to comment.