From ea130e434c8676ecb3b86ba742c52206e211a2f6 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 3 Jul 2023 15:14:48 +0800 Subject: [PATCH] types,cmd: add IntervalMap type to refactor the interval code --- pkg/cmd/backtest.go | 16 ++-------------- pkg/types/interval.go | 12 +++++++++++- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index 3982aab5d..6ebd73a13 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -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 { diff --git a/pkg/types/interval.go b/pkg/types/interval.go index 054db6d5f..338006193 100644 --- a/pkg/types/interval.go +++ b/pkg/types/interval.go @@ -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,