indicator: canonicalize the CalculateAndUpdate method call

also fix the xmaker boll indicator preloading
This commit is contained in:
c9s 2022-07-14 00:41:20 +08:00
parent ce3f7a3d51
commit c27f416dbc
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
12 changed files with 53 additions and 21 deletions

View File

@ -59,7 +59,7 @@ func (inc *AD) Length() int {
var _ types.SeriesExtend = &AD{} var _ types.SeriesExtend = &AD{}
func (inc *AD) calculateAndUpdate(kLines []types.KLine) { func (inc *AD) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines { for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue continue
@ -70,12 +70,13 @@ func (inc *AD) calculateAndUpdate(kLines []types.KLine) {
inc.EmitUpdate(inc.Last()) inc.EmitUpdate(inc.Last())
inc.EndTime = kLines[len(kLines)-1].EndTime.Time() inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
} }
func (inc *AD) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { func (inc *AD) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval { if inc.Interval != interval {
return return
} }
inc.calculateAndUpdate(window) inc.CalculateAndUpdate(window)
} }
func (inc *AD) Bind(updater KLineWindowUpdater) { func (inc *AD) Bind(updater KLineWindowUpdater) {

View File

@ -75,7 +75,7 @@ func (inc *ALMA) Length() int {
var _ types.SeriesExtend = &ALMA{} var _ types.SeriesExtend = &ALMA{}
func (inc *ALMA) calculateAndUpdate(allKLines []types.KLine) { func (inc *ALMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.input == nil { if inc.input == nil {
for _, k := range allKLines { for _, k := range allKLines {
inc.Update(k.Close.Float64()) inc.Update(k.Close.Float64())
@ -91,7 +91,7 @@ func (inc *ALMA) handleKLineWindowUpdate(interval types.Interval, window types.K
if inc.Interval != interval { if inc.Interval != interval {
return return
} }
inc.calculateAndUpdate(window) inc.CalculateAndUpdate(window)
} }
func (inc *ALMA) Bind(updater KLineWindowUpdater) { func (inc *ALMA) Bind(updater KLineWindowUpdater) {

View File

@ -52,7 +52,7 @@ func Test_ALMA(t *testing.T) {
Offset: 0.9, Offset: 0.9,
Sigma: 6, Sigma: 6,
} }
alma.calculateAndUpdate(tt.kLines) alma.CalculateAndUpdate(tt.kLines)
assert.InDelta(t, tt.want, alma.Last(), Delta) assert.InDelta(t, tt.want, alma.Last(), Delta)
assert.InDelta(t, tt.next, alma.Index(1), Delta) assert.InDelta(t, tt.next, alma.Index(1), Delta)
assert.Equal(t, tt.all, alma.Length()) assert.Equal(t, tt.all, alma.Length())

View File

@ -20,6 +20,8 @@ type ATR struct {
UpdateCallbacks []func(value float64) UpdateCallbacks []func(value float64)
} }
var _ types.SeriesExtend = &ATR{}
func (inc *ATR) Update(high, low, cloze float64) { func (inc *ATR) Update(high, low, cloze float64) {
if inc.Window <= 0 { if inc.Window <= 0 {
panic("window must be greater than 0") panic("window must be greater than 0")
@ -72,17 +74,20 @@ func (inc *ATR) Length() int {
if inc.RMA == nil { if inc.RMA == nil {
return 0 return 0
} }
return inc.RMA.Length() return inc.RMA.Length()
} }
var _ types.SeriesExtend = &ATR{} func (inc *ATR) PushK(k types.KLine) {
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
}
func (inc *ATR) CalculateAndUpdate(kLines []types.KLine) { func (inc *ATR) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines { for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue continue
} }
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64()) inc.PushK(k)
} }
inc.EmitUpdate(inc.Last()) inc.EmitUpdate(inc.Last())

View File

@ -87,12 +87,17 @@ func (inc *ATRP) Length() int {
var _ types.SeriesExtend = &ATRP{} var _ types.SeriesExtend = &ATRP{}
func (inc *ATRP) PushK(k types.KLine) {
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
}
func (inc *ATRP) CalculateAndUpdate(kLines []types.KLine) { func (inc *ATRP) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines { for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue continue
} }
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
inc.PushK(k)
} }
inc.EmitUpdate(inc.Last()) inc.EmitUpdate(inc.Last())

View File

@ -88,7 +88,7 @@ func (inc *BOLL) LastSMA() float64 {
return 0.0 return 0.0
} }
func (inc *BOLL) Update(kLines []types.KLine) { func (inc *BOLL) CalculateAndUpdate(kLines []types.KLine) {
if len(kLines) < inc.Window { if len(kLines) < inc.Window {
return return
} }
@ -142,7 +142,7 @@ func (inc *BOLL) handleKLineWindowUpdate(interval types.Interval, window types.K
return return
} }
inc.Update(window) inc.CalculateAndUpdate(window)
} }
func (inc *BOLL) Bind(updater KLineWindowUpdater) { func (inc *BOLL) Bind(updater KLineWindowUpdater) {

View File

@ -59,7 +59,7 @@ func TestBOLL(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
boll := BOLL{IntervalWindow: types.IntervalWindow{Window: tt.window}, K: tt.k} boll := BOLL{IntervalWindow: types.IntervalWindow{Window: tt.window}, K: tt.k}
boll.Update(tt.kLines) boll.CalculateAndUpdate(tt.kLines)
assert.InDelta(t, tt.up, boll.LastUpBand(), Delta) assert.InDelta(t, tt.up, boll.LastUpBand(), Delta)
assert.InDelta(t, tt.down, boll.LastDownBand(), Delta) assert.InDelta(t, tt.down, boll.LastDownBand(), Delta)
}) })

View File

@ -87,15 +87,19 @@ func (inc *DMI) Length() int {
return inc.ADX.Length() return inc.ADX.Length()
} }
func (inc *DMI) PushK(k types.KLine) {
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
}
func (inc *DMI) calculateAndUpdate(allKLines []types.KLine) { func (inc *DMI) calculateAndUpdate(allKLines []types.KLine) {
if inc.ADX == nil { if inc.ADX == nil {
for _, k := range allKLines { for _, k := range allKLines {
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64()) inc.PushK(k)
inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last()) inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last())
} }
} else { } else {
k := allKLines[len(allKLines)-1] k := allKLines[len(allKLines)-1]
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64()) inc.PushK(k)
inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last()) inc.EmitUpdate(inc.DIPlus.Last(), inc.DIMinus.Last(), inc.ADX.Last())
} }
} }

11
pkg/indicator/inf.go Normal file
View File

@ -0,0 +1,11 @@
package indicator
import "github.com/c9s/bbgo/pkg/types"
type KLineWindowUpdater interface {
OnKLineWindowUpdate(func(interval types.Interval, window types.KLineWindow))
}
type KLineCloseHandler interface {
OnKLineClosed(func(k types.KLine))
}

View File

@ -1,10 +1,11 @@
package indicator package indicator
import ( import (
"github.com/sirupsen/logrus"
"math" "math"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -50,6 +51,7 @@ func (inc *Supertrend) Index(i int) float64 {
func (inc *Supertrend) Length() int { func (inc *Supertrend) Length() int {
return len(inc.trendPrices) return len(inc.trendPrices)
} }
func (inc *Supertrend) Update(highPrice, lowPrice, closePrice float64) { func (inc *Supertrend) Update(highPrice, lowPrice, closePrice float64) {
if inc.Window <= 0 { if inc.Window <= 0 {
panic("window must be greater than 0") panic("window must be greater than 0")
@ -127,12 +129,17 @@ func (inc *Supertrend) GetSignal() types.Direction {
var _ types.SeriesExtend = &Supertrend{} var _ types.SeriesExtend = &Supertrend{}
func (inc *Supertrend) calculateAndUpdate(kLines []types.KLine) { func (inc *Supertrend) PushK(k types.KLine) {
inc.Update(k.GetHigh().Float64(), k.GetLow().Float64(), k.GetClose().Float64())
}
func (inc *Supertrend) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines { for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue continue
} }
inc.Update(k.GetHigh().Float64(), k.GetLow().Float64(), k.GetClose().Float64())
inc.PushK(k)
} }
inc.EmitUpdate(inc.Last()) inc.EmitUpdate(inc.Last())
@ -144,7 +151,7 @@ func (inc *Supertrend) handleKLineWindowUpdate(interval types.Interval, window t
return return
} }
inc.calculateAndUpdate(window) inc.CalculateAndUpdate(window)
} }
func (inc *Supertrend) Bind(updater KLineWindowUpdater) { func (inc *Supertrend) Bind(updater KLineWindowUpdater) {

View File

@ -24,6 +24,3 @@ func MapKLinePrice(kLines []types.KLine, f KLinePriceMapper) (prices []float64)
return prices return prices
} }
type KLineWindowUpdater interface {
OnKLineWindowUpdate(func(interval types.Interval, window types.KLineWindow))
}

View File

@ -693,7 +693,9 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
if store, ok := s.sourceSession.MarketDataStore(s.Symbol); ok { if store, ok := s.sourceSession.MarketDataStore(s.Symbol); ok {
if klines, ok2 := store.KLinesOfInterval(s.BollBandInterval); ok2 { if klines, ok2 := store.KLinesOfInterval(s.BollBandInterval); ok2 {
s.boll.Update(*klines) for i := 0; i < len(*klines); i++ {
s.boll.CalculateAndUpdate((*klines)[0 : i+1])
}
} }
} }