diff --git a/go.mod b/go.mod index 6f8553d1e..b479f1385 100644 --- a/go.mod +++ b/go.mod @@ -57,6 +57,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/denisenkom/go-mssqldb v0.12.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/evanphx/json-patch/v5 v5.6.0 // indirect github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/gin-contrib/sse v0.1.0 // indirect diff --git a/go.sum b/go.sum index 6160e67bd..3eb306306 100644 --- a/go.sum +++ b/go.sum @@ -112,6 +112,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww= +github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJdVkTrXpu9KtoVCSo1hg7mtI7G9KU= github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0yfBxPvZrHkprdPPTTS3N5rwmLE8T22KBXlw= github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc= @@ -265,6 +267,7 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w= github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= diff --git a/pkg/cmd/optimize.go b/pkg/cmd/optimize.go new file mode 100644 index 000000000..57d15bf81 --- /dev/null +++ b/pkg/cmd/optimize.go @@ -0,0 +1,171 @@ +package cmd + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "os" + + jsonpatch "github.com/evanphx/json-patch/v5" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "gopkg.in/yaml.v3" + + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/optimizer" +) + +func init() { + optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file") + 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 + } + + 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 + } + log.Info(string(configJson)) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + _ = ctx + + log.Info(os.Args) + binary := os.Args[0] + _ = binary + + type OpFunc func(configJson []byte, next func(configJson []byte) error) error + var ops []OpFunc + + for _, selector := range optConfig.Matrix { + path := selector.Path + + switch selector.Type { + case "range": + min := selector.Min + max := selector.Max + step := selector.Step + if step.IsZero() { + step = fixedpoint.One + } + + f := func(configJson []byte, next func(configJson []byte) error) error { + var values []fixedpoint.Value + for val := min; val.Compare(max) < 0; val = val.Add(step) { + values = append(values, val) + } + + log.Infof("ranged values: %v", values) + for _, val := range values { + jsonOp := []byte(fmt.Sprintf(`[ {"op": "replace", "path": "%s", "value": %v } ]`, path, val)) + patch, err := jsonpatch.DecodePatch(jsonOp) + if err != nil { + return err + } + + log.Debugf("json op: %s", jsonOp) + + configJson, err := patch.ApplyIndent(configJson, " ") + if err != nil { + return err + } + + if err := next(configJson); err != nil { + return err + } + } + + return nil + } + ops = append(ops, f) + + case "iterate": + values := selector.Values + f := func(configJson []byte, next func(configJson []byte) error) error { + log.Infof("iterate values: %v", values) + for _, val := range values { + jsonOp := []byte(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": "%s"}]`, path, val)) + patch, err := jsonpatch.DecodePatch(jsonOp) + if err != nil { + return err + } + + log.Debugf("json op: %s", jsonOp) + + configJson, err := patch.ApplyIndent(configJson, " ") + if err != nil { + return err + } + + if err := next(configJson); err != nil { + return err + } + } + + return nil + } + ops = append(ops, f) + } + } + + var last = func(configJson []byte, next func(configJson []byte) error) error { + log.Info("configJson", string(configJson)) + return nil + } + ops = append(ops, last) + + log.Infof("%d ops: %v", len(ops), ops) + + var wrapper = func(configJson []byte) error { return nil } + for i := len(ops) - 1; i > 0; i-- { + next := ops[i] + cur := ops[i-1] + inner := wrapper + a := i + wrapper = func(configJson []byte) error { + log.Infof("wrapper fn #%d", a) + return cur(configJson, func(configJson []byte) error { + return next(configJson, inner) + }) + } + } + + return wrapper(configJson) + }, +} diff --git a/pkg/optimizer/config.go b/pkg/optimizer/config.go new file mode 100644 index 000000000..40d1ff563 --- /dev/null +++ b/pkg/optimizer/config.go @@ -0,0 +1,36 @@ +package optimizer + +import ( + "io/ioutil" + + "gopkg.in/yaml.v3" + + "github.com/c9s/bbgo/pkg/fixedpoint" +) + +type SelectorConfig struct { + Type string `json:"type" yaml:"type"` + Path string `json:"path" yaml:"path"` + Values []string `json:"values,omitempty" yaml:"values,omitempty"` + Min fixedpoint.Value `json:"min,omitempty" yaml:"min,omitempty"` + Max fixedpoint.Value `json:"max,omitempty" yaml:"max,omitempty"` + Step fixedpoint.Value `json:"step,omitempty" yaml:"step,omitempty"` +} + +type Config struct { + Matrix []SelectorConfig `yaml:"matrix"` +} + +func LoadConfig(yamlConfigFileName string) (*Config, error) { + configYaml, err := ioutil.ReadFile(yamlConfigFileName) + if err != nil { + return nil, err + } + + var optConfig Config + if err := yaml.Unmarshal(configYaml, &optConfig); err != nil { + return nil, err + } + + return &optConfig, nil +}