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:
- type: iterate
label: interval
path: '/exchangeStrategies/0/bollmaker/interval'
values: ["1m", "5m", "15m", "30m"]
- type: range
path: '/exchangeStrategies/0/bollmaker/amount'
label: amount
min: 20.0
max: 100.0
step: 20.0
- type: range
label: spread
path: '/exchangeStrategies/0/bollmaker/spread'
min: 0.1%
max: 0.2%

View File

@ -3,6 +3,7 @@ package cmd
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
@ -15,6 +16,7 @@ import (
func init() {
optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file")
optimizeCmd.Flags().String("output", "output", "backtest report output directory")
optimizeCmd.Flags().Bool("json", false, "print optimizer metrics in json format")
RootCmd.AddCommand(optimizeCmd)
}
@ -36,6 +38,11 @@ var optimizeCmd = &cobra.Command{
return err
}
printJsonFormat, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}
outputDirectory, err := cmd.Flags().GetString("output")
if err != nil {
return err
@ -83,6 +90,25 @@ var optimizeCmd = &cobra.Command{
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 string `json:"type" yaml:"type"`
Label string `json:"label,omitempty" yaml:"label,omitempty"`
Path string `json:"path" yaml:"path"`
Values []string `json:"values,omitempty" yaml:"values,omitempty"`
Min fixedpoint.Value `json:"min,omitempty" yaml:"min,omitempty"`

View File

@ -25,8 +25,6 @@ type GridOptimizer struct {
Config *Config
CurrentParams []interface{}
Metrics []Metric
}
func (o *GridOptimizer) buildOps() []OpFunc {
@ -112,9 +110,11 @@ func (o *GridOptimizer) buildOps() []OpFunc {
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))
var metrics []Metric
var ops = o.buildOps()
var app = func(configJson []byte, next func(configJson []byte) error) error {
summaryReport, err := executor.Execute(configJson)
@ -126,7 +126,7 @@ func (o *GridOptimizer) Run(executor Executor, configJson []byte) error {
metricValue := TotalProfitMetricValueFunc(summaryReport)
// 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,
Value: metricValue,
})
@ -151,12 +151,10 @@ func (o *GridOptimizer) Run(executor Executor, configJson []byte) error {
err := wrapper(configJson)
// sort metrics
sort.Slice(o.Metrics, func(i, j int) bool {
a := o.Metrics[i].Value
b := o.Metrics[j].Value
sort.Slice(metrics, func(i, j int) bool {
a := metrics[i].Value
b := metrics[j].Value
return a.Compare(b) > 0
})
log.Infof("metrics: %+v", o.Metrics)
return err
return metrics, err
}