Merge pull request #889 from c9s/fix/backtest-last-kline-sync

service/backtest: check and filter kline by its endTime
This commit is contained in:
Yo-An Lin 2022-08-19 18:42:41 +08:00 committed by GitHub
commit 0764107199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -37,6 +37,7 @@ func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange type
_, _ = s.DB.Exec("PRAGMA synchronous = NORMAL") _, _ = s.DB.Exec("PRAGMA synchronous = NORMAL")
} }
now := time.Now()
tasks := []SyncTask{ tasks := []SyncTask{
{ {
Type: types.KLine{}, Type: types.KLine{},
@ -44,6 +45,19 @@ func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange type
Time: func(obj interface{}) time.Time { Time: func(obj interface{}) time.Time {
return obj.(types.KLine).StartTime.Time() return obj.(types.KLine).StartTime.Time()
}, },
Filter: func(obj interface{}) bool {
k := obj.(types.KLine)
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 { ID: func(obj interface{}) string {
kline := obj.(types.KLine) kline := obj.(types.KLine)
return strconv.FormatInt(kline.StartTime.UnixMilli(), 10) return strconv.FormatInt(kline.StartTime.UnixMilli(), 10)

View File

@ -35,6 +35,8 @@ type SyncTask struct {
OnLoad func(objs interface{}) OnLoad func(objs interface{})
// Filter is an optional field, which is used for filtering the remote records // Filter is an optional field, which is used for filtering the remote records
// Return true to keep the record,
// Return false to filter the record.
Filter func(obj interface{}) bool Filter func(obj interface{}) bool
// BatchQuery is used for querying remote records. // BatchQuery is used for querying remote records.