optimizer: fix op builder

This commit is contained in:
c9s 2022-05-19 20:31:25 +08:00
parent 960f967c34
commit b3da6caddb
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
6 changed files with 132 additions and 49 deletions

View File

@ -12,10 +12,10 @@ matrix:
path: '/exchangeStrategies/0/bollmaker/amount' path: '/exchangeStrategies/0/bollmaker/amount'
min: 20.0 min: 20.0
max: 100.0 max: 100.0
step: 10.0 step: 20.0
- type: range - type: range
path: '/exchangeStrategies/0/bollmaker/spread' path: '/exchangeStrategies/0/bollmaker/spread'
min: 0.1% min: 0.1%
max: 0.2% max: 0.2%
step: 0.01% step: 0.02%

View File

@ -12,6 +12,25 @@ type ManifestEntry struct {
type Manifests map[InstancePropertyIndex]string type Manifests map[InstancePropertyIndex]string
func (m *Manifests) UnmarshalJSON(j []byte) error {
var entries []ManifestEntry
if err := json.Unmarshal(j, &entries); err != nil {
return err
}
mm := make(Manifests)
for _, entry := range entries {
index := InstancePropertyIndex{
ID: entry.StrategyID,
InstanceID: entry.StrategyInstance,
Property: entry.StrategyProperty,
}
mm[index] = entry.Filename
}
*m = mm
return nil
}
func (m Manifests) MarshalJSON() ([]byte, error) { func (m Manifests) MarshalJSON() ([]byte, error) {
var arr []ManifestEntry var arr []ManifestEntry
for k, v := range m { for k, v := range m {

View File

@ -48,6 +48,17 @@ type SummaryReport struct {
Manifests Manifests `json:"manifests,omitempty"` Manifests Manifests `json:"manifests,omitempty"`
} }
func ReadSummaryReport(filename string) (*SummaryReport, error) {
o, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var report SummaryReport
err = json.Unmarshal(o, &report)
return &report, err
}
// SessionSymbolReport is the report per exchange session // SessionSymbolReport is the report per exchange session
// trades are merged, collected and re-calculated // trades are merged, collected and re-calculated
type SessionSymbolReport struct { type SessionSymbolReport struct {

View File

@ -452,13 +452,6 @@ var BacktestCmd = &cobra.Command{
finalTotalBalances = finalTotalBalances.Add(finalBalances) finalTotalBalances = finalTotalBalances.Add(finalBalances)
} }
color.Green("BACK-TEST REPORT")
color.Green("===============================================\n")
color.Green("START TIME: %s\n", startTime.Format(time.RFC1123))
color.Green("END TIME: %s\n", endTime.Format(time.RFC1123))
color.Green("INITIAL TOTAL BALANCE: %v\n", initTotalBalances)
color.Green("FINAL TOTAL BALANCE: %v\n", finalTotalBalances)
summaryReport := &backtest.SummaryReport{ summaryReport := &backtest.SummaryReport{
StartTime: startTime, StartTime: startTime,
EndTime: endTime, EndTime: endTime,
@ -526,6 +519,13 @@ var BacktestCmd = &cobra.Command{
} }
} }
} else { } else {
color.Green("BACK-TEST REPORT")
color.Green("===============================================\n")
color.Green("START TIME: %s\n", startTime.Format(time.RFC1123))
color.Green("END TIME: %s\n", endTime.Format(time.RFC1123))
color.Green("INITIAL TOTAL BALANCE: %v\n", initTotalBalances)
color.Green("FINAL TOTAL BALANCE: %v\n", finalTotalBalances)
for _, symbolReport := range summaryReport.SymbolReports { for _, symbolReport := range summaryReport.SymbolReports {
symbolReport.Print(wantBaseAssetBaseline) symbolReport.Print(wantBaseAssetBaseline)
} }

View File

@ -5,17 +5,37 @@ import (
"github.com/evanphx/json-patch/v5" "github.com/evanphx/json-patch/v5"
"github.com/c9s/bbgo/pkg/backtest"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
type MetricValueFunc func(summaryReport *backtest.SummaryReport) fixedpoint.Value
var TotalProfitMetricValueFunc = func(summaryReport *backtest.SummaryReport) fixedpoint.Value {
return summaryReport.TotalProfit
}
type Metric struct {
Params []interface{}
Value fixedpoint.Value
}
type GridOptimizer struct { type GridOptimizer struct {
Config *Config Config *Config
CurrentParams []interface{}
Metrics []Metric
} }
func (o *GridOptimizer) buildOps() []OpFunc { func (o *GridOptimizer) buildOps() []OpFunc {
var ops []OpFunc var ops []OpFunc
for _, selector := range o.Config.Matrix {
o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
for i, selector := range o.Config.Matrix {
var path = selector.Path var path = selector.Path
var ii = i // copy variable because we need to use them in the closure
switch selector.Type { switch selector.Type {
case "range": case "range":
@ -26,13 +46,12 @@ func (o *GridOptimizer) buildOps() []OpFunc {
step = fixedpoint.One step = fixedpoint.One
} }
f := func(configJson []byte, next func(configJson []byte) error) error { var values []fixedpoint.Value
var values []fixedpoint.Value for val := min; val.Compare(max) <= 0; val = val.Add(step) {
for val := min; val.Compare(max) < 0; val = val.Add(step) { values = append(values, val)
values = append(values, val) }
}
log.Debugf("ranged values: %v", values) f := func(configJson []byte, next func(configJson []byte) error) error {
for _, val := range values { for _, val := range values {
jsonOp := []byte(fmt.Sprintf(`[ {"op": "replace", "path": "%s", "value": %v } ]`, path, val)) jsonOp := []byte(fmt.Sprintf(`[ {"op": "replace", "path": "%s", "value": %v } ]`, path, val))
patch, err := jsonpatch.DecodePatch(jsonOp) patch, err := jsonpatch.DecodePatch(jsonOp)
@ -42,12 +61,14 @@ func (o *GridOptimizer) buildOps() []OpFunc {
log.Debugf("json op: %s", jsonOp) log.Debugf("json op: %s", jsonOp)
configJson, err := patch.ApplyIndent(configJson, " ") patchedJson, err := patch.ApplyIndent(configJson, " ")
if err != nil { if err != nil {
return err return err
} }
if err := next(configJson); err != nil { valCopy := val
o.CurrentParams[ii] = valCopy
if err := next(patchedJson); err != nil {
return err return err
} }
} }
@ -59,8 +80,9 @@ func (o *GridOptimizer) buildOps() []OpFunc {
case "iterate": case "iterate":
values := selector.Values values := selector.Values
f := func(configJson []byte, next func(configJson []byte) error) error { f := func(configJson []byte, next func(configJson []byte) error) error {
log.Debugf("iterate values: %v", values)
for _, val := range values { for _, val := range values {
log.Debugf("%d %s: %v of %v", ii, path, val, values)
jsonOp := []byte(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": "%s"}]`, path, val)) jsonOp := []byte(fmt.Sprintf(`[{"op": "replace", "path": "%s", "value": "%s"}]`, path, val))
patch, err := jsonpatch.DecodePatch(jsonOp) patch, err := jsonpatch.DecodePatch(jsonOp)
if err != nil { if err != nil {
@ -69,12 +91,14 @@ func (o *GridOptimizer) buildOps() []OpFunc {
log.Debugf("json op: %s", jsonOp) log.Debugf("json op: %s", jsonOp)
configJson, err := patch.ApplyIndent(configJson, " ") patchedJson, err := patch.ApplyIndent(configJson, " ")
if err != nil { if err != nil {
return err return err
} }
if err := next(configJson); err != nil { valCopy := val
o.CurrentParams[ii] = valCopy
if err := next(patchedJson); err != nil {
return err return err
} }
} }
@ -88,21 +112,38 @@ func (o *GridOptimizer) buildOps() []OpFunc {
} }
func (o *GridOptimizer) Run(executor Executor, configJson []byte) error { func (o *GridOptimizer) Run(executor Executor, configJson []byte) error {
var ops = o.buildOps() o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
var last = func(configJson []byte, next func(configJson []byte) error) error {
return executor.Execute(configJson)
}
ops = append(ops, last)
var wrapper = func(configJson []byte) error { return nil } var ops = o.buildOps()
for i := len(ops) - 1; i > 0; i-- { var app = func(configJson []byte, next func(configJson []byte) error) error {
next := ops[i] summaryReport, err := executor.Execute(configJson)
cur := ops[i-1] if err != nil {
return err
}
// TODO: Add other metric value function
metricValue := TotalProfitMetricValueFunc(summaryReport)
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)
}
for i := len(ops) - 1; i >= 0; i-- {
cur := ops[i]
inner := wrapper inner := wrapper
wrapper = func(configJson []byte) error { wrapper = func(configJson []byte) error {
return cur(configJson, func(configJson []byte) error { return cur(configJson, inner)
return next(configJson, inner)
})
} }
} }

View File

@ -4,51 +4,63 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"os/exec" "os/exec"
"strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/backtest"
) )
var log = logrus.WithField("component", "optimizer") var log = logrus.WithField("component", "optimizer")
type Executor interface { type Executor interface {
Execute(configJson []byte) error Execute(configJson []byte) (*backtest.SummaryReport, error)
} }
type LocalProcessExecutor struct { type LocalProcessExecutor struct {
Bin string Bin string
WorkDir string WorkDir string
ConfigDir string ConfigDir string
OutputDir string OutputDir string
CombineOutput bool
} }
func (e *LocalProcessExecutor) Execute(configJson []byte) error { func (e *LocalProcessExecutor) Execute(configJson []byte) (*backtest.SummaryReport, error) {
var o map[string]interface{} var o map[string]interface{}
if err := json.Unmarshal(configJson, &o); err != nil { if err := json.Unmarshal(configJson, &o); err != nil {
return err return nil, err
} }
yamlConfig, err := yaml.Marshal(o) yamlConfig, err := yaml.Marshal(o)
if err != nil { if err != nil {
return err return nil, err
} }
tf, err := os.CreateTemp(e.ConfigDir, "bbgo-*.yaml") tf, err := os.CreateTemp(e.ConfigDir, "bbgo-*.yaml")
if err != nil { if err != nil {
return err return nil, err
} }
if _, err = tf.Write(yamlConfig); err != nil { if _, err = tf.Write(yamlConfig); err != nil {
return err return nil, err
} }
c := exec.Command(e.Bin, "backtest", "--config", tf.Name(), "--output", e.OutputDir, "--subdir") c := exec.Command(e.Bin, "backtest", "--config", tf.Name(), "--output", e.OutputDir, "--subdir")
// c.Output() output, err := c.Output()
if e.CombineOutput { if err != nil {
c.Stdout = os.Stdout return nil, err
c.Stderr = os.Stderr
} }
return c.Run() summaryReportFilepath := strings.TrimSpace(string(output))
_, err = os.Stat(summaryReportFilepath)
if os.IsNotExist(err) {
return nil, err
}
summaryReport, err := backtest.ReadSummaryReport(summaryReportFilepath)
if err != nil {
return nil, err
}
return summaryReport, nil
} }