2023-05-26 06:31:06 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2023-05-26 07:18:43 +00:00
|
|
|
type KLineSubscription interface {
|
|
|
|
AddSubscriber(f func(k types.KLine))
|
2023-05-26 06:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PriceStream struct {
|
|
|
|
types.SeriesBase
|
|
|
|
Float64Updater
|
|
|
|
|
|
|
|
slice floats.Slice
|
|
|
|
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{
|
|
|
|
mapper: mapper,
|
|
|
|
}
|
2023-05-26 07:18:43 +00:00
|
|
|
|
2023-05-26 06:31:06 +00:00
|
|
|
s.SeriesBase.Series = s.slice
|
2023-05-26 07:18:43 +00:00
|
|
|
|
|
|
|
source.AddSubscriber(func(k types.KLine) {
|
2023-05-26 06:31:06 +00:00
|
|
|
v := s.mapper(k)
|
|
|
|
s.slice.Push(v)
|
|
|
|
s.EmitUpdate(v)
|
|
|
|
})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|