indicator: keltner channel

This commit is contained in:
Michal Jirman 2024-02-03 17:13:51 +05:45
parent 80ed46e23a
commit 825be2a08e
No known key found for this signature in database
GPG Key ID: 9503107A53007ACF
2 changed files with 65 additions and 0 deletions

View File

@ -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)
}

View File

@ -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
}