mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
support loading cross exchange strategies
This commit is contained in:
parent
606c59ad4d
commit
81f2cb4ac4
|
@ -11,9 +11,14 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
)
|
||||
|
||||
type SingleExchangeStrategyConfig struct {
|
||||
Mounts []string
|
||||
Strategy bbgo.SingleExchangeStrategy
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
ExchangeStrategies map[string][]bbgo.SingleExchangeStrategy
|
||||
CrossExchangeStrategies map[string][]bbgo.CrossExchangeStrategy
|
||||
ExchangeStrategies []SingleExchangeStrategyConfig
|
||||
CrossExchangeStrategies []bbgo.CrossExchangeStrategy
|
||||
}
|
||||
|
||||
type Stash map[string]interface{}
|
||||
|
@ -46,29 +51,29 @@ func Load(configFile string) (*Config, error) {
|
|||
}
|
||||
|
||||
config.ExchangeStrategies = strategies
|
||||
|
||||
crossExchangeStrategies, err := loadCrossExchangeStrategies(stash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.CrossExchangeStrategies = crossExchangeStrategies
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func loadExchangeStrategies(stash Stash) (strategies map[string][]bbgo.SingleExchangeStrategy, err error) {
|
||||
exchangeStrategiesConf, ok := stash["exchangeStrategies"]
|
||||
func loadCrossExchangeStrategies(stash Stash) (strategies []bbgo.CrossExchangeStrategy, err error) {
|
||||
exchangeStrategiesConf, ok := stash["crossExchangeStrategies"]
|
||||
if !ok {
|
||||
return nil, errors.New("exchangeStrategies is not defined")
|
||||
return strategies, nil
|
||||
}
|
||||
|
||||
sessionToConfigList, ok := exchangeStrategiesConf.(Stash)
|
||||
configList, ok := exchangeStrategiesConf.([]interface{})
|
||||
if !ok {
|
||||
return nil, errors.New("expecting session name at the first level")
|
||||
return nil, errors.New("expecting list in crossExchangeStrategies")
|
||||
}
|
||||
|
||||
strategies = make(map[string][]bbgo.SingleExchangeStrategy)
|
||||
|
||||
for sessionName, configList := range sessionToConfigList {
|
||||
list, ok := configList.([]interface{})
|
||||
if !ok {
|
||||
return nil, errors.New("exchangeStrategies should be a list")
|
||||
}
|
||||
|
||||
for _, entry := range list {
|
||||
for _, entry := range configList {
|
||||
configStash, ok := entry.(Stash)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("strategy config should be a map, given: %T %+v", entry, entry)
|
||||
|
@ -82,9 +87,54 @@ func loadExchangeStrategies(stash Stash) (strategies map[string][]bbgo.SingleExc
|
|||
return nil, err
|
||||
}
|
||||
|
||||
strategies[sessionName] = append(strategies[sessionName], val.(bbgo.SingleExchangeStrategy))
|
||||
strategies = append(strategies, val.(bbgo.CrossExchangeStrategy))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return strategies, nil
|
||||
}
|
||||
|
||||
func loadExchangeStrategies(stash Stash) (strategies []SingleExchangeStrategyConfig, err error) {
|
||||
exchangeStrategiesConf, ok := stash["exchangeStrategies"]
|
||||
if !ok {
|
||||
return strategies, nil
|
||||
}
|
||||
|
||||
configList, ok := exchangeStrategiesConf.([]interface{})
|
||||
if !ok {
|
||||
return nil, errors.New("expecting list in exchangeStrategies")
|
||||
}
|
||||
|
||||
for _, entry := range configList {
|
||||
configStash, ok := entry.(Stash)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("strategy config should be a map, given: %T %+v", entry, entry)
|
||||
}
|
||||
|
||||
var mounts []string
|
||||
if val, ok := configStash["on"]; ok {
|
||||
if values, ok := val.([]string); ok {
|
||||
mounts = append(mounts, values...)
|
||||
} else if str, ok := val.(string); ok {
|
||||
mounts = append(mounts, str)
|
||||
}
|
||||
}
|
||||
|
||||
for id, conf := range configStash {
|
||||
// look up the real struct type
|
||||
if st, ok := bbgo.LoadedExchangeStrategies[id]; ok {
|
||||
val, err := reUnmarshal(conf, st)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
strategies = append(strategies, SingleExchangeStrategyConfig{
|
||||
Mounts: mounts,
|
||||
Strategy: val.(bbgo.SingleExchangeStrategy),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
4
pkg/config/testdata/strategy.yaml
vendored
4
pkg/config/testdata/strategy.yaml
vendored
|
@ -9,8 +9,8 @@ sessions:
|
|||
keyVar: BINANCE_API_KEY
|
||||
secretVar: BINANCE_API_SECRET
|
||||
exchangeStrategies:
|
||||
binance:
|
||||
- buyandhold:
|
||||
- on: binance
|
||||
buyandhold:
|
||||
symbol: "BTCUSDT"
|
||||
interval: "1m"
|
||||
baseQuantity: 0.1
|
||||
|
|
Loading…
Reference in New Issue
Block a user