Skip to content

Commit

Permalink
indicator: drop the legacy CalculateAndUpdate for standard indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 26, 2022
1 parent 8bf9b28 commit 0df321c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 73 deletions.
16 changes: 12 additions & 4 deletions pkg/bbgo/standard_indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (set *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64
inc = &indicator.BOLL{IntervalWindow: iw, K: bandWidth}

if klines, ok := set.store.KLinesOfInterval(iw.Interval); ok {
inc.LoadK(*klines)
for _, k := range *klines {
inc.PushK(k)
}
}

if debugBOLL {
Expand All @@ -78,7 +80,9 @@ func (set *StandardIndicatorSet) SMA(iw types.IntervalWindow) *indicator.SMA {
inc = &indicator.SMA{IntervalWindow: iw}

if klines, ok := set.store.KLinesOfInterval(iw.Interval); ok {
inc.LoadK(*klines)
for _, k := range *klines {
inc.PushK(k)
}
}

if debugSMA {
Expand All @@ -101,7 +105,9 @@ func (set *StandardIndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMA {
inc = &indicator.EWMA{IntervalWindow: iw}

if klines, ok := set.store.KLinesOfInterval(iw.Interval); ok {
inc.LoadK(*klines)
for _, k := range *klines {
inc.PushK(k)
}
}

if debugEWMA {
Expand All @@ -123,7 +129,9 @@ func (set *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH
inc = &indicator.STOCH{IntervalWindow: iw}

if klines, ok := set.store.KLinesOfInterval(iw.Interval); ok {
inc.LoadK(*klines)
for _, k := range *klines {
inc.PushK(k)
}
}

inc.BindK(set.stream, set.Symbol, iw.Interval)
Expand Down
25 changes: 0 additions & 25 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,6 @@ func (inc *EWMA) Length() int {
return len(inc.Values)
}

func (inc *EWMA) 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 *EWMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}

inc.CalculateAndUpdate(window)
}

func (inc *EWMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func (inc *EWMA) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,6 @@ func (inc *SMA) LoadK(allKLines []types.KLine) {
}
}

func (inc *SMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.rawValues == nil {
inc.LoadK(allKLines)
} else {
var last = allKLines[len(allKLines)-1]
inc.PushK(last)
}

}

func (inc *SMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}

inc.CalculateAndUpdate(window)
}

func (inc *SMA) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func calculateSMA(kLines []types.KLine, window int, priceF KLinePriceMapper) (float64, error) {
length := len(kLines)
if length == 0 || length < window {
Expand Down
22 changes: 0 additions & 22 deletions pkg/indicator/stoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,6 @@ func (inc *STOCH) LoadK(allKLines []types.KLine) {
}
}

func (inc *STOCH) CalculateAndUpdate(kLines []types.KLine) {
if len(kLines) < inc.Window || len(kLines) < DPeriod {
return
}

for _, k := range kLines {
inc.PushK(k)
}
}

func (inc *STOCH) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}

inc.CalculateAndUpdate(window)
}

func (inc *STOCH) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func (inc *STOCH) GetD() types.Series {
return &inc.D
}
Expand Down

0 comments on commit 0df321c

Please sign in to comment.