bbgo_origin/pkg/cmd/optimize.go

147 lines
3.1 KiB
Go
Raw Normal View History

2022-05-19 09:27:59 +00:00
package cmd
import (
"context"
"encoding/json"
2022-05-19 17:42:32 +00:00
"fmt"
2022-05-19 09:27:59 +00:00
"io/ioutil"
"os"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/optimizer"
)
func init() {
optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file")
optimizeCmd.Flags().String("output", "output", "backtest report output directory")
2022-05-19 17:42:32 +00:00
optimizeCmd.Flags().Bool("json", false, "print optimizer metrics in json format")
optimizeCmd.Flags().Bool("tsv", false, "print optimizer metrics in csv format")
2022-09-20 20:49:21 +00:00
optimizeCmd.Flags().Int("limit", 50, "limit how many results to print pr metric")
2022-05-19 09:27:59 +00:00
RootCmd.AddCommand(optimizeCmd)
}
var optimizeCmd = &cobra.Command{
Use: "optimize",
Short: "run optimizer",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
optimizerConfigFilename, err := cmd.Flags().GetString("optimizer-config")
if err != nil {
return err
}
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
2022-05-19 17:42:32 +00:00
printJsonFormat, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}
printTsvFormat, err := cmd.Flags().GetBool("tsv")
if err != nil {
return err
}
outputDirectory, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
2022-09-20 20:49:21 +00:00
resultLimit, err := cmd.Flags().GetInt("limit")
if err != nil {
return err
}
2022-05-19 09:27:59 +00:00
yamlBody, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
var obj map[string]interface{}
if err := yaml.Unmarshal(yamlBody, &obj); err != nil {
return err
}
delete(obj, "notifications")
delete(obj, "sync")
optConfig, err := optimizer.LoadConfig(optimizerConfigFilename)
if err != nil {
return err
}
// the config json template used for patch
configJson, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_ = ctx
configDir, err := os.MkdirTemp("", "bbgo-config-*")
if err != nil {
return err
2022-05-19 09:27:59 +00:00
}
executor := &optimizer.LocalProcessExecutor{
Config: optConfig.Executor.LocalExecutorConfig,
Bin: os.Args[0],
WorkDir: ".",
ConfigDir: configDir,
OutputDir: outputDirectory,
2022-05-19 09:27:59 +00:00
}
optz := &optimizer.GridOptimizer{
Config: optConfig,
2022-05-19 09:27:59 +00:00
}
if err := executor.Prepare(configJson); err != nil {
return err
}
2022-05-19 17:42:32 +00:00
metrics, err := optz.Run(executor, configJson)
if err != nil {
return err
}
if printJsonFormat {
out, err := json.MarshalIndent(metrics, "", " ")
if err != nil {
return err
}
// print metrics JSON to stdout
fmt.Println(string(out))
} else if printTsvFormat {
if err := optimizer.FormatMetricsTsv(os.Stdout, metrics); err != nil {
return err
}
2022-05-19 17:42:32 +00:00
} else {
for n, values := range metrics {
if len(values) == 0 {
continue
}
2022-10-05 06:51:30 +00:00
if len(values) > resultLimit && resultLimit != 0 {
2022-09-22 07:16:37 +00:00
values = values[:resultLimit]
2022-09-20 20:49:21 +00:00
}
fmt.Printf("%v => %s\n", values[0].Labels, n)
2022-09-22 07:16:37 +00:00
for _, m := range values {
fmt.Printf("%v => %s %v\n", m.Params, n, m.Value)
}
2022-05-19 17:42:32 +00:00
}
}
return nil
2022-05-19 09:27:59 +00:00
},
}