From 9d9f898f17a2c5fa63a1a11d14b4b88873d4145e Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 9 Jun 2023 13:18:08 +0800 Subject: [PATCH] indicator: use pointer for float64series --- pkg/indicator/float64series.go | 4 ++-- pkg/indicator/v2_atrp.go | 6 ++++-- pkg/indicator/v2_cma.go | 2 +- pkg/indicator/v2_cross.go | 2 +- pkg/indicator/v2_ewma.go | 2 +- pkg/indicator/v2_macd_test.go | 2 +- pkg/indicator/v2_multiply.go | 2 +- pkg/indicator/v2_pivothigh.go | 2 +- pkg/indicator/v2_pivotlow.go | 2 +- pkg/indicator/v2_price.go | 17 +++++++++-------- pkg/indicator/v2_rma.go | 2 +- pkg/indicator/v2_rsi.go | 2 +- pkg/indicator/v2_rsi_test.go | 2 +- pkg/indicator/v2_sma.go | 2 +- pkg/indicator/v2_stddev.go | 2 +- pkg/indicator/v2_subtract.go | 2 +- pkg/indicator/v2_tr.go | 2 +- 17 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pkg/indicator/float64series.go b/pkg/indicator/float64series.go index 821c17666..c198e8e8d 100644 --- a/pkg/indicator/float64series.go +++ b/pkg/indicator/float64series.go @@ -11,8 +11,8 @@ type Float64Series struct { slice floats.Slice } -func NewFloat64Series(v ...float64) Float64Series { - s := Float64Series{} +func NewFloat64Series(v ...float64) *Float64Series { + s := &Float64Series{} s.slice = v s.SeriesBase.Series = s.slice return s diff --git a/pkg/indicator/v2_atrp.go b/pkg/indicator/v2_atrp.go index 6261c1cb3..bb9b982d3 100644 --- a/pkg/indicator/v2_atrp.go +++ b/pkg/indicator/v2_atrp.go @@ -1,11 +1,13 @@ package indicator type ATRPStream struct { - Float64Series + *Float64Series } func ATRP2(source KLineSubscription, window int) *ATRPStream { - s := &ATRPStream{} + s := &ATRPStream{ + Float64Series: NewFloat64Series(), + } tr := TR2(source) atr := RMA2(tr, window, true) atr.OnUpdate(func(x float64) { diff --git a/pkg/indicator/v2_cma.go b/pkg/indicator/v2_cma.go index f3c485aff..9bc2c8994 100644 --- a/pkg/indicator/v2_cma.go +++ b/pkg/indicator/v2_cma.go @@ -1,7 +1,7 @@ package indicator type CMAStream struct { - Float64Series + *Float64Series } func CMA2(source Float64Source) *CMAStream { diff --git a/pkg/indicator/v2_cross.go b/pkg/indicator/v2_cross.go index 084130fdb..835e87c30 100644 --- a/pkg/indicator/v2_cross.go +++ b/pkg/indicator/v2_cross.go @@ -13,7 +13,7 @@ const ( // CrossStream subscribes 2 upstreams, and calculate the cross signal type CrossStream struct { - Float64Series + *Float64Series a, b floats.Slice } diff --git a/pkg/indicator/v2_ewma.go b/pkg/indicator/v2_ewma.go index 1be654f85..45a15d7ca 100644 --- a/pkg/indicator/v2_ewma.go +++ b/pkg/indicator/v2_ewma.go @@ -1,7 +1,7 @@ package indicator type EWMAStream struct { - Float64Series + *Float64Series window int multiplier float64 diff --git a/pkg/indicator/v2_macd_test.go b/pkg/indicator/v2_macd_test.go index 74617089a..0fb9b647a 100644 --- a/pkg/indicator/v2_macd_test.go +++ b/pkg/indicator/v2_macd_test.go @@ -41,7 +41,7 @@ func Test_MACD2(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - prices := &PriceStream{} + prices := ClosePrices(nil) macd := MACD2(prices, 12, 26, 9) for _, k := range tt.kLines { prices.EmitUpdate(k.Close.Float64()) diff --git a/pkg/indicator/v2_multiply.go b/pkg/indicator/v2_multiply.go index 24f647005..ee76bdc00 100644 --- a/pkg/indicator/v2_multiply.go +++ b/pkg/indicator/v2_multiply.go @@ -3,7 +3,7 @@ package indicator import "github.com/c9s/bbgo/pkg/datatype/floats" type MultiplyStream struct { - Float64Series + *Float64Series a, b floats.Slice } diff --git a/pkg/indicator/v2_pivothigh.go b/pkg/indicator/v2_pivothigh.go index eb0352a73..9e4143816 100644 --- a/pkg/indicator/v2_pivothigh.go +++ b/pkg/indicator/v2_pivothigh.go @@ -5,7 +5,7 @@ import ( ) type PivotHighStream struct { - Float64Series + *Float64Series rawValues floats.Slice window, rightWindow int } diff --git a/pkg/indicator/v2_pivotlow.go b/pkg/indicator/v2_pivotlow.go index 47d76308f..1fa78e054 100644 --- a/pkg/indicator/v2_pivotlow.go +++ b/pkg/indicator/v2_pivotlow.go @@ -5,7 +5,7 @@ import ( ) type PivotLowStream struct { - Float64Series + *Float64Series rawValues floats.Slice window, rightWindow int } diff --git a/pkg/indicator/v2_price.go b/pkg/indicator/v2_price.go index d95976dda..6e4c61844 100644 --- a/pkg/indicator/v2_price.go +++ b/pkg/indicator/v2_price.go @@ -11,22 +11,23 @@ type KLineSubscription interface { } type PriceStream struct { - Float64Series + *Float64Series mapper KLineValueMapper } func Price(source KLineSubscription, mapper KLineValueMapper) *PriceStream { s := &PriceStream{ - mapper: mapper, + Float64Series: NewFloat64Series(), + mapper: mapper, } - s.SeriesBase.Series = s.slice - - source.AddSubscriber(func(k types.KLine) { - v := s.mapper(k) - s.PushAndEmit(v) - }) + if source != nil { + source.AddSubscriber(func(k types.KLine) { + v := s.mapper(k) + s.PushAndEmit(v) + }) + } return s } diff --git a/pkg/indicator/v2_rma.go b/pkg/indicator/v2_rma.go index 17650d9d9..0464ad96d 100644 --- a/pkg/indicator/v2_rma.go +++ b/pkg/indicator/v2_rma.go @@ -2,7 +2,7 @@ package indicator type RMAStream struct { // embedded structs - Float64Series + *Float64Series // config fields Adjust bool diff --git a/pkg/indicator/v2_rsi.go b/pkg/indicator/v2_rsi.go index 81e439320..11cf984c9 100644 --- a/pkg/indicator/v2_rsi.go +++ b/pkg/indicator/v2_rsi.go @@ -2,7 +2,7 @@ package indicator type RSIStream struct { // embedded structs - Float64Series + *Float64Series // config fields window int diff --git a/pkg/indicator/v2_rsi_test.go b/pkg/indicator/v2_rsi_test.go index 533a89e1a..311624024 100644 --- a/pkg/indicator/v2_rsi_test.go +++ b/pkg/indicator/v2_rsi_test.go @@ -67,7 +67,7 @@ func Test_RSI2(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // RSI2() - prices := &PriceStream{} + prices := ClosePrices(nil) rsi := RSI2(prices, tt.window) t.Logf("data length: %d", len(tt.values)) diff --git a/pkg/indicator/v2_sma.go b/pkg/indicator/v2_sma.go index 641cdb3d2..b6a79277c 100644 --- a/pkg/indicator/v2_sma.go +++ b/pkg/indicator/v2_sma.go @@ -3,7 +3,7 @@ package indicator import "github.com/c9s/bbgo/pkg/types" type SMAStream struct { - Float64Series + *Float64Series window int rawValues *types.Queue } diff --git a/pkg/indicator/v2_stddev.go b/pkg/indicator/v2_stddev.go index 28a5a4d07..9e465f970 100644 --- a/pkg/indicator/v2_stddev.go +++ b/pkg/indicator/v2_stddev.go @@ -3,7 +3,7 @@ package indicator import "github.com/c9s/bbgo/pkg/types" type StdDevStream struct { - Float64Series + *Float64Series rawValues *types.Queue diff --git a/pkg/indicator/v2_subtract.go b/pkg/indicator/v2_subtract.go index 7ccde2bf6..33a191730 100644 --- a/pkg/indicator/v2_subtract.go +++ b/pkg/indicator/v2_subtract.go @@ -6,7 +6,7 @@ import ( // SubtractStream subscribes 2 upstream data, and then subtract these 2 values type SubtractStream struct { - Float64Series + *Float64Series a, b floats.Slice i int diff --git a/pkg/indicator/v2_tr.go b/pkg/indicator/v2_tr.go index 05c6350e2..98f4bdc7b 100644 --- a/pkg/indicator/v2_tr.go +++ b/pkg/indicator/v2_tr.go @@ -9,7 +9,7 @@ import ( // This TRStream calculates the ATR first type TRStream struct { // embedded struct - Float64Series + *Float64Series // private states previousClose float64