Skip to content

FIX: [core] setting.InitializeConverter could return a nil converter object #1696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pkg/core/tradecollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ func (s *ConverterSetting) InitializeConverter() (Converter, error) {

logrus.Infof("initializing converter %T ...", converter)
err := converter.Initialize()
return nil, err
return converter, err
}

// ConverterManager manages the converters for trade conversion
// It can be used to convert the trade symbol into the target symbol, or convert the price, volume into different units.
type ConverterManager struct {
ConverterSettings []ConverterSetting `json:"converters,omitempty" yaml:"converters,omitempty"`

Expand All @@ -43,15 +45,18 @@ type ConverterManager struct {

func (c *ConverterManager) Initialize() error {
for _, setting := range c.ConverterSettings {

converter, err := setting.InitializeConverter()
if err != nil {
return err
}

c.AddConverter(converter)
if converter != nil {
c.AddConverter(converter)
}
}

numConverters := len(c.converters)
logrus.Infof("%d converters loaded", numConverters)
return nil
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/core/tradecollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

func TestTradeCollector_NilConvertManager(t *testing.T) {
symbol := "BTCUSDT"
position := types.NewPosition(symbol, "BTC", "USDT")
orderStore := NewOrderStore(symbol)
collector := NewTradeCollector(symbol, position, orderStore)

trade := types.Trade{
ID: 1,
OrderID: 399,
Exchange: types.ExchangeBinance,
Price: fixedpoint.NewFromInt(40000),
Quantity: fixedpoint.One,
QuoteQuantity: fixedpoint.NewFromInt(40000),
Symbol: "BTCUSDT",
Side: types.SideTypeBuy,
IsBuyer: true,
}

trade = collector.ConvertTrade(trade)
assert.Equal(t, "BTCUSDT", trade.Symbol)
}

func TestTradeCollector_ShouldNotCountDuplicatedTrade(t *testing.T) {
symbol := "BTCUSDT"
position := types.NewPosition(symbol, "BTC", "USDT")
Expand Down
4 changes: 3 additions & 1 deletion pkg/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (p *Position) NewMarketCloseOrder(percentage fixedpoint.Value) *SubmitOrder
}
}

// IsDust checks if the position is dust, the first argument is the price to calculate the dust quantity
func (p *Position) IsDust(a ...fixedpoint.Value) bool {
price := p.AverageCost
if len(a) > 0 {
Expand Down Expand Up @@ -448,6 +449,7 @@ func (p *Position) String() string {
)
}

// BindStream binds the trade update callback and update the position
func (p *Position) BindStream(stream Stream) {
stream.OnTradeUpdate(func(trade Trade) {
if p.Symbol == trade.Symbol {
Expand Down Expand Up @@ -540,7 +542,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
p.addTradeFee(td)

// Base > 0 means we're in long position
// Base < 0 means we're in short position
// Base < 0 means we're in short position
switch td.Side {

case SideTypeBuy:
Expand Down
Loading