mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-27 17:25:16 +00:00
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
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
|
|
}
|