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