mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
move and refactor functions
This commit is contained in:
parent
6c0165afe4
commit
1cc4c69c66
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
23
pkg/util/dir.go
Normal file
23
pkg/util/dir.go
Normal file
|
@ -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
|
||||
}
|
15
pkg/util/json.go
Normal file
15
pkg/util/json.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user