all: fix converter initialization

This commit is contained in:
c9s 2024-08-12 15:56:24 +08:00
parent 1b0d4599e2
commit 9911a4f711
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 56 additions and 9 deletions

View File

@ -1,6 +1,10 @@
package core package core
import "github.com/c9s/bbgo/pkg/types" import (
"errors"
"github.com/c9s/bbgo/pkg/types"
)
type Converter interface { type Converter interface {
OrderConverter OrderConverter
@ -53,6 +57,14 @@ func NewSymbolConverter(fromSymbol, toSymbol string) *SymbolConverter {
} }
func (c *SymbolConverter) Initialize() error { func (c *SymbolConverter) Initialize() error {
if c.ToSymbol == "" {
return errors.New("toSymbol can not be empty")
}
if c.FromSymbol == "" {
return errors.New("fromSymbol can not be empty")
}
return nil return nil
} }

View File

@ -12,28 +12,59 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
type ConverterSetting struct {
SymbolConverter *SymbolConverter `json:"symbolConverter" yaml:"symbolConverter"`
}
func (s *ConverterSetting) getConverter() Converter {
if s.SymbolConverter != nil {
return s.SymbolConverter
}
return nil
}
func (s *ConverterSetting) InitializeConverter() (Converter, error) {
converter := s.getConverter()
if converter == nil {
return nil, nil
}
logrus.Infof("initializing converter %T ...", converter)
err := converter.Initialize()
return nil, err
}
type ConverterManager struct { type ConverterManager struct {
Converters []Converter `json:"converters,omitempty" yaml:"converters,omitempty"` ConverterSettings []ConverterSetting `json:"converters,omitempty" yaml:"converters,omitempty"`
converters []Converter
} }
func (c *ConverterManager) Initialize() error { func (c *ConverterManager) Initialize() error {
for _, converter := range c.Converters { for _, setting := range c.ConverterSettings {
_ = converter
converter, err := setting.InitializeConverter()
if err != nil {
return err
}
c.AddConverter(converter)
} }
return nil return nil
} }
func (c *ConverterManager) AddConverter(converter Converter) { func (c *ConverterManager) AddConverter(converter Converter) {
c.Converters = append(c.Converters, converter) c.converters = append(c.converters, converter)
} }
func (c *ConverterManager) ConvertOrder(order types.Order) types.Order { func (c *ConverterManager) ConvertOrder(order types.Order) types.Order {
if len(c.Converters) == 0 { if len(c.converters) == 0 {
return order return order
} }
for _, converter := range c.Converters { for _, converter := range c.converters {
convOrder, err := converter.ConvertOrder(order) convOrder, err := converter.ConvertOrder(order)
if err != nil { if err != nil {
logrus.WithError(err).Errorf("converter %+v error, order: %s", converter, order.String()) logrus.WithError(err).Errorf("converter %+v error, order: %s", converter, order.String())
@ -47,11 +78,11 @@ func (c *ConverterManager) ConvertOrder(order types.Order) types.Order {
} }
func (c *ConverterManager) ConvertTrade(trade types.Trade) types.Trade { func (c *ConverterManager) ConvertTrade(trade types.Trade) types.Trade {
if len(c.Converters) == 0 { if len(c.converters) == 0 {
return trade return trade
} }
for _, converter := range c.Converters { for _, converter := range c.converters {
convTrade, err := converter.ConvertTrade(trade) convTrade, err := converter.ConvertTrade(trade)
if err != nil { if err != nil {
logrus.WithError(err).Errorf("converter %+v error, trade: %s", converter, trade.String()) logrus.WithError(err).Errorf("converter %+v error, trade: %s", converter, trade.String())

View File

@ -82,6 +82,10 @@ func (s *CrossExchangeMarketMakingStrategy) Initialize(
return fmt.Errorf("maker session market %s is not defined", symbol) return fmt.Errorf("maker session market %s is not defined", symbol)
} }
if err := s.ConverterManager.Initialize(); err != nil {
return err
}
if s.ProfitStats == nil { if s.ProfitStats == nil {
s.ProfitStats = types.NewProfitStats(s.makerMarket) s.ProfitStats = types.NewProfitStats(s.makerMarket)
} }