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

View File

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

View File

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

View File

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

View File

@ -87,12 +87,17 @@ func (inc *ATRP) Length() int {
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) {
for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
}
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
inc.PushK(k)
}
inc.EmitUpdate(inc.Last())

View File

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

View File

@ -59,7 +59,7 @@ func TestBOLL(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
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.down, boll.LastDownBand(), Delta)
})

View File

@ -87,15 +87,19 @@ func (inc *DMI) Length() int {
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) {
if inc.ADX == nil {
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())
}
} else {
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())
}
}

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

View File

@ -24,6 +24,3 @@ func MapKLinePrice(kLines []types.KLine, f KLinePriceMapper) (prices []float64)
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 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])
}
}
}