add LoadedCrossExchangeStrategies loader api

This commit is contained in:
c9s 2020-10-20 14:21:46 +08:00
parent f4066b18b3
commit 4ee10de40f
3 changed files with 25 additions and 22 deletions

View File

@ -17,12 +17,17 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
var LoadedStrategies = make(map[string]interface{}) var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy)
func RegisterStrategy(key string, configmap interface{}) { func RegisterExchangeStrategy(key string, configmap SingleExchangeStrategy) {
LoadedStrategies[key] = configmap 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 // Environment presents the real exchange data layer
type Environment struct { type Environment struct {

View File

@ -11,18 +11,29 @@ import (
"github.com/c9s/bbgo/pkg/bbgo" "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) config, err := ioutil.ReadFile(configFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
conf := make(map[string]interface{}) stash := make(Stash)
if err := yaml.Unmarshal(config, conf); err != nil { if err := yaml.Unmarshal(config, stash); err != nil {
return nil, err 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 { if !ok {
return nil, errors.New("exchangeStrategies is not defined") return nil, errors.New("exchangeStrategies is not defined")
} }
@ -39,7 +50,7 @@ func LoadExchangeStrategies(configFile string) (strategies []bbgo.SingleExchange
} }
for id, conf := range sConf { for id, conf := range sConf {
if st, ok := bbgo.LoadedStrategies[id]; ok { if st, ok := bbgo.LoadedExchangeStrategies[id]; ok {
// get the type "*Strategy" // get the type "*Strategy"
rt := reflect.TypeOf(st) rt := reflect.TypeOf(st)
val := reflect.New(rt) val := reflect.New(rt)

View File

@ -2,8 +2,6 @@ package buyandhold
import ( import (
"context" "context"
"encoding/json"
"io/ioutil"
"math" "math"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -14,7 +12,7 @@ import (
) )
func init() { func init() {
bbgo.RegisterStrategy("buyandhold", &Strategy{}) bbgo.RegisterExchangeStrategy("buyandhold", &Strategy{})
} }
type Strategy struct { type Strategy struct {
@ -25,17 +23,6 @@ type Strategy struct {
MinDropPercentage float64 `json:"minDropPercentage"` 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 { func New(symbol string, interval string, baseQuantity float64) *Strategy {
return &Strategy{ return &Strategy{
Symbol: symbol, Symbol: symbol,