bbgo_origin/pkg/optimizer/grid.go

163 lines
3.7 KiB
Go
Raw Normal View History

package optimizer
import (
"fmt"
2022-05-19 12:36:56 +00:00
"sort"
"github.com/evanphx/json-patch/v5"
2022-05-19 12:31:25 +00:00
"github.com/c9s/bbgo/pkg/backtest"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
2022-05-19 12:31:25 +00:00
type MetricValueFunc func(summaryReport *backtest.SummaryReport) fixedpoint.Value
var TotalProfitMetricValueFunc = func(summaryReport *backtest.SummaryReport) fixedpoint.Value {
return summaryReport.TotalProfit
}
type Metric struct {
2022-05-19 12:36:56 +00:00
Params []interface{} `json:"params,omitempty"`
Value fixedpoint.Value `json:"value,omitempty"`
2022-05-19 12:31:25 +00:00
}
type GridOptimizer struct {
Config *Config
2022-05-19 12:31:25 +00:00
CurrentParams []interface{}
Metrics []Metric
}
func (o *GridOptimizer) buildOps() []OpFunc {
var ops []OpFunc
2022-05-19 12:31:25 +00:00
o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
for i, selector := range o.Config.Matrix {
var path = selector.Path
2022-05-19 12:31:25 +00:00
var ii = i // copy variable because we need to use them in the closure
switch selector.Type {
case "range":
min := selector.Min
max := selector.Max
step := selector.Step
if step.IsZero() {
step = fixedpoint.One
}
2022-05-19 12:31:25 +00:00
var values []fixedpoint.Value
for val := min; val.Compare(max) <= 0; val = val.Add(step) {
values = append(values, val)
}
2022-05-19 12:31:25 +00:00
f := func(configJson []byte, next func(configJson []byte) error) error {
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)
2022-05-19 12:31:25 +00:00
patchedJson, err := patch.ApplyIndent(configJson, " ")
if err != nil {
return err
}
2022-05-19 12:31:25 +00:00
valCopy := val
o.CurrentParams[ii] = valCopy
if err := next(patchedJson); 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 {
for _, val := range values {
2022-05-19 12:31:25 +00:00
log.Debugf("%d %s: %v of %v", ii, path, val, 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)
2022-05-19 12:31:25 +00:00
patchedJson, err := patch.ApplyIndent(configJson, " ")
if err != nil {
return err
}
2022-05-19 12:31:25 +00:00
valCopy := val
o.CurrentParams[ii] = valCopy
if err := next(patchedJson); err != nil {
return err
}
}
return nil
}
ops = append(ops, f)
}
}
return ops
}
func (o *GridOptimizer) Run(executor Executor, configJson []byte) error {
2022-05-19 12:31:25 +00:00
o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
var ops = o.buildOps()
2022-05-19 12:31:25 +00:00
var app = func(configJson []byte, next func(configJson []byte) error) error {
summaryReport, err := executor.Execute(configJson)
if err != nil {
return err
}
// TODO: Add other metric value function
metricValue := TotalProfitMetricValueFunc(summaryReport)
2022-05-19 12:36:56 +00:00
// TODO: replace this with a local variable and return it as a function result
2022-05-19 12:31:25 +00:00
o.Metrics = append(o.Metrics, Metric{
Params: o.CurrentParams,
Value: metricValue,
})
log.Infof("current params: %+v => %+v", o.CurrentParams, metricValue)
return nil
}
log.Debugf("build %d ops", len(ops))
var wrapper = func(configJson []byte) error {
return app(configJson, nil)
}
2022-05-19 12:31:25 +00:00
for i := len(ops) - 1; i >= 0; i-- {
cur := ops[i]
inner := wrapper
wrapper = func(configJson []byte) error {
2022-05-19 12:31:25 +00:00
return cur(configJson, inner)
}
}
2022-05-19 12:36:56 +00:00
err := wrapper(configJson)
// sort metrics
sort.Slice(o.Metrics, func(i, j int) bool {
a := o.Metrics[i].Value
b := o.Metrics[j].Value
return a.Compare(b) > 0
})
log.Infof("metrics: %+v", o.Metrics)
return err
}