refine optimizer executor config structure

This commit is contained in:
c9s 2022-06-21 12:31:42 +08:00
parent 16682596df
commit 9b82de596b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 39 additions and 7 deletions

View File

@ -3,12 +3,16 @@
# go run ./cmd/bbgo optimize --config bollmaker_ethusdt.yaml --optimizer-config optimizer.yaml --debug
#
---
maxThread: 10
executor:
type: local
local:
maxNumberOfProcesses: 10
matrix:
- type: iterate
label: interval
path: '/exchangeStrategies/0/bollmaker/interval'
values: ["1m", "5m", "15m", "30m"]
values: [ "1m", "5m", "15m", "30m" ]
- type: range
path: '/exchangeStrategies/0/bollmaker/amount'

View File

@ -80,6 +80,7 @@ var optimizeCmd = &cobra.Command{
}
executor := &optimizer.LocalProcessExecutor{
Config: optConfig.Executor.LocalExecutorConfig,
Bin: os.Args[0],
WorkDir: ".",
ConfigDir: configDir,

View File

@ -18,11 +18,30 @@ type SelectorConfig struct {
Step fixedpoint.Value `json:"step,omitempty" yaml:"step,omitempty"`
}
type LocalExecutorConfig struct {
MaxNumberOfProcesses int `json:"maxNumberOfProcesses" yaml:"maxNumberOfProcesses"`
}
type ExecutorConfig struct {
Type string `json:"type" yaml:"type"`
LocalExecutorConfig *LocalExecutorConfig `json:"local" yaml:"local"`
}
type Config struct {
Executor *ExecutorConfig `json:"executor" yaml:"executor"`
MaxThread int `yaml:"maxThread,omitempty"`
Matrix []SelectorConfig `yaml:"matrix"`
}
var defaultExecutorConfig = &ExecutorConfig{
Type: "local",
LocalExecutorConfig: defaultLocalExecutorConfig,
}
var defaultLocalExecutorConfig = &LocalExecutorConfig{
MaxNumberOfProcesses: 10,
}
func LoadConfig(yamlConfigFileName string) (*Config, error) {
configYaml, err := ioutil.ReadFile(yamlConfigFileName)
if err != nil {
@ -34,9 +53,16 @@ func LoadConfig(yamlConfigFileName string) (*Config, error) {
return nil, err
}
// The default of MaxThread is 5
if optConfig.MaxThread <= 0 {
optConfig.MaxThread = 5
if optConfig.Executor == nil {
optConfig.Executor = defaultExecutorConfig
}
if optConfig.Executor.Type == "" {
optConfig.Executor.Type = "local"
}
if optConfig.Executor.Type == "local" && optConfig.Executor.LocalExecutorConfig == nil {
optConfig.Executor.LocalExecutorConfig = defaultLocalExecutorConfig
}
return &optConfig, nil

View File

@ -199,7 +199,7 @@ func (o *GridOptimizer) Run(executor Executor, configJson []byte) (map[string][]
}
}
ctx := context.WithValue(context.Background(), "MaxThread", o.Config.MaxThread)
ctx := context.Background()
resultsC, err := executor.Run(ctx, taskC)
if err != nil {
return nil, err

View File

@ -36,6 +36,7 @@ type AsyncHandle struct {
}
type LocalProcessExecutor struct {
Config *LocalExecutorConfig
Bin string
WorkDir string
ConfigDir string
@ -73,7 +74,7 @@ func (e *LocalProcessExecutor) readReport(output []byte) (*backtest.SummaryRepor
}
func (e *LocalProcessExecutor) Run(ctx context.Context, taskC chan BacktestTask) (chan BacktestTask, error) {
var maxNumOfProcess = ctx.Value("MaxThread").(int)
var maxNumOfProcess = e.Config.MaxNumberOfProcesses
var resultsC = make(chan BacktestTask, maxNumOfProcess*2)
wg := sync.WaitGroup{}