bbgo_origin/pkg/bbgo/config_test.go

219 lines
5.6 KiB
Go
Raw Permalink Normal View History

2020-10-26 13:45:02 +00:00
package bbgo
2020-10-20 05:52:25 +00:00
import (
2020-10-26 13:45:02 +00:00
"context"
2021-02-03 01:58:31 +00:00
"io/ioutil"
2020-10-20 05:52:25 +00:00
"testing"
"github.com/stretchr/testify/assert"
2021-02-03 01:58:31 +00:00
"gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/fixedpoint"
2020-10-20 05:52:25 +00:00
)
2020-10-26 13:45:02 +00:00
func init() {
2020-10-28 23:54:59 +00:00
RegisterStrategy("test", &TestStrategy{})
2020-10-26 13:45:02 +00:00
}
type TestStrategy struct {
Symbol string `json:"symbol"`
Interval string `json:"interval"`
BaseQuantity fixedpoint.Value `json:"baseQuantity"`
MaxAssetQuantity fixedpoint.Value `json:"maxAssetQuantity"`
MinDropPercentage fixedpoint.Value `json:"minDropPercentage"`
2020-10-26 13:45:02 +00:00
}
2021-02-03 01:09:19 +00:00
func (s *TestStrategy) ID() string {
return "test"
}
2020-10-26 13:45:02 +00:00
func (s *TestStrategy) Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error {
return nil
}
2020-10-23 06:01:45 +00:00
func TestLoadConfig(t *testing.T) {
2020-10-20 05:52:25 +00:00
type args struct {
configFile string
}
tests := []struct {
name string
args args
wantErr bool
2020-10-26 09:57:28 +00:00
f func(t *testing.T, config *Config)
2020-10-20 05:52:25 +00:00
}{
2020-10-27 00:48:47 +00:00
{
name: "notification",
args: args{configFile: "testdata/notification.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.NotNil(t, config.Notifications)
assert.NotNil(t, config.Notifications.SessionChannels)
assert.NotNil(t, config.Notifications.SymbolChannels)
assert.Equal(t, map[string]string{
"^BTC": "#btc",
"^ETH": "#eth",
}, config.Notifications.SymbolChannels)
assert.NotNil(t, config.Notifications.Routing)
assert.Equal(t, "#dev-bbgo", config.Notifications.Slack.DefaultChannel)
assert.Equal(t, "#error", config.Notifications.Slack.ErrorChannel)
},
},
2021-02-03 01:34:53 +00:00
2020-10-20 05:52:25 +00:00
{
2020-10-26 09:57:28 +00:00
name: "strategy",
args: args{configFile: "testdata/strategy.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.Len(t, config.ExchangeStrategies, 1)
2021-02-03 01:58:31 +00:00
assert.Equal(t, []ExchangeStrategyMount{{
2021-02-03 01:34:53 +00:00
Mounts: []string{"binance"},
Strategy: &TestStrategy{
Symbol: "BTCUSDT",
Interval: "1m",
BaseQuantity: fixedpoint.NewFromFloat(0.1),
MaxAssetQuantity: fixedpoint.NewFromFloat(1.1),
MinDropPercentage: fixedpoint.NewFromFloat(-0.05),
2021-02-03 01:34:53 +00:00
},
2021-02-03 01:58:31 +00:00
}}, config.ExchangeStrategies)
2021-02-03 01:34:53 +00:00
m, err := config.Map()
assert.NoError(t, err)
assert.Equal(t, map[string]interface{}{
"sessions": map[string]interface{}{
"max": map[string]interface{}{
"exchange": "max",
"envVarPrefix": "MAX",
"takerFeeRate": 0.,
"makerFeeRate": 0.,
2021-02-03 01:34:53 +00:00
},
"binance": map[string]interface{}{
"exchange": "binance",
"envVarPrefix": "BINANCE",
"takerFeeRate": 0.,
"makerFeeRate": 0.,
2021-02-03 01:34:53 +00:00
},
},
"build": map[string]interface{}{
"buildDir": "build",
"targets": []interface{}{
map[string]interface{}{
"name": "bbgow-amd64-darwin",
"arch": "amd64",
"os": "darwin",
},
map[string]interface{}{
"name": "bbgow-amd64-linux",
"arch": "amd64",
"os": "linux",
},
},
},
"exchangeStrategies": []map[string]interface{}{
{
"on": []string{"binance"},
"test": map[string]interface{}{
2021-02-03 01:34:53 +00:00
"symbol": "BTCUSDT",
"baseQuantity": 0.1,
"interval": "1m",
2021-02-03 01:58:31 +00:00
"maxAssetQuantity": 1.1,
2021-02-03 01:34:53 +00:00
"minDropPercentage": -0.05,
},
},
},
}, m)
2021-02-03 01:58:31 +00:00
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)
2020-10-20 05:52:25 +00:00
},
2020-10-26 09:57:28 +00:00
},
2020-12-06 10:58:05 +00:00
{
name: "persistence",
args: args{configFile: "testdata/persistence.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.NotNil(t, config.Persistence)
assert.NotNil(t, config.Persistence.Redis)
assert.NotNil(t, config.Persistence.Json)
},
},
2020-10-26 09:57:28 +00:00
{
name: "order_executor",
args: args{configFile: "testdata/order_executor.yaml"},
2020-10-20 05:52:25 +00:00
wantErr: false,
2020-10-26 09:57:28 +00:00
f: func(t *testing.T, config *Config) {
assert.Len(t, config.Sessions, 2)
session, ok := config.Sessions["max"]
assert.True(t, ok)
assert.NotNil(t, session)
riskControls := config.RiskControls
assert.NotNil(t, riskControls)
assert.NotNil(t, riskControls.SessionBasedRiskControl)
2020-10-26 09:57:28 +00:00
conf, ok := riskControls.SessionBasedRiskControl["max"]
assert.True(t, ok)
assert.NotNil(t, conf)
assert.NotNil(t, conf.OrderExecutor)
assert.NotNil(t, conf.OrderExecutor.BySymbol)
executorConf, ok := conf.OrderExecutor.BySymbol["BTCUSDT"]
2020-10-26 09:57:28 +00:00
assert.True(t, ok)
assert.NotNil(t, executorConf)
},
2020-10-20 05:52:25 +00:00
},
{
name: "backtest",
args: args{configFile: "testdata/backtest.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.Len(t, config.ExchangeStrategies, 1)
assert.NotNil(t, config.Backtest)
assert.NotNil(t, config.Backtest.Account)
assert.NotNil(t, config.Backtest.Account["binance"].Balances)
assert.Len(t, config.Backtest.Account["binance"].Balances, 2)
},
},
2020-10-20 05:52:25 +00:00
}
2020-10-20 06:14:21 +00:00
2020-10-20 05:52:25 +00:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-12-29 08:00:03 +00:00
config, err := Load(tt.args.configFile, true)
2020-10-20 05:52:25 +00:00
if err != nil {
2020-10-20 06:32:22 +00:00
t.Errorf("Load() error = %v", err)
return
2020-10-20 05:52:25 +00:00
} else {
if tt.wantErr {
2020-10-20 06:32:22 +00:00
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
return
2020-10-20 05:52:25 +00:00
}
}
2020-10-20 06:32:22 +00:00
assert.NotNil(t, config)
2020-10-26 09:57:28 +00:00
if tt.f != nil {
tt.f(t, config)
}
2020-10-20 05:52:25 +00:00
})
}
2020-10-20 05:52:25 +00:00
}