2022-05-19 10:23:12 +00:00
|
|
|
package optimizer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-05-19 12:31:25 +00:00
|
|
|
"strings"
|
2022-05-19 10:23:12 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v3"
|
2022-05-19 12:31:25 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/backtest"
|
2022-05-19 10:23:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("component", "optimizer")
|
|
|
|
|
|
|
|
type Executor interface {
|
2022-05-19 12:31:25 +00:00
|
|
|
Execute(configJson []byte) (*backtest.SummaryReport, error)
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
type AsyncHandle struct {
|
|
|
|
Error error
|
|
|
|
Report *backtest.SummaryReport
|
|
|
|
Done chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-05-19 10:23:12 +00:00
|
|
|
type LocalProcessExecutor struct {
|
2022-05-19 12:31:25 +00:00
|
|
|
Bin string
|
|
|
|
WorkDir string
|
|
|
|
ConfigDir string
|
|
|
|
OutputDir string
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
func (e *LocalProcessExecutor) ExecuteAsync(configJson []byte) *AsyncHandle {
|
|
|
|
handle := &AsyncHandle{
|
|
|
|
Done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-06-20 03:54:55 +00:00
|
|
|
defer close(handle.Done)
|
2022-06-20 03:20:26 +00:00
|
|
|
report, err := e.Execute(configJson)
|
|
|
|
handle.Error = err
|
|
|
|
handle.Report = report
|
|
|
|
}()
|
|
|
|
|
|
|
|
return handle
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:54:55 +00:00
|
|
|
func (e *LocalProcessExecutor) readReport(output []byte) (*backtest.SummaryReport, error) {
|
|
|
|
summaryReportFilepath := strings.TrimSpace(string(output))
|
|
|
|
_, err := os.Stat(summaryReportFilepath)
|
|
|
|
if os.IsNotExist(err) {
|
2022-05-19 12:31:25 +00:00
|
|
|
return nil, err
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:54:55 +00:00
|
|
|
summaryReport, err := backtest.ReadSummaryReport(summaryReportFilepath)
|
2022-05-19 10:23:12 +00:00
|
|
|
if err != nil {
|
2022-05-19 12:31:25 +00:00
|
|
|
return nil, err
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:54:55 +00:00
|
|
|
return summaryReport, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LocalProcessExecutor) Execute(configJson []byte) (*backtest.SummaryReport, error) {
|
|
|
|
tf, err := jsonToYamlConfig(e.ConfigDir, configJson)
|
|
|
|
if err != nil {
|
2022-06-20 03:20:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:54:55 +00:00
|
|
|
c := exec.Command(e.Bin, "backtest", "--config", tf.Name(), "--output", e.OutputDir, "--subdir")
|
|
|
|
output, err := c.Output()
|
2022-05-19 10:23:12 +00:00
|
|
|
if err != nil {
|
2022-05-19 12:31:25 +00:00
|
|
|
return nil, err
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:54:55 +00:00
|
|
|
return e.readReport(output)
|
2022-06-20 03:20:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 06:52:40 +00:00
|
|
|
// jsonToYamlConfig translate json format config into a YAML format config file
|
|
|
|
// The generated file is a temp file
|
2022-06-20 03:20:26 +00:00
|
|
|
func jsonToYamlConfig(dir string, configJson []byte) (*os.File, error) {
|
|
|
|
var o map[string]interface{}
|
|
|
|
if err := json.Unmarshal(configJson, &o); err != nil {
|
2022-05-19 12:31:25 +00:00
|
|
|
return nil, err
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
yamlConfig, err := yaml.Marshal(o)
|
|
|
|
if err != nil {
|
2022-06-20 03:07:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
tf, err := os.CreateTemp(dir, "bbgo-*.yaml")
|
2022-05-19 12:31:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
if _, err = tf.Write(yamlConfig); err != nil {
|
2022-05-19 12:31:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
if err := tf.Close(); err != nil {
|
2022-05-19 12:31:25 +00:00
|
|
|
return nil, err
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:20:26 +00:00
|
|
|
return tf, nil
|
2022-05-19 10:23:12 +00:00
|
|
|
}
|