pkg/exchange: types.kline end time should -1 time.Millisecond

This commit is contained in:
Edwin 2023-11-13 12:51:19 +08:00
parent 755ea5e427
commit eb04eaeea4
4 changed files with 8 additions and 4 deletions

View File

@ -346,6 +346,8 @@ func toGlobalBalanceMap(balances []Balance) types.BalanceMap {
func toGlobalKLines(symbol string, interval types.Interval, kLines v2.KLineResponse) []types.KLine { func toGlobalKLines(symbol string, interval types.Interval, kLines v2.KLineResponse) []types.KLine {
gKLines := make([]types.KLine, len(kLines)) gKLines := make([]types.KLine, len(kLines))
for i, kline := range 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)) endTime := types.Time(kline.Ts.Time().Add(interval.Duration() - time.Millisecond))
gKLines[i] = types.KLine{ gKLines[i] = types.KLine{
Exchange: types.ExchangeBitget, Exchange: types.ExchangeBitget,

View File

@ -368,7 +368,7 @@ func toLocalInterval(interval types.Interval) (string, error) {
func toGlobalKLines(symbol string, interval types.Interval, klines []bybitapi.KLine) []types.KLine { func toGlobalKLines(symbol string, interval types.Interval, klines []bybitapi.KLine) []types.KLine {
gKLines := make([]types.KLine, len(klines)) gKLines := make([]types.KLine, len(klines))
for i, kline := range 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{ gKLines[i] = types.KLine{
Exchange: types.ExchangeBybit, Exchange: types.ExchangeBybit,
Symbol: symbol, Symbol: symbol,

View File

@ -836,7 +836,7 @@ func Test_toGlobalKLines(t *testing.T) {
Exchange: types.ExchangeBybit, Exchange: types.ExchangeBybit,
Symbol: resp.Symbol, Symbol: resp.Symbol,
StartTime: types.Time(resp.List[0].StartTime.Time()), 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, Interval: interval,
Open: fixedpoint.NewFromFloat(29045.3), Open: fixedpoint.NewFromFloat(29045.3),
Close: fixedpoint.NewFromFloat(29228.56), Close: fixedpoint.NewFromFloat(29228.56),
@ -850,7 +850,7 @@ func Test_toGlobalKLines(t *testing.T) {
Exchange: types.ExchangeBybit, Exchange: types.ExchangeBybit,
Symbol: resp.Symbol, Symbol: resp.Symbol,
StartTime: types.Time(resp.List[1].StartTime.Time()), 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, Interval: interval,
Open: fixedpoint.NewFromFloat(29167.33), Open: fixedpoint.NewFromFloat(29167.33),
Close: fixedpoint.NewFromFloat(29045.3), Close: fixedpoint.NewFromFloat(29045.3),

View File

@ -54,7 +54,9 @@ type KLine struct {
Symbol string `json:"symbol" db:"symbol"` Symbol string `json:"symbol" db:"symbol"`
StartTime Time `json:"startTime" db:"start_time"` 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"` Interval Interval `json:"interval" db:"interval"`