diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 8c2c33536..8b6cef0cd 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -17,12 +17,17 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -var LoadedStrategies = make(map[string]interface{}) +var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy) -func RegisterStrategy(key string, configmap interface{}) { - LoadedStrategies[key] = configmap +func RegisterExchangeStrategy(key string, configmap SingleExchangeStrategy) { + LoadedExchangeStrategies[key] = configmap } +var LoadedCrossExchangeStrategies = make(map[string]CrossExchangeStrategy) + +func RegisterCrossExchangeStrategy(key string, configmap CrossExchangeStrategy) { + LoadedCrossExchangeStrategies[key] = configmap +} // Environment presents the real exchange data layer type Environment struct { diff --git a/pkg/loader/loader.go b/pkg/loader/loader.go index 05b8552b3..cf5b6da6e 100644 --- a/pkg/loader/loader.go +++ b/pkg/loader/loader.go @@ -11,18 +11,29 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" ) -func LoadExchangeStrategies(configFile string) (strategies []bbgo.SingleExchangeStrategy, err error) { +type Stash map[string]interface{} + +func Load(configFile string) (Stash, error) { config, err := ioutil.ReadFile(configFile) if err != nil { return nil, err } - conf := make(map[string]interface{}) - if err := yaml.Unmarshal(config, conf); err != nil { + stash := make(Stash) + if err := yaml.Unmarshal(config, stash); err != nil { return nil, err } - exchangeStrategiesConf, ok := conf["exchangeStrategies"] + return stash, err +} + +func LoadExchangeStrategies(configFile string) (strategies []bbgo.SingleExchangeStrategy, err error) { + stash, err := Load(configFile) + if err != nil { + return nil, err + } + + exchangeStrategiesConf, ok := stash["exchangeStrategies"] if !ok { return nil, errors.New("exchangeStrategies is not defined") } @@ -39,7 +50,7 @@ func LoadExchangeStrategies(configFile string) (strategies []bbgo.SingleExchange } for id, conf := range sConf { - if st, ok := bbgo.LoadedStrategies[id]; ok { + if st, ok := bbgo.LoadedExchangeStrategies[id]; ok { // get the type "*Strategy" rt := reflect.TypeOf(st) val := reflect.New(rt) diff --git a/pkg/strategy/buyandhold/strategy.go b/pkg/strategy/buyandhold/strategy.go index bba3ca96d..a26d35783 100644 --- a/pkg/strategy/buyandhold/strategy.go +++ b/pkg/strategy/buyandhold/strategy.go @@ -2,8 +2,6 @@ package buyandhold import ( "context" - "encoding/json" - "io/ioutil" "math" log "github.com/sirupsen/logrus" @@ -14,7 +12,7 @@ import ( ) func init() { - bbgo.RegisterStrategy("buyandhold", &Strategy{}) + bbgo.RegisterExchangeStrategy("buyandhold", &Strategy{}) } type Strategy struct { @@ -25,17 +23,6 @@ type Strategy struct { MinDropPercentage float64 `json:"minDropPercentage"` } -func LoadFile(filepath string) (*Strategy, error) { - o, err := ioutil.ReadFile(filepath) - if err != nil { - return nil, err - } - - var strategy Strategy - err = json.Unmarshal(o, &strategy) - return &strategy, err -} - func New(symbol string, interval string, baseQuantity float64) *Strategy { return &Strategy{ Symbol: symbol,