bbgo_origin/pkg/indicator/v2/price.go

68 lines
1.3 KiB
Go
Raw Normal View History

2023-07-10 08:54:22 +00:00
package indicatorv2
import (
"github.com/c9s/bbgo/pkg/types"
)
type PriceStream struct {
2023-07-10 08:54:22 +00:00
*types.Float64Series
2023-07-10 08:54:22 +00:00
mapper types.KLineValueMapper
}
2023-07-10 08:54:22 +00:00
func Price(source KLineSubscription, mapper types.KLineValueMapper) *PriceStream {
s := &PriceStream{
2023-07-10 08:54:22 +00:00
Float64Series: types.NewFloat64Series(),
mapper: mapper,
}
if source == nil {
return s
}
source.AddSubscriber(func(k types.KLine) {
v := s.mapper(k)
s.PushAndEmit(v)
})
return s
}
// AddSubscriber adds the subscriber function and push historical data to the subscriber
func (s *PriceStream) AddSubscriber(f func(v float64)) {
s.OnUpdate(f)
2023-07-10 08:54:22 +00:00
if len(s.Slice) == 0 {
return
}
// push historical value to the subscriber
2023-07-10 08:54:22 +00:00
for _, v := range s.Slice {
f(v)
}
}
2023-05-31 08:30:04 +00:00
func (s *PriceStream) PushAndEmit(v float64) {
2023-07-10 08:54:22 +00:00
s.Slice.Push(v)
2023-05-31 08:30:04 +00:00
s.EmitUpdate(v)
}
func ClosePrices(source KLineSubscription) *PriceStream {
2023-07-10 08:54:22 +00:00
return Price(source, types.KLineClosePriceMapper)
}
func LowPrices(source KLineSubscription) *PriceStream {
2023-07-10 08:54:22 +00:00
return Price(source, types.KLineLowPriceMapper)
}
func HighPrices(source KLineSubscription) *PriceStream {
2023-07-10 08:54:22 +00:00
return Price(source, types.KLineHighPriceMapper)
}
func OpenPrices(source KLineSubscription) *PriceStream {
2023-07-10 08:54:22 +00:00
return Price(source, types.KLineOpenPriceMapper)
}
2023-06-30 02:37:42 +00:00
func Volumes(source KLineSubscription) *PriceStream {
2023-07-10 08:54:22 +00:00
return Price(source, types.KLineVolumeMapper)
2023-06-30 02:37:42 +00:00
}