indicator: split pivot low indicator

This commit is contained in:
c9s 2022-07-26 16:50:45 +08:00
parent 5bb1722007
commit 44c3e5a6f7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 98 additions and 2 deletions

View File

@ -0,0 +1,87 @@
package indicator
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type PivotLow
type PivotLow struct {
types.IntervalWindow
Values types.Float64Slice
EndTime time.Time
updateCallbacks []func(value float64)
}
func (inc *PivotLow) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}
return inc.Values[len(inc.Values)-1]
}
func (inc *PivotLow) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
var end = len(klines) - 1
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 {
log.WithError(err).Error("can not calculate pivots")
return
}
if l > 0.0 {
inc.Values.Push(l)
}
inc.EndTime = klines[end].GetEndTime().Time()
inc.EmitUpdate(l)
}
func (inc *PivotLow) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
}
func (inc *PivotLow) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
func calculatePivotLow(klines []types.KLine, window int, valLow KLineValueMapper) (float64, error) {
length := len(klines)
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))
}
pl := 0.
if lows.Min() == lows.Index(int(window/2.)-1) {
pl = lows.Min()
}
return pl, nil
}

View File

@ -120,17 +120,26 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
}) })
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) { session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
lastLow := fixedpoint.NewFromFloat(s.pivot.LastLow()) lastLow := fixedpoint.NewFromFloat(s.pivot.LastLow())
if lastLow.IsZero() { if lastLow.IsZero() {
return return
} }
if lastLow.Compare(s.lastLow) != 0 { if lastLow.Compare(s.lastLow) == 0 {
bbgo.Notify("%s new pivot low: %f", s.Symbol, s.pivot.LastLow()) return
} }
s.lastLow = lastLow s.lastLow = lastLow
s.pivotLowPrices = append(s.pivotLowPrices, s.lastLow) s.pivotLowPrices = append(s.pivotLowPrices, s.lastLow)
// when position is opened, do not send pivot low notify
if position.IsOpened(kline.Close) {
return
}
bbgo.Notify("%s new pivot low: %f", s.Symbol, s.pivot.LastLow())
})) }))
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, types.Interval1m, func(kline types.KLine) { session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, types.Interval1m, func(kline types.KLine) {