Skip to content

Commit

Permalink
indicator: canonicalize the CalculateAndUpdate method call
Browse files Browse the repository at this point in the history
also fix the xmaker boll indicator preloading
  • Loading branch information
c9s committed Jul 14, 2022
1 parent ce3f7a3 commit c27f416
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 21 deletions.
5 changes: 3 additions & 2 deletions pkg/indicator/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/alma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 7 additions & 2 deletions pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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())
Expand Down
7 changes: 6 additions & 1 deletion pkg/indicator/atrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/boll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/boll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
8 changes: 6 additions & 2 deletions pkg/indicator/dmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/indicator/inf.go
Original file line number Diff line number Diff line change
@@ -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))
}
15 changes: 11 additions & 4 deletions pkg/indicator/supertrend.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package indicator

import (
"github.com/sirupsen/logrus"
"math"
"time"

"github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/types"
)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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())
Expand All @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions pkg/indicator/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 3 additions & 1 deletion pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}

Expand Down

0 comments on commit c27f416

Please sign in to comment.