mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
core: add dynamic converter
This commit is contained in:
parent
f277b191d2
commit
00e860df26
|
@ -17,26 +17,47 @@ type TradeConverter interface {
|
||||||
ConvertTrade(trade types.Trade) (types.Trade, error)
|
ConvertTrade(trade types.Trade) (types.Trade, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OrderConvertFunc func(order types.Order) (types.Order, error)
|
||||||
|
type TradeConvertFunc func(trade types.Trade) (types.Trade, error)
|
||||||
|
|
||||||
|
type DynamicConverter struct {
|
||||||
|
orderConverter OrderConvertFunc
|
||||||
|
tradeConverter TradeConvertFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDynamicConverter(orderConverter OrderConvertFunc, tradeConverter TradeConvertFunc) *DynamicConverter {
|
||||||
|
return &DynamicConverter{orderConverter: orderConverter, tradeConverter: tradeConverter}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DynamicConverter) ConvertOrder(order types.Order) (types.Order, error) {
|
||||||
|
return c.orderConverter(order)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DynamicConverter) ConvertTrade(trade types.Trade) (types.Trade, error) {
|
||||||
|
return c.tradeConverter(trade)
|
||||||
|
}
|
||||||
|
|
||||||
// SymbolConverter converts the symbol to another symbol
|
// SymbolConverter converts the symbol to another symbol
|
||||||
type SymbolConverter struct {
|
type SymbolConverter struct {
|
||||||
fromSymbol, toSymbol string
|
FromSymbol string `json:"from"`
|
||||||
|
ToSymbol string `json:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSymbolConverter(fromSymbol, toSymbol string) *SymbolConverter {
|
func NewSymbolConverter(fromSymbol, toSymbol string) *SymbolConverter {
|
||||||
return &SymbolConverter{fromSymbol: fromSymbol, toSymbol: toSymbol}
|
return &SymbolConverter{FromSymbol: fromSymbol, ToSymbol: toSymbol}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SymbolConverter) ConvertOrder(order types.Order) (types.Order, error) {
|
func (c *SymbolConverter) ConvertOrder(order types.Order) (types.Order, error) {
|
||||||
if order.Symbol == c.fromSymbol {
|
if order.Symbol == c.FromSymbol {
|
||||||
order.Symbol = c.toSymbol
|
order.Symbol = c.ToSymbol
|
||||||
}
|
}
|
||||||
|
|
||||||
return order, nil
|
return order, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SymbolConverter) ConvertTrade(trade types.Trade) (types.Trade, error) {
|
func (c *SymbolConverter) ConvertTrade(trade types.Trade) (types.Trade, error) {
|
||||||
if trade.Symbol == c.fromSymbol {
|
if trade.Symbol == c.FromSymbol {
|
||||||
trade.Symbol = c.toSymbol
|
trade.Symbol = c.ToSymbol
|
||||||
}
|
}
|
||||||
|
|
||||||
return trade, nil
|
return trade, nil
|
||||||
|
|
|
@ -13,19 +13,27 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConverterManager struct {
|
type ConverterManager struct {
|
||||||
converters []Converter
|
Converters []Converter `json:"converters,omitempty" yaml:"converters,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConverterManager) Initialize() error {
|
||||||
|
for _, converter := range c.Converters {
|
||||||
|
_ = converter
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
@ -39,11 +47,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())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user