bbgo_origin/pkg/indicator/v2_price.go

54 lines
1.0 KiB
Go
Raw Normal View History

package indicator
import (
"github.com/c9s/bbgo/pkg/types"
)
type KLineSubscription interface {
AddSubscriber(f func(k types.KLine))
2023-06-02 10:56:28 +00:00
Length() int
Last(i int) *types.KLine
}
type PriceStream struct {
*Float64Series
mapper KLineValueMapper
}
func Price(source KLineSubscription, mapper KLineValueMapper) *PriceStream {
s := &PriceStream{
Float64Series: NewFloat64Series(),
mapper: mapper,
}
if source != nil {
source.AddSubscriber(func(k types.KLine) {
v := s.mapper(k)
s.PushAndEmit(v)
})
}
return s
}
2023-05-31 08:30:04 +00:00
func (s *PriceStream) PushAndEmit(v float64) {
s.slice.Push(v)
s.EmitUpdate(v)
}
func ClosePrices(source KLineSubscription) *PriceStream {
return Price(source, KLineClosePriceMapper)
}
func LowPrices(source KLineSubscription) *PriceStream {
return Price(source, KLineLowPriceMapper)
}
func HighPrices(source KLineSubscription) *PriceStream {
return Price(source, KLineHighPriceMapper)
}
func OpenPrices(source KLineSubscription) *PriceStream {
return Price(source, KLineOpenPriceMapper)
}