From 1cc4c69c6627f50c686bcfc090615d6624e4b02b Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 17 May 2022 18:23:09 +0800 Subject: [PATCH] move and refactor functions --- pkg/backtest/report.go | 31 ++++++++++++++++++++++++ pkg/cmd/backtest.go | 54 ++++++------------------------------------ pkg/types/time.go | 5 ++++ pkg/util/dir.go | 23 ++++++++++++++++++ pkg/util/json.go | 15 ++++++++++++ 5 files changed, 81 insertions(+), 47 deletions(-) create mode 100644 pkg/util/dir.go create mode 100644 pkg/util/json.go diff --git a/pkg/backtest/report.go b/pkg/backtest/report.go index 3af4d9348..d353ee95e 100644 --- a/pkg/backtest/report.go +++ b/pkg/backtest/report.go @@ -1,7 +1,11 @@ package backtest import ( + "encoding/json" "fmt" + "io/ioutil" + "os" + "path/filepath" "strings" "time" @@ -9,6 +13,7 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) type Run struct { @@ -56,3 +61,29 @@ func FormatSessionName(sessions []string, symbols []string, startTime, endTime t endTime.Format(SessionTimeFormat), ) } + +func WriteReportIndex(outputDirectory string, reportIndex *ReportIndex) error { + indexFile := filepath.Join(outputDirectory, "index.json") + if err := util.WriteJsonFile(indexFile, reportIndex); err != nil { + return err + } + return nil +} + +func LoadReportIndex(outputDirectory string) (*ReportIndex, error) { + var reportIndex ReportIndex + indexFile := filepath.Join(outputDirectory, "index.json") + if _, err := os.Stat(indexFile); err == nil { + o, err := ioutil.ReadFile(indexFile) + if err != nil { + return nil, err + } + + if err := json.Unmarshal(o, &reportIndex); err != nil { + return nil, err + } + } + + return &reportIndex, nil +} + diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index e5b7a2a74..e5d663783 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -3,9 +3,7 @@ package cmd import ( "bufio" "context" - "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -26,6 +24,7 @@ import ( "github.com/c9s/bbgo/pkg/data/tsv" "github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/types" + "github.com/c9s/bbgo/pkg/util" ) func init() { @@ -301,7 +300,7 @@ var BacktestCmd = &cobra.Command{ } kLineDataDir := filepath.Join(reportDir, "klines") - if err := safeMkdirAll(kLineDataDir); err != nil { + if err := util.SafeMkdirAll(kLineDataDir); err != nil { return err } @@ -526,7 +525,7 @@ var BacktestCmd = &cobra.Command{ Manifests: manifests, } - if err := writeJsonFile(filepath.Join(outputDirectory, symbol+".json"), &result); err != nil { + if err := util.WriteJsonFile(filepath.Join(outputDirectory, symbol+".json"), &result); err != nil { return err } } @@ -581,17 +580,9 @@ var BacktestCmd = &cobra.Command{ if generatingReport && reportFileInSubDir { // append report index - var reportIndex backtest.ReportIndex - indexFile := filepath.Join(outputDirectory, "index.json") - if _, err := os.Stat(indexFile); err == nil { - o, err := ioutil.ReadFile(indexFile) - if err != nil { - return err - } - - if err := json.Unmarshal(o, &reportIndex); err != nil { - return err - } + reportIndex, err := backtest.LoadReportIndex(outputDirectory) + if err != nil { + return err } reportIndex.Runs = append(reportIndex.Runs, backtest.Run{ @@ -600,12 +591,7 @@ var BacktestCmd = &cobra.Command{ Time: time.Now(), }) - o, err := json.MarshalIndent(reportIndex, "", " ") - if err != nil { - return err - } - - if err := ioutil.WriteFile(indexFile, o, 0644); err != nil { + if err := backtest.WriteReportIndex(outputDirectory, reportIndex); err != nil { return err } } @@ -646,32 +632,6 @@ func confirmation(s string) bool { } } -func writeJsonFile(p string, obj interface{}) error { - out, err := json.MarshalIndent(obj, "", " ") - if err != nil { - return err - } - - return ioutil.WriteFile(p, out, 0644) -} - -func safeMkdirAll(p string) error { - st, err := os.Stat(p) - if err == nil { - if !st.IsDir() { - return fmt.Errorf("path %s is not a directory", p) - } - - return nil - } - - if os.IsNotExist(err) { - return os.MkdirAll(p, 0755) - } - - return nil -} - func toExchangeSources(sessions map[string]*bbgo.ExchangeSession) (exchangeSources []backtest.ExchangeDataSource, err error) { for _, session := range sessions { exchange := session.Exchange.(*backtest.Exchange) diff --git a/pkg/types/time.go b/pkg/types/time.go index 8502b605c..bf3e286d1 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -266,6 +266,11 @@ func (t *LooseFormatTime) UnmarshalJSON(data []byte) error { return nil } +func (t LooseFormatTime) MarshalJSON() ([]byte, error) { + return []byte(strconv.Quote(time.Time(t).Format(time.RFC3339))), nil +} + + func (t LooseFormatTime) Time() time.Time { return time.Time(t) } diff --git a/pkg/util/dir.go b/pkg/util/dir.go new file mode 100644 index 000000000..5e1914c0e --- /dev/null +++ b/pkg/util/dir.go @@ -0,0 +1,23 @@ +package util + +import ( + "fmt" + "os" +) + +func SafeMkdirAll(p string) error { + st, err := os.Stat(p) + if err == nil { + if !st.IsDir() { + return fmt.Errorf("path %s is not a directory", p) + } + + return nil + } + + if os.IsNotExist(err) { + return os.MkdirAll(p, 0755) + } + + return nil +} diff --git a/pkg/util/json.go b/pkg/util/json.go new file mode 100644 index 000000000..62ea74311 --- /dev/null +++ b/pkg/util/json.go @@ -0,0 +1,15 @@ +package util + +import ( + "encoding/json" + "io/ioutil" +) + +func WriteJsonFile(p string, obj interface{}) error { + out, err := json.MarshalIndent(obj, "", " ") + if err != nil { + return err + } + + return ioutil.WriteFile(p, out, 0644) +}