mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
implement config yaml dummper
This commit is contained in:
parent
098a966813
commit
705edc38c0
|
@ -1,6 +1,7 @@
|
||||||
package bbgo
|
package bbgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -207,11 +208,51 @@ func (c *Config) Map() (map[string]interface{}, error) {
|
||||||
strategyID: params,
|
strategyID: params,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
data["exchangeStrategies"] = exchangeStrategies
|
|
||||||
|
if len(exchangeStrategies) > 0 {
|
||||||
|
data["exchangeStrategies"] = exchangeStrategies
|
||||||
|
}
|
||||||
|
|
||||||
|
var crossExchangeStrategies []map[string]interface{}
|
||||||
|
for _, st := range c.CrossExchangeStrategies {
|
||||||
|
strategyID := st.ID()
|
||||||
|
|
||||||
|
var params Stash
|
||||||
|
|
||||||
|
out, err := json.Marshal(st)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(out, ¶ms); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
crossExchangeStrategies = append(crossExchangeStrategies, map[string]interface{}{
|
||||||
|
strategyID: params,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(crossExchangeStrategies) > 0 {
|
||||||
|
data["crossExchangeStrategies"] = crossExchangeStrategies
|
||||||
|
}
|
||||||
|
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) YAML() ([]byte, error) {
|
||||||
|
m, err := c.Map()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var enc = yaml.NewEncoder(&buf)
|
||||||
|
enc.SetIndent(2)
|
||||||
|
err = enc.Encode(m)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
}
|
||||||
|
|
||||||
type Stash map[string]interface{}
|
type Stash map[string]interface{}
|
||||||
|
|
||||||
func loadStash(config []byte) (Stash, error) {
|
func loadStash(config []byte) (Stash, error) {
|
||||||
|
@ -367,10 +408,26 @@ func loadExchangeStrategies(config *Config, stash Stash) (err error) {
|
||||||
|
|
||||||
var mounts []string
|
var mounts []string
|
||||||
if val, ok := configStash["on"]; ok {
|
if val, ok := configStash["on"]; ok {
|
||||||
if values, ok := val.([]string); ok {
|
switch tv := val.(type) {
|
||||||
mounts = append(mounts, values...)
|
|
||||||
} else if str, ok := val.(string); ok {
|
case []string:
|
||||||
mounts = append(mounts, str)
|
mounts = append(mounts, tv...)
|
||||||
|
|
||||||
|
case string:
|
||||||
|
mounts = append(mounts, tv)
|
||||||
|
|
||||||
|
case []interface{}:
|
||||||
|
for _, f := range tv {
|
||||||
|
s, ok := f.(string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%+v (%T) is not a string", f, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
mounts = append(mounts, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unexpected mount type: %T value: %+v", val, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,11 @@ package bbgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -62,16 +64,16 @@ func TestLoadConfig(t *testing.T) {
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
f: func(t *testing.T, config *Config) {
|
f: func(t *testing.T, config *Config) {
|
||||||
assert.Len(t, config.ExchangeStrategies, 1)
|
assert.Len(t, config.ExchangeStrategies, 1)
|
||||||
assert.Equal(t, config.ExchangeStrategies, []ExchangeStrategyMount{{
|
assert.Equal(t, []ExchangeStrategyMount{{
|
||||||
Mounts: []string{"binance"},
|
Mounts: []string{"binance"},
|
||||||
Strategy: &TestStrategy{
|
Strategy: &TestStrategy{
|
||||||
Symbol: "BTCUSDT",
|
Symbol: "BTCUSDT",
|
||||||
Interval: "1m",
|
Interval: "1m",
|
||||||
BaseQuantity: 0.1,
|
BaseQuantity: 0.1,
|
||||||
MaxAssetQuantity: 0,
|
MaxAssetQuantity: 1.1,
|
||||||
MinDropPercentage: -0.05,
|
MinDropPercentage: -0.05,
|
||||||
},
|
},
|
||||||
}})
|
}}, config.ExchangeStrategies)
|
||||||
|
|
||||||
m, err := config.Map()
|
m, err := config.Map()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@ -108,12 +110,30 @@ func TestLoadConfig(t *testing.T) {
|
||||||
"symbol": "BTCUSDT",
|
"symbol": "BTCUSDT",
|
||||||
"baseQuantity": 0.1,
|
"baseQuantity": 0.1,
|
||||||
"interval": "1m",
|
"interval": "1m",
|
||||||
"maxAssetQuantity": 0.0,
|
"maxAssetQuantity": 1.1,
|
||||||
"minDropPercentage": -0.05,
|
"minDropPercentage": -0.05,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, m)
|
}, m)
|
||||||
|
|
||||||
|
yamlText, err := config.YAML()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
yamlTextSource, err := ioutil.ReadFile("testdata/strategy.yaml")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var sourceMap map[string]interface{}
|
||||||
|
err = yaml.Unmarshal(yamlTextSource, &sourceMap)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
delete(sourceMap, "build")
|
||||||
|
|
||||||
|
var actualMap map[string]interface{}
|
||||||
|
err = yaml.Unmarshal(yamlText, &actualMap)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
delete(actualMap, "build")
|
||||||
|
|
||||||
|
assert.Equal(t, sourceMap, actualMap)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
5
pkg/bbgo/testdata/strategy.yaml
vendored
5
pkg/bbgo/testdata/strategy.yaml
vendored
|
@ -3,16 +3,15 @@ sessions:
|
||||||
max:
|
max:
|
||||||
exchange: max
|
exchange: max
|
||||||
envVarPrefix: MAX_
|
envVarPrefix: MAX_
|
||||||
|
|
||||||
binance:
|
binance:
|
||||||
exchange: binance
|
exchange: binance
|
||||||
envVarPrefix: BINANCE_
|
envVarPrefix: BINANCE_
|
||||||
|
|
||||||
exchangeStrategies:
|
exchangeStrategies:
|
||||||
- on: binance
|
- on: ["binance"]
|
||||||
test:
|
test:
|
||||||
symbol: "BTCUSDT"
|
symbol: "BTCUSDT"
|
||||||
interval: "1m"
|
interval: "1m"
|
||||||
baseQuantity: 0.1
|
baseQuantity: 0.1
|
||||||
minDropPercentage: -0.05
|
minDropPercentage: -0.05
|
||||||
|
maxAssetQuantity: 1.1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user