From eb04eaeea44ee3ac27cb0cc6f303d3b5859d63dd Mon Sep 17 00:00:00 2001 From: Edwin Date: Mon, 13 Nov 2023 12:51:19 +0800 Subject: [PATCH] pkg/exchange: types.kline end time should -1 time.Millisecond --- pkg/exchange/bitget/convert.go | 2 ++ pkg/exchange/bybit/convert.go | 2 +- pkg/exchange/bybit/convert_test.go | 4 ++-- pkg/types/kline.go | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/exchange/bitget/convert.go b/pkg/exchange/bitget/convert.go index a5e0e91a7..6c3c92546 100644 --- a/pkg/exchange/bitget/convert.go +++ b/pkg/exchange/bitget/convert.go @@ -346,6 +346,8 @@ func toGlobalBalanceMap(balances []Balance) types.BalanceMap { func toGlobalKLines(symbol string, interval types.Interval, kLines v2.KLineResponse) []types.KLine { gKLines := make([]types.KLine, len(kLines)) for i, kline := range kLines { + // follow the binance rule, to avoid endTime overlapping with the next startTime. So we subtract -1 time.Millisecond + // on endTime. endTime := types.Time(kline.Ts.Time().Add(interval.Duration() - time.Millisecond)) gKLines[i] = types.KLine{ Exchange: types.ExchangeBitget, diff --git a/pkg/exchange/bybit/convert.go b/pkg/exchange/bybit/convert.go index d89f194fe..8c2d91541 100644 --- a/pkg/exchange/bybit/convert.go +++ b/pkg/exchange/bybit/convert.go @@ -368,7 +368,7 @@ func toLocalInterval(interval types.Interval) (string, error) { func toGlobalKLines(symbol string, interval types.Interval, klines []bybitapi.KLine) []types.KLine { gKLines := make([]types.KLine, len(klines)) for i, kline := range klines { - endTime := types.Time(kline.StartTime.Time().Add(interval.Duration())) + endTime := types.Time(kline.StartTime.Time().Add(interval.Duration() - time.Millisecond)) gKLines[i] = types.KLine{ Exchange: types.ExchangeBybit, Symbol: symbol, diff --git a/pkg/exchange/bybit/convert_test.go b/pkg/exchange/bybit/convert_test.go index a5ddb08bc..daac379e3 100644 --- a/pkg/exchange/bybit/convert_test.go +++ b/pkg/exchange/bybit/convert_test.go @@ -836,7 +836,7 @@ func Test_toGlobalKLines(t *testing.T) { Exchange: types.ExchangeBybit, Symbol: resp.Symbol, StartTime: types.Time(resp.List[0].StartTime.Time()), - EndTime: types.Time(resp.List[0].StartTime.Time().Add(interval.Duration())), + EndTime: types.Time(resp.List[0].StartTime.Time().Add(interval.Duration() - time.Millisecond)), Interval: interval, Open: fixedpoint.NewFromFloat(29045.3), Close: fixedpoint.NewFromFloat(29228.56), @@ -850,7 +850,7 @@ func Test_toGlobalKLines(t *testing.T) { Exchange: types.ExchangeBybit, Symbol: resp.Symbol, StartTime: types.Time(resp.List[1].StartTime.Time()), - EndTime: types.Time(resp.List[1].StartTime.Time().Add(interval.Duration())), + EndTime: types.Time(resp.List[1].StartTime.Time().Add(interval.Duration() - time.Millisecond)), Interval: interval, Open: fixedpoint.NewFromFloat(29167.33), Close: fixedpoint.NewFromFloat(29045.3), diff --git a/pkg/types/kline.go b/pkg/types/kline.go index 6bf863c30..bd31ed9a2 100644 --- a/pkg/types/kline.go +++ b/pkg/types/kline.go @@ -54,7 +54,9 @@ type KLine struct { Symbol string `json:"symbol" db:"symbol"` StartTime Time `json:"startTime" db:"start_time"` - EndTime Time `json:"endTime" db:"end_time"` + // EndTime follows the binance rule, to avoid endTime overlapping with the next startTime. So if your end time (2023-01-01 01:00:00) + // are overlapping with next start time interval (2023-01-01 01:00:00), you should subtract -1 time.millisecond on EndTime. + EndTime Time `json:"endTime" db:"end_time"` Interval Interval `json:"interval" db:"interval"`