fix all indicators for KLineCalculateUpdater interface

This commit is contained in:
c9s 2022-07-14 10:28:53 +08:00
parent 3f800243a7
commit cb481c660f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
24 changed files with 73 additions and 58 deletions

View File

@ -54,7 +54,7 @@ func (inc *DEMA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *DEMA) calculateAndUpdate(allKLines []types.KLine) {
func (inc *DEMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.a1 == nil {
for _, k := range allKLines {
inc.PushK(k)
@ -73,7 +73,7 @@ func (inc *DEMA) handleKLineWindowUpdate(interval types.Interval, window types.K
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *DEMA) Bind(updater KLineWindowUpdater) {

View File

@ -45,7 +45,7 @@ func Test_DEMA(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dema := DEMA{IntervalWindow: types.IntervalWindow{Window: 16}}
dema.calculateAndUpdate(tt.kLines)
dema.CalculateAndUpdate(tt.kLines)
last := dema.Last()
assert.InDelta(t, tt.want, last, Delta)
assert.InDelta(t, tt.next, dema.Index(1), Delta)

View File

@ -91,7 +91,7 @@ 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) {
func (inc *DMI) CalculateAndUpdate(allKLines []types.KLine) {
if inc.ADX == nil {
for _, k := range allKLines {
inc.PushK(k)
@ -109,7 +109,7 @@ func (inc *DMI) handleKLineWindowUpdate(interval types.Interval, window types.KL
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *DMI) Bind(updater KLineWindowUpdater) {

View File

@ -77,7 +77,7 @@ func Test_DMI(t *testing.T) {
IntervalWindow: types.IntervalWindow{Window: 5},
ADXSmoothing: 14,
}
dmi.calculateAndUpdate(tt.klines)
dmi.CalculateAndUpdate(tt.klines)
assert.InDelta(t, dmi.GetDIPlus().Last(), tt.want.dip, Delta)
assert.InDelta(t, dmi.GetDIMinus().Last(), tt.want.dim, Delta)
assert.InDelta(t, dmi.GetADX().Last(), tt.want.adx, Delta)

View File

@ -55,7 +55,7 @@ func (inc *HULL) Length() int {
var _ types.SeriesExtend = &HULL{}
// TODO: should we just ignore the possible overlapping?
func (inc *HULL) calculateAndUpdate(allKLines []types.KLine) {
func (inc *HULL) CalculateAndUpdate(allKLines []types.KLine) {
doable := false
if inc.ma1 == nil || inc.ma1.Length() == 0 {
doable = true
@ -76,7 +76,7 @@ func (inc *HULL) handleKLineWindowUpdate(interval types.Interval, window types.K
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *HULL) Bind(updater KLineWindowUpdater) {

View File

@ -45,7 +45,7 @@ func Test_HULL(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hull := HULL{IntervalWindow: types.IntervalWindow{Window: 16}}
hull.calculateAndUpdate(tt.kLines)
hull.CalculateAndUpdate(tt.kLines)
last := hull.Last()
assert.InDelta(t, tt.want, last, Delta)
assert.InDelta(t, tt.next, hull.Index(1), Delta)

View File

@ -16,6 +16,6 @@ type KLinePusher interface {
PushK(k types.KLine)
}
type KLineCalculator interface {
type KLineCalculateUpdater interface {
CalculateAndUpdate(allKLines []types.KLine)
}

View File

@ -19,9 +19,9 @@ type MACD struct {
ShortPeriod int // 12
LongPeriod int // 26
Values types.Float64Slice
FastEWMA EWMA
SlowEWMA EWMA
SignalLine EWMA
FastEWMA *EWMA
SlowEWMA *EWMA
SignalLine *EWMA
Histogram types.Float64Slice
EndTime time.Time
@ -31,9 +31,9 @@ type MACD struct {
func (inc *MACD) Update(x float64) {
if len(inc.Values) == 0 {
inc.FastEWMA = EWMA{IntervalWindow: types.IntervalWindow{Window: inc.ShortPeriod}}
inc.SlowEWMA = EWMA{IntervalWindow: types.IntervalWindow{Window: inc.LongPeriod}}
inc.SignalLine = EWMA{IntervalWindow: types.IntervalWindow{Window: inc.Window}}
inc.FastEWMA = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.ShortPeriod}}
inc.SlowEWMA = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.LongPeriod}}
inc.SignalLine = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.Window}}
}
// update fast and slow ema
@ -60,25 +60,38 @@ func (inc *MACD) calculateMACD(kLines []types.KLine, priceF KLinePriceMapper) fl
return inc.Values[len(inc.Values)-1]
}
func (inc *MACD) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}
return inc.Values[len(inc.Values)-1]
}
func (inc *MACD) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *MACD) CalculateAndUpdate(kLines []types.KLine) {
if len(kLines) == 0 {
func (inc *MACD) CalculateAndUpdate(allKLines []types.KLine) {
if len(allKLines) == 0 {
return
}
for _, k := range kLines {
last := allKLines[len(allKLines)-1]
if len(inc.Values) == 0 {
for _, k := range allKLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
}
inc.PushK(k)
}
} else {
inc.PushK(last)
}
inc.EmitUpdate(inc.Values[len(inc.Values)-1])
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
inc.EmitUpdate(inc.Last())
inc.EndTime = last.EndTime.Time()
}
func (inc *MACD) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
@ -102,6 +115,7 @@ func (inc *MACDValues) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}
return inc.Values[len(inc.Values)-1]
}
@ -110,6 +124,7 @@ func (inc *MACDValues) Index(i int) float64 {
if length == 0 || length-1-i < 0 {
return 0.0
}
return inc.Values[length-1+i]
}
@ -124,5 +139,5 @@ func (inc *MACD) MACD() types.SeriesExtend {
}
func (inc *MACD) Singals() types.SeriesExtend {
return &inc.SignalLine
return inc.SignalLine
}

View File

@ -71,7 +71,7 @@ func (inc *RMA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *RMA) calculateAndUpdate(kLines []types.KLine) {
func (inc *RMA) CalculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
@ -88,7 +88,7 @@ func (inc *RMA) handleKLineWindowUpdate(interval types.Interval, window types.KL
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *RMA) Bind(updater KLineWindowUpdater) {

View File

@ -74,7 +74,7 @@ func (inc *VIDYA) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}
func (inc *VIDYA) calculateAndUpdate(allKLines []types.KLine) {
func (inc *VIDYA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.input.Length() == 0 {
for _, k := range allKLines {
inc.PushK(k)
@ -92,7 +92,7 @@ func (inc *VIDYA) handleKLineWindowUpdate(interval types.Interval, window types.
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *VIDYA) Bind(updater KLineWindowUpdater) {

View File

@ -29,7 +29,7 @@ func (inc *Correlation) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *Correlation) calculateAndUpdate(klines []types.KLine) {
func (inc *Correlation) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -64,7 +64,7 @@ func (inc *Correlation) handleKLineWindowUpdate(interval types.Interval, window
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *Correlation) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *A18) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *A18) calculateAndUpdate(klines []types.KLine) {
func (inc *A18) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *A18) handleKLineWindowUpdate(interval types.Interval, window types.KL
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *A18) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *A2) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *A2) calculateAndUpdate(klines []types.KLine) {
func (inc *A2) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *A2) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *A2) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -27,7 +27,7 @@ func (inc *A3) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *A3) calculateAndUpdate(klines []types.KLine) {
func (inc *A3) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -63,7 +63,7 @@ func (inc *A3) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *A3) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *A34) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *A34) calculateAndUpdate(klines []types.KLine) {
func (inc *A34) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *A34) handleKLineWindowUpdate(interval types.Interval, window types.KL
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *A34) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -30,7 +30,7 @@ func (inc *R) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *R) calculateAndUpdate(klines []types.KLine) {
func (inc *R) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -66,7 +66,7 @@ func (inc *R) handleKLineWindowUpdate(interval types.Interval, window types.KLin
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *R) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *S0) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S0) calculateAndUpdate(klines []types.KLine) {
func (inc *S0) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *S0) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S0) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -25,7 +25,7 @@ func (inc *S1) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S1) calculateAndUpdate(klines []types.KLine) {
func (inc *S1) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -60,7 +60,7 @@ func (inc *S1) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S1) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -25,7 +25,7 @@ func (inc *S2) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S2) calculateAndUpdate(klines []types.KLine) {
func (inc *S2) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -60,7 +60,7 @@ func (inc *S2) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S2) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *S3) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S3) calculateAndUpdate(klines []types.KLine) {
func (inc *S3) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *S3) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S3) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *S4) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S4) calculateAndUpdate(klines []types.KLine) {
func (inc *S4) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *S4) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S4) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *S5) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S5) calculateAndUpdate(klines []types.KLine) {
func (inc *S5) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *S5) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S5) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *S6) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S6) calculateAndUpdate(klines []types.KLine) {
func (inc *S6) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *S6) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S6) Bind(updater indicator.KLineWindowUpdater) {

View File

@ -26,7 +26,7 @@ func (inc *S7) Last() float64 {
return inc.Values[len(inc.Values)-1]
}
func (inc *S7) calculateAndUpdate(klines []types.KLine) {
func (inc *S7) CalculateAndUpdate(klines []types.KLine) {
if len(klines) < inc.Window {
return
}
@ -62,7 +62,7 @@ func (inc *S7) handleKLineWindowUpdate(interval types.Interval, window types.KLi
return
}
inc.calculateAndUpdate(window)
inc.CalculateAndUpdate(window)
}
func (inc *S7) Bind(updater indicator.KLineWindowUpdater) {