Merge pull request #627 from c9s/feature/optimizer

feature: add grid optimizer
This commit is contained in:
Yo-An Lin 2022-05-19 20:51:47 +08:00 committed by GitHub
commit bdf31f0044
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 468 additions and 21 deletions

View File

@ -0,0 +1,21 @@
# usage:
#
# go run ./cmd/bbgo optimize --config bollmaker_ethusdt.yaml --optimizer-config optimizer.yaml --debug
#
---
matrix:
- type: iterate
path: '/exchangeStrategies/0/bollmaker/interval'
values: ["1m", "5m", "15m", "30m"]
- type: range
path: '/exchangeStrategies/0/bollmaker/amount'
min: 20.0
max: 100.0
step: 20.0
- type: range
path: '/exchangeStrategies/0/bollmaker/spread'
min: 0.1%
max: 0.2%
step: 0.01%

21
config/optimizer.yaml Normal file
View File

@ -0,0 +1,21 @@
# usage:
#
# go run ./cmd/bbgo optimize --config bollmaker_ethusdt.yaml --optimizer-config optimizer.yaml --debug
#
---
matrix:
- type: iterate
path: '/exchangeStrategies/0/bollmaker/interval'
values: ["1m", "5m"]
- type: range
path: '/exchangeStrategies/0/bollmaker/amount'
min: 20.0
max: 40.0
step: 20.0
- type: range
path: '/exchangeStrategies/0/bollmaker/spread'
min: 0.1%
max: 0.2%
step: 0.02%

1
go.mod
View File

@ -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

3
go.sum
View File

@ -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=

View File

@ -12,6 +12,25 @@ type ManifestEntry struct {
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) {
var arr []ManifestEntry
for k, v := range m {

View File

@ -39,11 +39,26 @@ type SummaryReport struct {
InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"`
FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"`
// TotalProfit is the profit aggregated from the symbol reports
TotalProfit fixedpoint.Value `json:"totalProfit,omitempty"`
TotalUnrealizedProfit fixedpoint.Value `json:"totalUnrealizedProfit,omitempty"`
SymbolReports []SessionSymbolReport `json:"symbolReports,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
// trades are merged, collected and re-calculated
type SessionSymbolReport struct {

View File

@ -452,13 +452,6 @@ var BacktestCmd = &cobra.Command{
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{
StartTime: startTime,
EndTime: endTime,
@ -492,6 +485,8 @@ var BacktestCmd = &cobra.Command{
summaryReport.Symbols = append(summaryReport.Symbols, symbol)
summaryReport.SymbolReports = append(summaryReport.SymbolReports, *symbolReport)
summaryReport.TotalProfit = symbolReport.PnL.Profit
summaryReport.TotalUnrealizedProfit = symbolReport.PnL.UnrealizedProfit
// write report to a file
if generatingReport {
@ -500,24 +495,39 @@ var BacktestCmd = &cobra.Command{
return err
}
}
symbolReport.Print(wantBaseAssetBaseline)
}
}
if generatingReport && reportFileInSubDir {
if generatingReport {
summaryReportFile := filepath.Join(reportDir, "summary.json")
// output summary report filepath to stdout, so that our optimizer can read from it
fmt.Println(summaryReportFile)
if err := util.WriteJsonFile(summaryReportFile, summaryReport); err != nil {
return err
}
// append report index
if err := backtest.AddReportIndexRun(outputDirectory, backtest.Run{
ID: runID,
Config: userConfig,
Time: time.Now(),
}); err != nil {
return err
if reportFileInSubDir {
if err := backtest.AddReportIndexRun(outputDirectory, backtest.Run{
ID: runID,
Config: userConfig,
Time: time.Now(),
}); err != nil {
return err
}
}
} 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 {
symbolReport.Print(wantBaseAssetBaseline)
}
}

88
pkg/cmd/optimize.go Normal file
View File

@ -0,0 +1,88 @@
package cmd
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/optimizer"
)
func init() {
optimizeCmd.Flags().String("optimizer-config", "optimizer.yaml", "config file")
optimizeCmd.Flags().String("output", "output", "backtest report output directory")
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
}
outputDirectory, err := cmd.Flags().GetString("output")
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
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_ = ctx
configDir, err := os.MkdirTemp("", "bbgo-config-*")
if err != nil {
return err
}
executor := &optimizer.LocalProcessExecutor{
Bin: os.Args[0],
WorkDir: ".",
ConfigDir: configDir,
OutputDir: outputDirectory,
}
optz := &optimizer.GridOptimizer{
Config: optConfig,
}
return optz.Run(executor, configJson)
},
}

36
pkg/optimizer/config.go Normal file
View File

@ -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
}

162
pkg/optimizer/grid.go Normal file
View File

@ -0,0 +1,162 @@
package optimizer
import (
"fmt"
"sort"
"github.com/evanphx/json-patch/v5"
"github.com/c9s/bbgo/pkg/backtest"
"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{} `json:"params,omitempty"`
Value fixedpoint.Value `json:"value,omitempty"`
}
type GridOptimizer struct {
Config *Config
CurrentParams []interface{}
Metrics []Metric
}
func (o *GridOptimizer) buildOps() []OpFunc {
var ops []OpFunc
o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
for i, selector := range o.Config.Matrix {
var path = selector.Path
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
}
var values []fixedpoint.Value
for val := min; val.Compare(max) <= 0; val = val.Add(step) {
values = append(values, val)
}
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)
patchedJson, err := patch.ApplyIndent(configJson, " ")
if err != nil {
return err
}
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 {
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)
patchedJson, err := patch.ApplyIndent(configJson, " ")
if err != nil {
return err
}
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 {
o.CurrentParams = make([]interface{}, len(o.Config.Matrix))
var ops = o.buildOps()
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)
// TODO: replace this with a local variable and return it as a function result
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
wrapper = func(configJson []byte) error {
return cur(configJson, inner)
}
}
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
}

66
pkg/optimizer/local.go Normal file
View File

@ -0,0 +1,66 @@
package optimizer
import (
"encoding/json"
"os"
"os/exec"
"strings"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/backtest"
)
var log = logrus.WithField("component", "optimizer")
type Executor interface {
Execute(configJson []byte) (*backtest.SummaryReport, error)
}
type LocalProcessExecutor struct {
Bin string
WorkDir string
ConfigDir string
OutputDir string
}
func (e *LocalProcessExecutor) Execute(configJson []byte) (*backtest.SummaryReport, error) {
var o map[string]interface{}
if err := json.Unmarshal(configJson, &o); err != nil {
return nil, err
}
yamlConfig, err := yaml.Marshal(o)
if err != nil {
return nil, err
}
tf, err := os.CreateTemp(e.ConfigDir, "bbgo-*.yaml")
if err != nil {
return nil, err
}
if _, err = tf.Write(yamlConfig); err != nil {
return nil, err
}
c := exec.Command(e.Bin, "backtest", "--config", tf.Name(), "--output", e.OutputDir, "--subdir")
output, err := c.Output()
if err != nil {
return nil, err
}
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
}

View File

@ -0,0 +1,3 @@
package optimizer
type OpFunc func(configJson []byte, next func(configJson []byte) error) error

View File

@ -6,6 +6,8 @@ import (
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
@ -60,7 +62,6 @@ func (b Balance) String() (o string) {
return o
}
type BalanceSnapshot struct {
Balances BalanceMap `json:"balances"`
Session string `json:"session"`
@ -191,15 +192,16 @@ func (m BalanceMap) Print() {
continue
}
fmt.Printf(" %s: %v", balance.Currency, balance.Available)
o := fmt.Sprintf(" %s: %v", balance.Currency, balance.Available)
if balance.Locked.Sign() > 0 {
fmt.Printf(" (locked %v)", balance.Locked)
o += fmt.Sprintf(" (locked %v)", balance.Locked)
}
if balance.Borrowed.Sign() > 0 {
fmt.Printf(" (borrowed %v)", balance.Borrowed)
o += fmt.Sprintf(" (borrowed %v)", balance.Borrowed)
}
fmt.Println()
log.Infoln(o)
}
}