return metrics as a optimizer result

This commit is contained in:
c9s 2022-05-20 01:42:32 +08:00
parent 5c92bc5d66
commit 95c9fe4502
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 39 additions and 11 deletions

View File

@ -5,16 +5,19 @@
--- ---
matrix: matrix:
- type: iterate - type: iterate
label: interval
path: '/exchangeStrategies/0/bollmaker/interval' path: '/exchangeStrategies/0/bollmaker/interval'
values: ["1m", "5m", "15m", "30m"] values: ["1m", "5m", "15m", "30m"]
- type: range - type: range
path: '/exchangeStrategies/0/bollmaker/amount' path: '/exchangeStrategies/0/bollmaker/amount'
label: amount
min: 20.0 min: 20.0
max: 100.0 max: 100.0
step: 20.0 step: 20.0
- type: range - type: range
label: spread
path: '/exchangeStrategies/0/bollmaker/spread' path: '/exchangeStrategies/0/bollmaker/spread'
min: 0.1% min: 0.1%
max: 0.2% max: 0.2%

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -15,6 +16,7 @@ import (
func init() { func init() {
optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file") optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file")
optimizeCmd.Flags().String("output", "output", "backtest report output directory") optimizeCmd.Flags().String("output", "output", "backtest report output directory")
optimizeCmd.Flags().Bool("json", false, "print optimizer metrics in json format")
RootCmd.AddCommand(optimizeCmd) RootCmd.AddCommand(optimizeCmd)
} }
@ -36,6 +38,11 @@ var optimizeCmd = &cobra.Command{
return err return err
} }
printJsonFormat, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}
outputDirectory, err := cmd.Flags().GetString("output") outputDirectory, err := cmd.Flags().GetString("output")
if err != nil { if err != nil {
return err return err
@ -83,6 +90,25 @@ var optimizeCmd = &cobra.Command{
Config: optConfig, Config: optConfig,
} }
return optz.Run(executor, configJson) 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 {
for _, m := range metrics {
fmt.Printf("%v => %v\n", m.Params, m.Value)
}
}
return nil
}, },
} }

View File

@ -10,6 +10,7 @@ import (
type SelectorConfig struct { type SelectorConfig struct {
Type string `json:"type" yaml:"type"` Type string `json:"type" yaml:"type"`
Label string `json:"label,omitempty" yaml:"label,omitempty"`
Path string `json:"path" yaml:"path"` Path string `json:"path" yaml:"path"`
Values []string `json:"values,omitempty" yaml:"values,omitempty"` Values []string `json:"values,omitempty" yaml:"values,omitempty"`
Min fixedpoint.Value `json:"min,omitempty" yaml:"min,omitempty"` Min fixedpoint.Value `json:"min,omitempty" yaml:"min,omitempty"`

View File

@ -25,8 +25,6 @@ type GridOptimizer struct {
Config *Config Config *Config
CurrentParams []interface{} CurrentParams []interface{}
Metrics []Metric
} }
func (o *GridOptimizer) buildOps() []OpFunc { func (o *GridOptimizer) buildOps() []OpFunc {
@ -112,9 +110,11 @@ func (o *GridOptimizer) buildOps() []OpFunc {
return ops return ops
} }
func (o *GridOptimizer) Run(executor Executor, configJson []byte) error { func (o *GridOptimizer) Run(executor Executor, configJson []byte) ([]Metric, error) {
o.CurrentParams = make([]interface{}, len(o.Config.Matrix)) o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
var metrics []Metric
var ops = o.buildOps() var ops = o.buildOps()
var app = func(configJson []byte, next func(configJson []byte) error) error { var app = func(configJson []byte, next func(configJson []byte) error) error {
summaryReport, err := executor.Execute(configJson) summaryReport, err := executor.Execute(configJson)
@ -126,7 +126,7 @@ func (o *GridOptimizer) Run(executor Executor, configJson []byte) error {
metricValue := TotalProfitMetricValueFunc(summaryReport) metricValue := TotalProfitMetricValueFunc(summaryReport)
// TODO: replace this with a local variable and return it as a function result // TODO: replace this with a local variable and return it as a function result
o.Metrics = append(o.Metrics, Metric{ metrics = append(metrics, Metric{
Params: o.CurrentParams, Params: o.CurrentParams,
Value: metricValue, Value: metricValue,
}) })
@ -151,12 +151,10 @@ func (o *GridOptimizer) Run(executor Executor, configJson []byte) error {
err := wrapper(configJson) err := wrapper(configJson)
// sort metrics sort.Slice(metrics, func(i, j int) bool {
sort.Slice(o.Metrics, func(i, j int) bool { a := metrics[i].Value
a := o.Metrics[i].Value b := metrics[j].Value
b := o.Metrics[j].Value
return a.Compare(b) > 0 return a.Compare(b) > 0
}) })
log.Infof("metrics: %+v", o.Metrics) return metrics, err
return err
} }