Skip to content

Commit

Permalink
indicator: keltner channel
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljirman committed Feb 3, 2024
1 parent 80ed46e commit 825be2a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/bbgo/indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (i *IndicatorSet) BOLL(iw types.IntervalWindow, k float64) *indicatorv2.BOL
return indicatorv2.BOLL(i.CLOSE(iw.Interval), iw.Window, k)
}

func (i *IndicatorSet) Keltner(iw types.IntervalWindow, atrLength int) *indicatorv2.KeltnerStream {
return indicatorv2.Keltner(i.KLines(iw.Interval), iw.Window, atrLength)
}

func (i *IndicatorSet) MACD(interval types.Interval, shortWindow, longWindow, signalWindow int) *indicatorv2.MACDStream {
return indicatorv2.MACD2(i.CLOSE(interval), shortWindow, longWindow, signalWindow)
}
Expand Down
61 changes: 61 additions & 0 deletions pkg/indicator/v2/keltner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package indicatorv2

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

type KeltnerStream struct {
types.SeriesBase

window, atrLength int

EWMA *EWMAStream
StdDev *StdDevStream
ATR *ATRStream

highPrices, lowPrices, closePrices *PriceStream

Mid *types.Float64Series
FirstUpperBand, FirstLowerBand *types.Float64Series
SecondUpperBand, SecondLowerBand *types.Float64Series
ThirdUpperBand, ThirdLowerBand *types.Float64Series
}

func Keltner(source KLineSubscription, window, atrLength int) *KeltnerStream {
atr := ATR2(source, atrLength)

highPrices := HighPrices(source)
lowPrices := LowPrices(source)
closePrices := ClosePrices(source)
ewma := EWMA2(closePrices, window)

s := &KeltnerStream{
window: window,
atrLength: atrLength,
highPrices: highPrices,
lowPrices: lowPrices,
closePrices: closePrices,
ATR: atr,
EWMA: ewma,
Mid: types.NewFloat64Series(),
FirstUpperBand: types.NewFloat64Series(),
FirstLowerBand: types.NewFloat64Series(),
SecondUpperBand: types.NewFloat64Series(),
SecondLowerBand: types.NewFloat64Series(),
ThirdUpperBand: types.NewFloat64Series(),
ThirdLowerBand: types.NewFloat64Series(),
}

source.AddSubscriber(func(kLine types.KLine) {
mid := s.EWMA.Last(0)
atr := s.ATR.Last(0)
s.Mid.PushAndEmit(mid)
s.FirstUpperBand.PushAndEmit(mid + atr)
s.FirstLowerBand.PushAndEmit(mid - atr)
s.SecondUpperBand.PushAndEmit(mid + 2*atr)
s.SecondLowerBand.PushAndEmit(mid - 2*atr)
s.ThirdUpperBand.PushAndEmit(mid + 3*atr)
s.ThirdLowerBand.PushAndEmit(mid - 3*atr)
})
return s
}

0 comments on commit 825be2a

Please sign in to comment.