From 5d85ceeec4f5493ba6b02b2e3b14d48a009ed67b Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 19 Aug 2022 17:55:48 +0800 Subject: [PATCH] service/backtest: filter klines that will be closed in the future --- pkg/service/backtest.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/service/backtest.go b/pkg/service/backtest.go index 0e759526e..3a73726f3 100644 --- a/pkg/service/backtest.go +++ b/pkg/service/backtest.go @@ -37,6 +37,7 @@ func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange type _, _ = s.DB.Exec("PRAGMA synchronous = NORMAL") } + now := time.Now() tasks := []SyncTask{ { Type: types.KLine{}, @@ -46,7 +47,16 @@ func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange type }, Filter: func(obj interface{}) bool { k := obj.(types.KLine) - return !k.EndTime.Before(k.StartTime.Time().Add(k.Interval.Duration() - time.Second)) + if k.EndTime.Before(k.StartTime.Time().Add(k.Interval.Duration() - time.Second)) { + return false + } + + // Filter klines that has the endTime closed in the future + if k.EndTime.After(now) { + return false + } + + return true }, ID: func(obj interface{}) string { kline := obj.(types.KLine)