indicator: use pointer for float64series

This commit is contained in:
c9s 2023-06-09 13:18:08 +08:00
parent ea3b1cc937
commit 9d9f898f17
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
17 changed files with 29 additions and 26 deletions

View File

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

View File

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

View File

@ -1,7 +1,7 @@
package indicator
type CMAStream struct {
Float64Series
*Float64Series
}
func CMA2(source Float64Source) *CMAStream {

View File

@ -13,7 +13,7 @@ const (
// CrossStream subscribes 2 upstreams, and calculate the cross signal
type CrossStream struct {
Float64Series
*Float64Series
a, b floats.Slice
}

View File

@ -1,7 +1,7 @@
package indicator
type EWMAStream struct {
Float64Series
*Float64Series
window int
multiplier float64

View File

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

View File

@ -3,7 +3,7 @@ package indicator
import "github.com/c9s/bbgo/pkg/datatype/floats"
type MultiplyStream struct {
Float64Series
*Float64Series
a, b floats.Slice
}

View File

@ -5,7 +5,7 @@ import (
)
type PivotHighStream struct {
Float64Series
*Float64Series
rawValues floats.Slice
window, rightWindow int
}

View File

@ -5,7 +5,7 @@ import (
)
type PivotLowStream struct {
Float64Series
*Float64Series
rawValues floats.Slice
window, rightWindow int
}

View File

@ -11,22 +11,23 @@ type KLineSubscription interface {
}
type PriceStream struct {
Float64Series
*Float64Series
mapper KLineValueMapper
}
func Price(source KLineSubscription, mapper KLineValueMapper) *PriceStream {
s := &PriceStream{
Float64Series: NewFloat64Series(),
mapper: mapper,
}
s.SeriesBase.Series = s.slice
if source != nil {
source.AddSubscriber(func(k types.KLine) {
v := s.mapper(k)
s.PushAndEmit(v)
})
}
return s
}

View File

@ -2,7 +2,7 @@ package indicator
type RMAStream struct {
// embedded structs
Float64Series
*Float64Series
// config fields
Adjust bool

View File

@ -2,7 +2,7 @@ package indicator
type RSIStream struct {
// embedded structs
Float64Series
*Float64Series
// config fields
window int

View File

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

View File

@ -3,7 +3,7 @@ package indicator
import "github.com/c9s/bbgo/pkg/types"
type SMAStream struct {
Float64Series
*Float64Series
window int
rawValues *types.Queue
}

View File

@ -3,7 +3,7 @@ package indicator
import "github.com/c9s/bbgo/pkg/types"
type StdDevStream struct {
Float64Series
*Float64Series
rawValues *types.Queue

View File

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

View File

@ -9,7 +9,7 @@ import (
// This TRStream calculates the ATR first
type TRStream struct {
// embedded struct
Float64Series
*Float64Series
// private states
previousClose float64