diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 87bea4fe18..4d7ab00e48 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -43,7 +43,7 @@ type StandardIndicatorSet struct { ewma map[types.IntervalWindow]*indicator.EWMA boll map[types.IntervalWindowBandWidth]*indicator.BOLL stoch map[types.IntervalWindow]*indicator.STOCH - volatility map[types.IntervalWindow]*indicator.VOLATILITY + volatility map[types.IntervalWindow]*indicator.Volatility store *MarketDataStore } @@ -55,7 +55,7 @@ func NewStandardIndicatorSet(symbol string, store *MarketDataStore) *StandardInd ewma: make(map[types.IntervalWindow]*indicator.EWMA), boll: make(map[types.IntervalWindowBandWidth]*indicator.BOLL), stoch: make(map[types.IntervalWindow]*indicator.STOCH), - volatility: make(map[types.IntervalWindow]*indicator.VOLATILITY), + volatility: make(map[types.IntervalWindow]*indicator.Volatility), store: store, } @@ -146,10 +146,10 @@ func (set *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH } // VOLATILITY returns the volatility(stddev) indicator of the given interval and the window size. -func (set *StandardIndicatorSet) VOLATILITY(iw types.IntervalWindow) *indicator.VOLATILITY { +func (set *StandardIndicatorSet) VOLATILITY(iw types.IntervalWindow) *indicator.Volatility { inc, ok := set.volatility[iw] if !ok { - inc = &indicator.VOLATILITY{IntervalWindow: iw} + inc = &indicator.Volatility{IntervalWindow: iw} inc.Bind(set.store) set.volatility[iw] = inc } diff --git a/pkg/indicator/cci.go b/pkg/indicator/cci.go index ef0ec558d9..d7165037a7 100644 --- a/pkg/indicator/cci.go +++ b/pkg/indicator/cci.go @@ -3,7 +3,6 @@ package indicator import ( "math" - "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" ) @@ -79,17 +78,20 @@ func (inc *CCI) Length() int { var _ types.SeriesExtend = &CCI{} -var three = fixedpoint.NewFromInt(3) -func (inc *CCI) calculateAndUpdate(allKLines []types.KLine) { +func (inc *CCI) PushK(k types.KLine) { + inc.Update(k.High.Add(k.Low).Add(k.Close).Div(three).Float64()) +} + +func (inc *CCI) CalculateAndUpdate(allKLines []types.KLine) { if inc.TypicalPrice.Length() == 0 { for _, k := range allKLines { - inc.Update(k.High.Add(k.Low).Add(k.Close).Div(three).Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { k := allKLines[len(allKLines)-1] - inc.Update(k.High.Add(k.Low).Add(k.Close).Div(three).Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -99,7 +101,7 @@ func (inc *CCI) handleKLineWindowUpdate(interval types.Interval, window types.KL return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *CCI) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/cma.go b/pkg/indicator/cma.go index fbdf357349..d319bd41ff 100644 --- a/pkg/indicator/cma.go +++ b/pkg/indicator/cma.go @@ -48,9 +48,13 @@ func (inc *CA) Length() int { var _ types.SeriesExtend = &CA{} -func (inc *CA) calculateAndUpdate(allKLines []types.KLine) { +func (inc *CA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *CA) CalculateAndUpdate(allKLines []types.KLine) { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -60,7 +64,7 @@ func (inc *CA) handleKLineWindowUpdate(interval types.Interval, window types.KLi return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *CA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/const.go b/pkg/indicator/const.go new file mode 100644 index 0000000000..4b48f2b998 --- /dev/null +++ b/pkg/indicator/const.go @@ -0,0 +1,12 @@ +package indicator + +import ( + "time" + + "github.com/c9s/bbgo/pkg/fixedpoint" +) + +var three = fixedpoint.NewFromInt(3) + +var zeroTime = time.Time{} + diff --git a/pkg/indicator/dema.go b/pkg/indicator/dema.go index d9152d2791..2194ecb192 100644 --- a/pkg/indicator/dema.go +++ b/pkg/indicator/dema.go @@ -50,14 +50,20 @@ func (inc *DEMA) Length() int { var _ types.SeriesExtend = &DEMA{} +func (inc *DEMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + func (inc *DEMA) calculateAndUpdate(allKLines []types.KLine) { if inc.a1 == nil { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + // last k + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } diff --git a/pkg/indicator/drift.go b/pkg/indicator/drift.go index 8494acc398..f0cc1f7570 100644 --- a/pkg/indicator/drift.go +++ b/pkg/indicator/drift.go @@ -68,14 +68,19 @@ func (inc *Drift) Length() int { var _ types.SeriesExtend = &Drift{} -func (inc *Drift) calculateAndUpdate(allKLines []types.KLine) { +func (inc *Drift) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *Drift) CalculateAndUpdate(allKLines []types.KLine) { if inc.chng == nil { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -85,7 +90,7 @@ func (inc *Drift) handleKLineWindowUpdate(interval types.Interval, window types. return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *Drift) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/drift_test.go b/pkg/indicator/drift_test.go index 38d6a732a3..9318da9d61 100644 --- a/pkg/indicator/drift_test.go +++ b/pkg/indicator/drift_test.go @@ -30,7 +30,7 @@ func Test_Drift(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { drift := Drift{IntervalWindow: types.IntervalWindow{Window: 3}} - drift.calculateAndUpdate(tt.kLines) + drift.CalculateAndUpdate(tt.kLines) assert.Equal(t, drift.Length(), tt.all) for _, v := range drift.Values { assert.LessOrEqual(t, v, 1.0) diff --git a/pkg/indicator/emv.go b/pkg/indicator/emv.go index f626ad70eb..a2e1eb359d 100644 --- a/pkg/indicator/emv.go +++ b/pkg/indicator/emv.go @@ -63,17 +63,21 @@ func (inc *EMV) Length() int { var _ types.SeriesExtend = &EMV{} -func (inc *EMV) calculateAndUpdate(allKLines []types.KLine) { +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.Update(k.High.Float64(), k.Low.Float64(), k.Volume.Float64()) + inc.PushK(k) if inc.Length() > 0 { inc.EmitUpdate(inc.Last()) } } } else { k := allKLines[len(allKLines)-1] - inc.Update(k.High.Float64(), k.Low.Float64(), k.Volume.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -82,7 +86,7 @@ func (inc *EMV) handleKLineWindowUpdate(interval types.Interval, window types.KL if inc.Interval != interval { return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *EMV) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/inf.go b/pkg/indicator/inf.go index e72b0611a2..0f777765d9 100644 --- a/pkg/indicator/inf.go +++ b/pkg/indicator/inf.go @@ -9,3 +9,13 @@ type KLineWindowUpdater interface { type KLineCloseHandler interface { OnKLineClosed(func(k types.KLine)) } + +// KLinePusher provides an interface for API user to push kline value to the indicator. +// The indicator implements its own way to calculate the value from the given kline object. +type KLinePusher interface { + PushK(k types.KLine) +} + +type KLineCalculator interface { + CalculateAndUpdate(allKLines []types.KLine) +} diff --git a/pkg/indicator/line.go b/pkg/indicator/line.go index a3932ac1c0..edb8276f16 100644 --- a/pkg/indicator/line.go +++ b/pkg/indicator/line.go @@ -22,11 +22,12 @@ type Line struct { Interval types.Interval } -func (l *Line) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { +func (l *Line) handleKLineWindowUpdate(interval types.Interval, allKLines types.KLineWindow) { if interval != l.Interval { return } - newTime := window.Last().EndTime.Time() + + newTime := allKLines.Last().EndTime.Time() delta := int(newTime.Sub(l.currentTime).Minutes()) / l.Interval.Minutes() l.startIndex += delta l.endIndex += delta diff --git a/pkg/indicator/macd.go b/pkg/indicator/macd.go index 43e94589bb..b1ced01c7a 100644 --- a/pkg/indicator/macd.go +++ b/pkg/indicator/macd.go @@ -51,14 +51,20 @@ func (inc *MACD) Update(x float64) { inc.Histogram.Push(macd - inc.SignalLine.Last()) } +// Deprecated -- this function is not used ??? ask @narumi func (inc *MACD) calculateMACD(kLines []types.KLine, priceF KLinePriceMapper) float64 { - for _, kline := range kLines { - inc.Update(kline.Close.Float64()) + for _, k := range kLines { + inc.PushK(k) } + return inc.Values[len(inc.Values)-1] } -func (inc *MACD) calculateAndUpdate(kLines []types.KLine) { +func (inc *MACD) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *MACD) CalculateAndUpdate(kLines []types.KLine) { if len(kLines) == 0 { return } @@ -67,7 +73,8 @@ func (inc *MACD) calculateAndUpdate(kLines []types.KLine) { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { continue } - inc.Update(k.Close.Float64()) + + inc.PushK(k) } inc.EmitUpdate(inc.Values[len(inc.Values)-1]) @@ -79,7 +86,7 @@ func (inc *MACD) handleKLineWindowUpdate(interval types.Interval, window types.K return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *MACD) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/mapper.go b/pkg/indicator/mapper.go new file mode 100644 index 0000000000..b101e62aef --- /dev/null +++ b/pkg/indicator/mapper.go @@ -0,0 +1,33 @@ +package indicator + +import "github.com/c9s/bbgo/pkg/types" + +type KLinePriceMapper func(k types.KLine) float64 + +func KLineOpenPriceMapper(k types.KLine) float64 { + return k.Open.Float64() +} + +func KLineClosePriceMapper(k types.KLine) float64 { + return k.Close.Float64() +} + +func KLineTypicalPriceMapper(k types.KLine) float64 { + return (k.High.Float64() + k.Low.Float64() + k.Close.Float64()) / 3. +} + +func KLinePriceVolumeMapper(k types.KLine) float64 { + return k.Close.Mul(k.Volume).Float64() +} + +func KLineVolumeMapper(k types.KLine) float64 { + return k.Volume.Float64() +} + +func MapKLinePrice(kLines []types.KLine, f KLinePriceMapper) (prices []float64) { + for _, k := range kLines { + prices = append(prices, f(k)) + } + + return prices +} diff --git a/pkg/indicator/obv.go b/pkg/indicator/obv.go index 52321892ff..db5688bd59 100644 --- a/pkg/indicator/obv.go +++ b/pkg/indicator/obv.go @@ -54,13 +54,19 @@ func (inc *OBV) Index(i int) float64 { var _ types.SeriesExtend = &OBV{} -func (inc *OBV) calculateAndUpdate(kLines []types.KLine) { +func (inc *OBV) PushK(k types.KLine) { + inc.Update(k.Close.Float64(), k.Volume.Float64()) +} + +func (inc *OBV) CalculateAndUpdate(kLines []types.KLine) { for _, k := range kLines { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { continue } - inc.Update(k.Close.Float64(), k.Volume.Float64()) + + inc.PushK(k) } + inc.EmitUpdate(inc.Last()) inc.EndTime = kLines[len(kLines)-1].EndTime.Time() } @@ -70,7 +76,7 @@ func (inc *OBV) handleKLineWindowUpdate(interval types.Interval, window types.KL return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *OBV) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/obv_test.go b/pkg/indicator/obv_test.go index 66d951a29d..a70a0be4b5 100644 --- a/pkg/indicator/obv_test.go +++ b/pkg/indicator/obv_test.go @@ -51,7 +51,7 @@ func Test_calculateOBV(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { obv := OBV{IntervalWindow: types.IntervalWindow{Window: tt.window}} - obv.calculateAndUpdate(tt.kLines) + obv.CalculateAndUpdate(tt.kLines) assert.Equal(t, len(obv.Values), len(tt.want)) for i, v := range obv.Values { assert.InDelta(t, v, tt.want[i], Delta) diff --git a/pkg/indicator/pivot.go b/pkg/indicator/pivot.go index ccc1322e20..e3912afeab 100644 --- a/pkg/indicator/pivot.go +++ b/pkg/indicator/pivot.go @@ -38,7 +38,7 @@ func (inc *Pivot) LastHigh() float64 { return inc.Highs[len(inc.Highs)-1] } -func (inc *Pivot) Update(klines []types.KLine) { +func (inc *Pivot) CalculateAndUpdate(klines []types.KLine) { if len(klines) < inc.Window { return } @@ -84,7 +84,7 @@ func (inc *Pivot) handleKLineWindowUpdate(interval types.Interval, window types. return } - inc.Update(window) + inc.CalculateAndUpdate(window) } func (inc *Pivot) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/rma.go b/pkg/indicator/rma.go index 857261ecc9..9e6b71fdbe 100644 --- a/pkg/indicator/rma.go +++ b/pkg/indicator/rma.go @@ -64,12 +64,17 @@ func (inc *RMA) Length() int { var _ types.SeriesExtend = &RMA{} +func (inc *RMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + func (inc *RMA) calculateAndUpdate(kLines []types.KLine) { for _, k := range kLines { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { continue } - inc.Update(k.Close.Float64()) + + inc.PushK(k) } inc.EmitUpdate(inc.Last()) diff --git a/pkg/indicator/rsi.go b/pkg/indicator/rsi.go index 8a76322abd..30d8662a59 100644 --- a/pkg/indicator/rsi.go +++ b/pkg/indicator/rsi.go @@ -80,12 +80,17 @@ func (inc *RSI) Length() int { var _ types.SeriesExtend = &RSI{} -func (inc *RSI) calculateAndUpdate(kLines []types.KLine) { +func (inc *RSI) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *RSI) CalculateAndUpdate(kLines []types.KLine) { for _, k := range kLines { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { continue } - inc.Update(k.Close.Float64()) + + inc.PushK(k) } inc.EmitUpdate(inc.Last()) @@ -97,7 +102,7 @@ func (inc *RSI) handleKLineWindowUpdate(interval types.Interval, window types.KL return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *RSI) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/rsi_test.go b/pkg/indicator/rsi_test.go index 80e4c91870..3b96fd4335 100644 --- a/pkg/indicator/rsi_test.go +++ b/pkg/indicator/rsi_test.go @@ -59,7 +59,7 @@ func Test_calculateRSI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { rsi := RSI{IntervalWindow: types.IntervalWindow{Window: tt.window}} - rsi.calculateAndUpdate(tt.kLines) + rsi.CalculateAndUpdate(tt.kLines) assert.Equal(t, len(rsi.Values), len(tt.want)) for i, v := range rsi.Values { assert.InDelta(t, v, tt.want[i], Delta) diff --git a/pkg/indicator/sma.go b/pkg/indicator/sma.go index c07f3293f2..2f762a4d9e 100644 --- a/pkg/indicator/sma.go +++ b/pkg/indicator/sma.go @@ -10,8 +10,6 @@ import ( const MaxNumOfSMA = 5_000 const MaxNumOfSMATruncateSize = 100 -var zeroTime time.Time - //go:generate callbackgen -type SMA type SMA struct { types.SeriesBase @@ -59,20 +57,25 @@ func (inc *SMA) Update(value float64) { } } -func (inc *SMA) calculateAndUpdate(kLines []types.KLine) { +func (inc *SMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *SMA) CalculateAndUpdate(kLines []types.KLine) { var index = len(kLines) - 1 var kline = kLines[index] if inc.EndTime != zeroTime && kline.EndTime.Before(inc.EndTime) { return } + if inc.Cache == nil { for _, k := range kLines { - inc.Update(KLineClosePriceMapper(k)) + inc.PushK(k) inc.EndTime = k.EndTime.Time() inc.EmitUpdate(inc.Values.Last()) } } else { - inc.Update(KLineClosePriceMapper(kline)) + inc.PushK(kline) inc.EndTime = kline.EndTime.Time() inc.EmitUpdate(inc.Values.Last()) } @@ -83,7 +86,7 @@ func (inc *SMA) handleKLineWindowUpdate(interval types.Interval, window types.KL return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *SMA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/sma_test.go b/pkg/indicator/sma_test.go index e385e7d682..fb40716cc8 100644 --- a/pkg/indicator/sma_test.go +++ b/pkg/indicator/sma_test.go @@ -52,7 +52,7 @@ func Test_SMA(t *testing.T) { sma := SMA{ IntervalWindow: types.IntervalWindow{Window: 5}, } - sma.calculateAndUpdate(tt.kLines) + sma.CalculateAndUpdate(tt.kLines) assert.InDelta(t, tt.want, sma.Last(), Delta) assert.InDelta(t, tt.next, sma.Index(1), Delta) sma.Update(tt.update) diff --git a/pkg/indicator/ssf.go b/pkg/indicator/ssf.go index 28871a041a..a055dd0596 100644 --- a/pkg/indicator/ssf.go +++ b/pkg/indicator/ssf.go @@ -93,14 +93,19 @@ func (inc *SSF) Last() float64 { var _ types.SeriesExtend = &SSF{} -func (inc *SSF) calculateAndUpdate(allKLines []types.KLine) { +func (inc *SSF) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *SSF) CalculateAndUpdate(allKLines []types.KLine) { if inc.Values != nil { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) return } for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -109,7 +114,7 @@ func (inc *SSF) handleKLineWindowUpdate(interval types.Interval, window types.KL if inc.Interval != interval { return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *SSF) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/ssf_test.go b/pkg/indicator/ssf_test.go index 0eced9ca98..253d722204 100644 --- a/pkg/indicator/ssf_test.go +++ b/pkg/indicator/ssf_test.go @@ -62,7 +62,7 @@ func Test_SSF(t *testing.T) { IntervalWindow: types.IntervalWindow{Window: 5}, Poles: tt.poles, } - ssf.calculateAndUpdate(tt.kLines) + ssf.CalculateAndUpdate(tt.kLines) assert.InDelta(t, tt.want, ssf.Last(), Delta) assert.InDelta(t, tt.next, ssf.Index(1), Delta) assert.Equal(t, tt.all, ssf.Length()) diff --git a/pkg/indicator/stoch.go b/pkg/indicator/stoch.go index c24cd81697..602df765f3 100644 --- a/pkg/indicator/stoch.go +++ b/pkg/indicator/stoch.go @@ -59,7 +59,11 @@ func (inc *STOCH) LastD() float64 { return inc.D[len(inc.D)-1] } -func (inc *STOCH) calculateAndUpdate(kLines []types.KLine) { +func (inc *STOCH) PushK(k types.KLine) { + inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64()) +} + +func (inc *STOCH) CalculateAndUpdate(kLines []types.KLine) { if len(kLines) < inc.Window || len(kLines) < DPeriod { return } @@ -68,7 +72,8 @@ func (inc *STOCH) calculateAndUpdate(kLines []types.KLine) { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { continue } - inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64()) + + inc.PushK(k) } inc.EmitUpdate(inc.LastK(), inc.LastD()) @@ -80,7 +85,7 @@ func (inc *STOCH) handleKLineWindowUpdate(interval types.Interval, window types. return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *STOCH) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/stoch_test.go b/pkg/indicator/stoch_test.go index f8a90bfee8..56839dadc4 100644 --- a/pkg/indicator/stoch_test.go +++ b/pkg/indicator/stoch_test.go @@ -56,7 +56,7 @@ func TestSTOCH_update(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { kd := STOCH{IntervalWindow: types.IntervalWindow{Window: tt.window}} - kd.calculateAndUpdate(tt.kLines) + kd.CalculateAndUpdate(tt.kLines) got_k := kd.LastK() diff_k := math.Trunc((got_k-tt.want_k)*100) / 100 diff --git a/pkg/indicator/tema.go b/pkg/indicator/tema.go index 8d1fc3fd3c..37a9f78b00 100644 --- a/pkg/indicator/tema.go +++ b/pkg/indicator/tema.go @@ -55,14 +55,19 @@ func (inc *TEMA) Length() int { var _ types.SeriesExtend = &TEMA{} -func (inc *TEMA) calculateAndUpdate(allKLines []types.KLine) { +func (inc *TEMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *TEMA) CalculateAndUpdate(allKLines []types.KLine) { if inc.A1 == nil { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -72,7 +77,7 @@ func (inc *TEMA) handleKLineWindowUpdate(interval types.Interval, window types.K return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *TEMA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/tema_test.go b/pkg/indicator/tema_test.go index 641153f402..b50c72ca75 100644 --- a/pkg/indicator/tema_test.go +++ b/pkg/indicator/tema_test.go @@ -46,7 +46,7 @@ func Test_TEMA(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tema := TEMA{IntervalWindow: types.IntervalWindow{Window: 16}} - tema.calculateAndUpdate(tt.kLines) + tema.CalculateAndUpdate(tt.kLines) last := tema.Last() assert.InDelta(t, tt.want, last, Delta) assert.InDelta(t, tt.next, tema.Index(1), Delta) diff --git a/pkg/indicator/till.go b/pkg/indicator/till.go index 795194e5cb..3fdc2a7068 100644 --- a/pkg/indicator/till.go +++ b/pkg/indicator/till.go @@ -12,18 +12,19 @@ const defaultVolumeFactor = 0.7 type TILL struct { types.SeriesBase types.IntervalWindow - VolumeFactor float64 - e1 *EWMA - e2 *EWMA - e3 *EWMA - e4 *EWMA - e5 *EWMA - e6 *EWMA - c1 float64 - c2 float64 - c3 float64 - c4 float64 - UpdateCallbacks []func(value float64) + VolumeFactor float64 + e1 *EWMA + e2 *EWMA + e3 *EWMA + e4 *EWMA + e5 *EWMA + e6 *EWMA + c1 float64 + c2 float64 + c3 float64 + c4 float64 + + updateCallbacks []func(value float64) } func (inc *TILL) Update(value float64) { @@ -85,7 +86,11 @@ func (inc *TILL) Length() int { var _ types.Series = &TILL{} -func (inc *TILL) calculateAndUpdate(allKLines []types.KLine) { +func (inc *TILL) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *TILL) CalculateAndUpdate(allKLines []types.KLine) { doable := false if inc.e1 == nil { doable = true @@ -94,8 +99,9 @@ func (inc *TILL) calculateAndUpdate(allKLines []types.KLine) { if !doable && k.StartTime.After(inc.e1.LastOpenTime) { doable = true } + if doable { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -106,7 +112,7 @@ func (inc *TILL) handleKLineWindowUpdate(interval types.Interval, window types.K return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *TILL) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/till_callbacks.go b/pkg/indicator/till_callbacks.go index 53d89cb8d8..d17a8dcd96 100644 --- a/pkg/indicator/till_callbacks.go +++ b/pkg/indicator/till_callbacks.go @@ -5,11 +5,11 @@ package indicator import () func (inc *TILL) OnUpdate(cb func(value float64)) { - inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) + inc.updateCallbacks = append(inc.updateCallbacks, cb) } func (inc *TILL) EmitUpdate(value float64) { - for _, cb := range inc.UpdateCallbacks { + for _, cb := range inc.updateCallbacks { cb(value) } } diff --git a/pkg/indicator/till_test.go b/pkg/indicator/till_test.go index 4615a5dbe0..1b03017b80 100644 --- a/pkg/indicator/till_test.go +++ b/pkg/indicator/till_test.go @@ -4,9 +4,10 @@ import ( "encoding/json" "testing" + "github.com/stretchr/testify/assert" + "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" - "github.com/stretchr/testify/assert" ) /* @@ -55,7 +56,7 @@ func Test_TILL(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { till := TILL{IntervalWindow: types.IntervalWindow{Window: 16}} - till.calculateAndUpdate(tt.kLines) + till.CalculateAndUpdate(tt.kLines) last := till.Last() assert.InDelta(t, tt.want, last, Delta) assert.InDelta(t, tt.next, till.Index(1), Delta) diff --git a/pkg/indicator/tma.go b/pkg/indicator/tma.go index c100a987d8..31600f2c42 100644 --- a/pkg/indicator/tma.go +++ b/pkg/indicator/tma.go @@ -50,14 +50,19 @@ func (inc *TMA) Length() int { var _ types.SeriesExtend = &TMA{} -func (inc *TMA) calculateAndUpdate(allKLines []types.KLine) { +func (inc *TMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *TMA) CalculateAndUpdate(allKLines []types.KLine) { if inc.s1 == nil { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -67,7 +72,7 @@ func (inc *TMA) handleKLineWindowUpdate(interval types.Interval, window types.KL return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *TMA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/util.go b/pkg/indicator/util.go index eb27b167ac..d676406cf3 100644 --- a/pkg/indicator/util.go +++ b/pkg/indicator/util.go @@ -1,26 +1,2 @@ package indicator -import "github.com/c9s/bbgo/pkg/types" - -type KLinePriceMapper func(k types.KLine) float64 - -func KLineOpenPriceMapper(k types.KLine) float64 { - return k.Open.Float64() -} - -func KLineClosePriceMapper(k types.KLine) float64 { - return k.Close.Float64() -} - -func KLineTypicalPriceMapper(k types.KLine) float64 { - return (k.High.Float64() + k.Low.Float64() + k.Close.Float64()) / 3. -} - -func MapKLinePrice(kLines []types.KLine, f KLinePriceMapper) (prices []float64) { - for _, k := range kLines { - prices = append(prices, f(k)) - } - - return prices -} - diff --git a/pkg/indicator/vidya.go b/pkg/indicator/vidya.go index cda286e00b..9626bb9b95 100644 --- a/pkg/indicator/vidya.go +++ b/pkg/indicator/vidya.go @@ -15,7 +15,7 @@ type VIDYA struct { Values types.Float64Slice input types.Float64Slice - UpdateCallbacks []func(value float64) + updateCallbacks []func(value float64) } func (inc *VIDYA) Update(value float64) { @@ -70,14 +70,19 @@ func (inc *VIDYA) Length() int { var _ types.SeriesExtend = &VIDYA{} +func (inc *VIDYA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + func (inc *VIDYA) calculateAndUpdate(allKLines []types.KLine) { if inc.input.Length() == 0 { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } diff --git a/pkg/indicator/vidya_callbacks.go b/pkg/indicator/vidya_callbacks.go index b78e797c45..c05d0a20b3 100644 --- a/pkg/indicator/vidya_callbacks.go +++ b/pkg/indicator/vidya_callbacks.go @@ -5,11 +5,11 @@ package indicator import () func (inc *VIDYA) OnUpdate(cb func(value float64)) { - inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) + inc.updateCallbacks = append(inc.updateCallbacks, cb) } func (inc *VIDYA) EmitUpdate(value float64) { - for _, cb := range inc.UpdateCallbacks { + for _, cb := range inc.updateCallbacks { cb(value) } } diff --git a/pkg/indicator/volatility.go b/pkg/indicator/volatility.go index 9f4571408e..9d86c7f31c 100644 --- a/pkg/indicator/volatility.go +++ b/pkg/indicator/volatility.go @@ -13,10 +13,10 @@ import ( const MaxNumOfVOL = 5_000 const MaxNumOfVOLTruncateSize = 100 -//var zeroTime time.Time +// var zeroTime time.Time -//go:generate callbackgen -type VOLATILITY -type VOLATILITY struct { +//go:generate callbackgen -type Volatility +type Volatility struct { types.SeriesBase types.IntervalWindow Values types.Float64Slice @@ -25,42 +25,43 @@ type VOLATILITY struct { UpdateCallbacks []func(value float64) } -func (inc *VOLATILITY) Last() float64 { +func (inc *Volatility) Last() float64 { if len(inc.Values) == 0 { return 0.0 } return inc.Values[len(inc.Values)-1] } -func (inc *VOLATILITY) Index(i int) float64 { +func (inc *Volatility) Index(i int) float64 { if len(inc.Values)-i <= 0 { return 0.0 } return inc.Values[len(inc.Values)-i-1] } -func (inc *VOLATILITY) Length() int { +func (inc *Volatility) Length() int { return len(inc.Values) } -var _ types.SeriesExtend = &VOLATILITY{} +var _ types.SeriesExtend = &Volatility{} -func (inc *VOLATILITY) calculateAndUpdate(klines []types.KLine) { - if len(klines) < inc.Window { +func (inc *Volatility) CalculateAndUpdate(allKLines []types.KLine) { + if len(allKLines) < inc.Window { return } - var end = len(klines) - 1 - var lastKLine = klines[end] + var end = len(allKLines) - 1 + var lastKLine = allKLines[end] if inc.EndTime != zeroTime && lastKLine.GetEndTime().Before(inc.EndTime) { return } + if len(inc.Values) == 0 { inc.SeriesBase.Series = inc } - var recentT = klines[end-(inc.Window-1) : end+1] + var recentT = allKLines[end-(inc.Window-1) : end+1] volatility, err := calculateVOLATILITY(recentT, inc.Window, KLineClosePriceMapper) if err != nil { @@ -73,20 +74,20 @@ func (inc *VOLATILITY) calculateAndUpdate(klines []types.KLine) { inc.Values = inc.Values[MaxNumOfVOLTruncateSize-1:] } - inc.EndTime = klines[end].GetEndTime().Time() + inc.EndTime = allKLines[end].GetEndTime().Time() inc.EmitUpdate(volatility) } -func (inc *VOLATILITY) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { +func (inc *Volatility) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { if inc.Interval != interval { return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } -func (inc *VOLATILITY) Bind(updater KLineWindowUpdater) { +func (inc *Volatility) Bind(updater KLineWindowUpdater) { updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) } diff --git a/pkg/indicator/volatility_callbacks.go b/pkg/indicator/volatility_callbacks.go index 9f5311d757..c04211a083 100644 --- a/pkg/indicator/volatility_callbacks.go +++ b/pkg/indicator/volatility_callbacks.go @@ -1,14 +1,14 @@ -// Code generated by "callbackgen -type VOLATILITY"; DO NOT EDIT. +// Code generated by "callbackgen -type Volatility"; DO NOT EDIT. package indicator import () -func (inc *VOLATILITY) OnUpdate(cb func(value float64)) { +func (inc *Volatility) OnUpdate(cb func(value float64)) { inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) } -func (inc *VOLATILITY) EmitUpdate(value float64) { +func (inc *Volatility) EmitUpdate(value float64) { for _, cb := range inc.UpdateCallbacks { cb(value) } diff --git a/pkg/indicator/vwap.go b/pkg/indicator/vwap.go index 89e3b28e92..a95cee2064 100644 --- a/pkg/indicator/vwap.go +++ b/pkg/indicator/vwap.go @@ -71,18 +71,21 @@ func (inc *VWAP) Length() int { var _ types.SeriesExtend = &VWAP{} -func (inc *VWAP) calculateAndUpdate(kLines []types.KLine) { - var priceF = KLineTypicalPriceMapper +func (inc *VWAP) PushK(k types.KLine) { + inc.Update(KLineTypicalPriceMapper(k), k.Volume.Float64()) +} - for _, k := range kLines { +func (inc *VWAP) CalculateAndUpdate(allKLines []types.KLine) { + for _, k := range allKLines { if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) { continue } - inc.Update(priceF(k), k.Volume.Float64()) + + inc.PushK(k) } inc.EmitUpdate(inc.Last()) - inc.EndTime = kLines[len(kLines)-1].EndTime.Time() + inc.EndTime = allKLines[len(allKLines)-1].EndTime.Time() } func (inc *VWAP) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { @@ -90,14 +93,14 @@ func (inc *VWAP) handleKLineWindowUpdate(interval types.Interval, window types.K return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *VWAP) Bind(updater KLineWindowUpdater) { updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) } -func CalculateVWAP(klines []types.KLine, priceF KLinePriceMapper, window int) float64 { +func calculateVWAP(klines []types.KLine, priceF KLinePriceMapper, window int) float64 { vwap := VWAP{IntervalWindow: types.IntervalWindow{Window: window}} for _, k := range klines { vwap.Update(priceF(k), k.Volume.Float64()) diff --git a/pkg/indicator/vwap_callbacks.go b/pkg/indicator/vwap_callbacks.go index 9a235d17ae..918ddcf506 100644 --- a/pkg/indicator/vwap_callbacks.go +++ b/pkg/indicator/vwap_callbacks.go @@ -4,12 +4,12 @@ package indicator import () -func (V *VWAP) OnUpdate(cb func(value float64)) { - V.UpdateCallbacks = append(V.UpdateCallbacks, cb) +func (inc *VWAP) OnUpdate(cb func(value float64)) { + inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) } -func (V *VWAP) EmitUpdate(value float64) { - for _, cb := range V.UpdateCallbacks { +func (inc *VWAP) EmitUpdate(value float64) { + for _, cb := range inc.UpdateCallbacks { cb(value) } } diff --git a/pkg/indicator/vwap_test.go b/pkg/indicator/vwap_test.go index d168bb938f..7929b4bbae 100644 --- a/pkg/indicator/vwap_test.go +++ b/pkg/indicator/vwap_test.go @@ -64,7 +64,7 @@ func Test_calculateVWAP(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { priceF := KLineTypicalPriceMapper - got := CalculateVWAP(tt.kLines, priceF, tt.window) + got := calculateVWAP(tt.kLines, priceF, tt.window) diff := math.Trunc((got-tt.want)*100) / 100 if diff != 0 { t.Errorf("calculateVWAP() = %v, want %v", got, tt.want) diff --git a/pkg/indicator/vwma.go b/pkg/indicator/vwma.go index 4ee1068c94..742a37aca0 100644 --- a/pkg/indicator/vwma.go +++ b/pkg/indicator/vwma.go @@ -49,27 +49,20 @@ func (inc *VWMA) Length() int { var _ types.SeriesExtend = &VWMA{} -func KLinePriceVolumeMapper(k types.KLine) float64 { - return k.Close.Mul(k.Volume).Float64() -} - -func KLineVolumeMapper(k types.KLine) float64 { - return k.Volume.Float64() -} -func (inc *VWMA) calculateAndUpdate(kLines []types.KLine) { - if len(kLines) < inc.Window { +func (inc *VWMA) CalculateAndUpdate(allKLines []types.KLine) { + if len(allKLines) < inc.Window { return } - var index = len(kLines) - 1 - var kline = kLines[index] + var index = len(allKLines) - 1 + var kline = allKLines[index] if inc.EndTime != zeroTime && kline.EndTime.Before(inc.EndTime) { return } - var recentK = kLines[index-(inc.Window-1) : index+1] + var recentK = allKLines[index-(inc.Window-1) : index+1] pv, err := calculateSMA(recentK, inc.Window, KLinePriceVolumeMapper) if err != nil { @@ -93,7 +86,7 @@ func (inc *VWMA) calculateAndUpdate(kLines []types.KLine) { inc.Values = inc.Values[MaxNumOfSMATruncateSize-1:] } - inc.EndTime = kLines[index].EndTime.Time() + inc.EndTime = allKLines[index].EndTime.Time() inc.EmitUpdate(vwma) } @@ -103,7 +96,7 @@ func (inc *VWMA) handleKLineWindowUpdate(interval types.Interval, window types.K return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *VWMA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/wwma.go b/pkg/indicator/wwma.go index be0ec0a7ed..bc5ab5a679 100644 --- a/pkg/indicator/wwma.go +++ b/pkg/indicator/wwma.go @@ -1,8 +1,9 @@ package indicator import ( - "github.com/c9s/bbgo/pkg/types" "time" + + "github.com/c9s/bbgo/pkg/types" ) // Refer: Welles Wilder's Moving Average @@ -56,7 +57,11 @@ func (inc *WWMA) Length() int { return len(inc.Values) } -func (inc *WWMA) calculateAndUpdate(allKLines []types.KLine) { +func (inc *WWMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *WWMA) CalculateAndUpdate(allKLines []types.KLine) { if len(allKLines) < inc.Window { // we can't calculate return @@ -68,7 +73,7 @@ func (inc *WWMA) calculateAndUpdate(allKLines []types.KLine) { doable = true } if doable { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.LastOpenTime = k.StartTime.Time() inc.EmitUpdate(inc.Last()) } @@ -80,7 +85,7 @@ func (inc *WWMA) handleKLineWindowUpdate(interval types.Interval, window types.K return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *WWMA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/zlema.go b/pkg/indicator/zlema.go index f127c0008b..cb7dfbde35 100644 --- a/pkg/indicator/zlema.go +++ b/pkg/indicator/zlema.go @@ -16,7 +16,7 @@ type ZLEMA struct { zlema *EWMA lag int - UpdateCallbacks []func(value float64) + updateCallbacks []func(value float64) } func (inc *ZLEMA) Index(i int) float64 { @@ -59,14 +59,19 @@ func (inc *ZLEMA) Update(value float64) { var _ types.SeriesExtend = &ZLEMA{} -func (inc *ZLEMA) calculateAndUpdate(allKLines []types.KLine) { +func (inc *ZLEMA) PushK(k types.KLine) { + inc.Update(k.Close.Float64()) +} + +func (inc *ZLEMA) CalculateAndUpdate(allKLines []types.KLine) { if inc.zlema == nil { for _, k := range allKLines { - inc.Update(k.Close.Float64()) + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } else { - inc.Update(allKLines[len(allKLines)-1].Close.Float64()) + k := allKLines[len(allKLines)-1] + inc.PushK(k) inc.EmitUpdate(inc.Last()) } } @@ -76,7 +81,7 @@ func (inc *ZLEMA) handleKLineWindowUpdate(interval types.Interval, window types. return } - inc.calculateAndUpdate(window) + inc.CalculateAndUpdate(window) } func (inc *ZLEMA) Bind(updater KLineWindowUpdater) { diff --git a/pkg/indicator/zlema_callbacks.go b/pkg/indicator/zlema_callbacks.go index d70147699f..98a84c6597 100644 --- a/pkg/indicator/zlema_callbacks.go +++ b/pkg/indicator/zlema_callbacks.go @@ -5,11 +5,11 @@ package indicator import () func (inc *ZLEMA) OnUpdate(cb func(value float64)) { - inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) + inc.updateCallbacks = append(inc.updateCallbacks, cb) } func (inc *ZLEMA) EmitUpdate(value float64) { - for _, cb := range inc.UpdateCallbacks { + for _, cb := range inc.updateCallbacks { cb(value) } } diff --git a/pkg/indicator/zlema_test.go b/pkg/indicator/zlema_test.go index 4b0e546ab2..4560f4276b 100644 --- a/pkg/indicator/zlema_test.go +++ b/pkg/indicator/zlema_test.go @@ -45,7 +45,7 @@ func Test_ZLEMA(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { zlema := ZLEMA{IntervalWindow: types.IntervalWindow{Window: 16}} - zlema.calculateAndUpdate(tt.kLines) + zlema.CalculateAndUpdate(tt.kLines) last := zlema.Last() assert.InDelta(t, tt.want, last, Delta) assert.InDelta(t, tt.next, zlema.Index(1), Delta) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 1b843fd4be..8756cbd5dc 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -296,7 +296,7 @@ func preloadPivot(pivot *indicator.Pivot, store *bbgo.MarketDataStore) *types.KL log.Debugf("updating pivot indicator: %d klines", len(*klines)) for i := pivot.Window; i < len(*klines); i++ { - pivot.Update((*klines)[0 : i+1]) + pivot.CalculateAndUpdate((*klines)[0 : i+1]) } log.Debugf("found %v previous lows: %v", pivot.IntervalWindow, pivot.Lows)