From 775ad7d9063402bc4603eebeee930583f92a0bda Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 30 Jun 2023 10:58:07 +0800 Subject: [PATCH] indicator: improve kline stream backfill --- pkg/indicator/klinestream.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/indicator/klinestream.go b/pkg/indicator/klinestream.go index 156c8b97e..11215b3e7 100644 --- a/pkg/indicator/klinestream.go +++ b/pkg/indicator/klinestream.go @@ -28,16 +28,21 @@ func (s *KLineStream) Last(i int) *types.KLine { func (s *KLineStream) AddSubscriber(f func(k types.KLine)) { s.OnUpdate(f) - if len(s.kLines) > 0 { - // push historical klines to the subscriber - for _, k := range s.kLines { - f(k) - } + if len(s.kLines) == 0 { + return + } + + // push historical klines to the subscriber + for _, k := range s.kLines { + f(k) } } func (s *KLineStream) BackFill(kLines []types.KLine) { - s.kLines = append(s.kLines, kLines...) + for _, k := range kLines { + s.kLines = append(s.kLines, k) + s.EmitUpdate(k) + } } // KLines creates a KLine stream that pushes the klines to the subscribers