Skip to content

Commit

Permalink
bbgo: add ATR, ATRP, EMV to the standard indicator set
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 26, 2022
1 parent 1d6b1de commit f5e64e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 65 deletions.
15 changes: 15 additions & 0 deletions pkg/bbgo/standard_indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ func (s *StandardIndicatorSet) PivotLow(iw types.IntervalWindow) *indicator.Pivo
return inc.(*indicator.PivotLow)
}

func (s *StandardIndicatorSet) ATR(iw types.IntervalWindow) *indicator.ATR {
inc := s.allocateSimpleIndicator(&indicator.ATR{IntervalWindow: iw}, iw)
return inc.(*indicator.ATR)
}

func (s *StandardIndicatorSet) ATRP(iw types.IntervalWindow) *indicator.ATRP {
inc := s.allocateSimpleIndicator(&indicator.ATRP{IntervalWindow: iw}, iw)
return inc.(*indicator.ATRP)
}

func (s *StandardIndicatorSet) EMV(iw types.IntervalWindow) *indicator.EMV {
inc := s.allocateSimpleIndicator(&indicator.ATRP{IntervalWindow: iw}, iw)
return inc.(*indicator.EMV)
}

func (s *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH {
inc, ok := s.stoch[iw]
if !ok {
Expand Down
28 changes: 0 additions & 28 deletions pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,3 @@ func (inc *ATR) PushK(k types.KLine) {
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last())
}

func (inc *ATR) LoadK(allKlines []types.KLine) {
for _, k := range allKlines {
inc.PushK(k)
}
}

func (inc *ATR) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
}

func (inc *ATR) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines {
inc.PushK(k)
}
}

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

inc.CalculateAndUpdate(window)
}

func (inc *ATR) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
29 changes: 3 additions & 26 deletions pkg/indicator/emv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type EMV struct {
types.SeriesBase
types.IntervalWindow

prevH float64
prevL float64
Values *SMA
Expand All @@ -25,13 +26,15 @@ func (inc *EMV) Update(high, low, vol float64) {
if inc.EMVScale == 0 {
inc.EMVScale = DefaultEMVScale
}

if inc.prevH == 0 || inc.Values == nil {
inc.SeriesBase.Series = inc
inc.prevH = high
inc.prevL = low
inc.Values = &SMA{IntervalWindow: inc.IntervalWindow}
return
}

distanceMoved := (high+low)/2. - (inc.prevH+inc.prevL)/2.
boxRatio := vol / inc.EMVScale / (high - low)
result := distanceMoved / boxRatio
Expand Down Expand Up @@ -66,29 +69,3 @@ var _ types.SeriesExtend = &EMV{}
func (inc *EMV) PushK(k types.KLine) {
inc.Update(k.High.Float64(), k.Low.Float64(), k.Volume.Float64())
}

func (inc *EMV) CalculateAndUpdate(allKLines []types.KLine) {
if inc.Values == nil {
for _, k := range allKLines {
inc.PushK(k)
if inc.Length() > 0 {
inc.EmitUpdate(inc.Last())
}
}
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last())
}
}

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

func (inc *EMV) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
11 changes: 0 additions & 11 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func (inc *EWMA) Length() int {
return len(inc.Values)
}

func (inc *EWMA) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
}

func (inc *EWMA) PushK(k types.KLine) {
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
return
Expand All @@ -72,13 +68,6 @@ func (inc *EWMA) PushK(k types.KLine) {
inc.EmitUpdate(inc.Last())
}

func (inc *EWMA) LoadK(allKLines []types.KLine) {
for _, k := range allKLines {
inc.PushK(k)
}
inc.EmitUpdate(inc.Last())
}

func CalculateKLinesEMA(allKLines []types.KLine, priceF KLinePriceMapper, window int) float64 {
var multiplier = 2.0 / (float64(window) + 1)
return ewma(MapKLinePrice(allKLines, priceF), multiplier)
Expand Down

0 comments on commit f5e64e8

Please sign in to comment.