2022-05-19 09:27:59 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-05-19 17:42:32 +00:00
|
|
|
"fmt"
|
2022-07-06 18:11:52 +00:00
|
|
|
"io"
|
2022-05-19 09:27:59 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2022-07-06 18:11:52 +00:00
|
|
|
"strconv"
|
2022-05-19 09:27:59 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2022-07-06 18:11:52 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/data/tsv"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-05-19 09:27:59 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/optimizer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file")
|
2022-05-19 10:23:12 +00:00
|
|
|
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")
|
2022-07-06 18:11:52 +00:00
|
|
|
optimizeCmd.Flags().Bool("tsv", false, "print optimizer metrics in csv format")
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:11:52 +00:00
|
|
|
printTsvFormat, err := cmd.Flags().GetBool("tsv")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 10:23:12 +00:00
|
|
|
outputDirectory, err := cmd.Flags().GetString("output")
|
|
|
|
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
|
|
|
|
|
2022-05-19 10:23:12 +00:00
|
|
|
configDir, err := os.MkdirTemp("", "bbgo-config-*")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-19 09:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 10:23:12 +00:00
|
|
|
executor := &optimizer.LocalProcessExecutor{
|
2022-06-21 04:31:42 +00:00
|
|
|
Config: optConfig.Executor.LocalExecutorConfig,
|
2022-05-19 10:23:12 +00:00
|
|
|
Bin: os.Args[0],
|
|
|
|
WorkDir: ".",
|
|
|
|
ConfigDir: configDir,
|
|
|
|
OutputDir: outputDirectory,
|
2022-05-19 09:27:59 +00:00
|
|
|
}
|
2022-05-19 10:23:12 +00:00
|
|
|
|
|
|
|
optz := &optimizer.GridOptimizer{
|
|
|
|
Config: optConfig,
|
2022-05-19 09:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 07:28:11 +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))
|
2022-07-06 18:11:52 +00:00
|
|
|
} else if printTsvFormat {
|
|
|
|
if err := formatMetricsTsv(metrics, os.Stdout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-19 17:42:32 +00:00
|
|
|
} else {
|
2022-06-20 06:52:40 +00:00
|
|
|
for n, values := range metrics {
|
|
|
|
if len(values) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%v => %s\n", values[0].Labels, n)
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2022-07-06 18:11:52 +00:00
|
|
|
|
|
|
|
func transformMetricsToRows(metrics map[string][]optimizer.Metric) (headers []string, rows [][]interface{}) {
|
|
|
|
var metricsKeys []string
|
|
|
|
for k := range metrics {
|
|
|
|
metricsKeys = append(metricsKeys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
var numEntries int
|
|
|
|
var paramLabels []string
|
|
|
|
for _, ms := range metrics {
|
|
|
|
for _, m := range ms {
|
|
|
|
paramLabels = m.Labels
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
numEntries = len(ms)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = append(paramLabels, metricsKeys...)
|
|
|
|
rows = make([][]interface{}, numEntries)
|
|
|
|
|
|
|
|
var metricsRows = make([][]interface{}, numEntries)
|
|
|
|
|
|
|
|
// build params into the rows
|
|
|
|
for i, m := range metrics[metricsKeys[0]] {
|
|
|
|
rows[i] = m.Params
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, metricKey := range metricsKeys {
|
|
|
|
for i, ms := range metrics[metricKey] {
|
|
|
|
if len(metricsRows[i]) == 0 {
|
|
|
|
metricsRows[i] = make([]interface{}, 0, len(metricsKeys))
|
|
|
|
}
|
|
|
|
metricsRows[i] = append(metricsRows[i], ms.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge rows
|
|
|
|
for i := range rows {
|
|
|
|
rows[i] = append(rows[i], metricsRows[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers, rows
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatMetricsTsv(metrics map[string][]optimizer.Metric, writer io.WriteCloser) error {
|
|
|
|
headers, rows := transformMetricsToRows(metrics)
|
|
|
|
w := tsv.NewWriter(writer)
|
|
|
|
if err := w.Write(headers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
var cells []string
|
|
|
|
for _, o := range row {
|
|
|
|
cell, err := castCellValue(o)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cells = append(cells, cell)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Write(cells); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func castCellValue(a interface{}) (string, error) {
|
|
|
|
switch tv := a.(type) {
|
|
|
|
case fixedpoint.Value:
|
|
|
|
return tv.String(), nil
|
|
|
|
case float64:
|
|
|
|
return strconv.FormatFloat(tv, 'f', -1, 64), nil
|
|
|
|
case int64:
|
|
|
|
return strconv.FormatInt(tv, 10), nil
|
|
|
|
case int32:
|
|
|
|
return strconv.FormatInt(int64(tv), 10), nil
|
|
|
|
case int:
|
|
|
|
return strconv.Itoa(tv), nil
|
|
|
|
case bool:
|
|
|
|
return strconv.FormatBool(tv), nil
|
|
|
|
case string:
|
|
|
|
return tv, nil
|
|
|
|
case []byte:
|
|
|
|
return string(tv), nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unsupported object type: %T value: %v", tv, tv)
|
|
|
|
}
|
|
|
|
}
|