types,cmd: add IntervalMap type to refactor the interval code

This commit is contained in:
c9s 2023-07-03 15:14:48 +08:00
parent d60dbe5e0b
commit ea130e434c
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 13 additions and 15 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
@ -697,7 +696,7 @@ func confirmation(s string) bool {
}
}
func getExchangeIntervals(ex types.Exchange) map[types.Interval]int {
func getExchangeIntervals(ex types.Exchange) types.IntervalMap {
exCustom, ok := ex.(types.CustomIntervalProvider)
if ok {
return exCustom.SupportedInterval()
@ -705,13 +704,6 @@ func getExchangeIntervals(ex types.Exchange) map[types.Interval]int {
return types.SupportedIntervals
}
func sortIntervals(intervals []types.Interval) types.IntervalSlice {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].Duration() < intervals[j].Duration()
})
return intervals
}
func sync(ctx context.Context, userConfig *bbgo.Config, backtestService *service.BacktestService, sourceExchanges map[types.ExchangeName]types.Exchange, syncFrom, syncTo time.Time) error {
for _, symbol := range userConfig.Backtest.Symbols {
for _, sourceExchange := range sourceExchanges {
@ -722,11 +714,7 @@ func sync(ctx context.Context, userConfig *bbgo.Config, backtestService *service
}
// sort intervals
var intervals types.IntervalSlice
for interval := range supportIntervals {
intervals = append(intervals, interval)
}
var intervals = supportIntervals.Slice()
intervals.Sort()
for _, interval := range intervals {

View File

@ -142,7 +142,17 @@ func ParseInterval(input Interval) int {
return t
}
var SupportedIntervals = map[Interval]int{
type IntervalMap map[Interval]int
func (m IntervalMap) Slice() (slice IntervalSlice) {
for interval := range m {
slice = append(slice, interval)
}
return slice
}
var SupportedIntervals = IntervalMap{
Interval1s: 1,
Interval1m: 1 * 60,
Interval3m: 3 * 60,