Skip to content
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

FIX: [bitget] support market order on bitget unfilled order conversion #1561

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions pkg/exchange/bitget/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,17 @@ func unfilledOrderToGlobalOrder(order v2.UnfilledOrder) (*types.Order, error) {
qty := order.Size
price := order.PriceAvg

// The market order will be executed immediately, so this check is used to handle corner cases.
// 2023/11/05 The market order will be executed immediately, so this check is used to handle corner cases.
// 2024/03/06 After placing a Market Order, we can retrieve it through the unfilledOrder API, so we still need to
// handle the Market Order status.
if orderType == types.OrderTypeMarket {
qty = order.BaseVolume
log.Warnf("!!! The price(%f) and quantity(%f) are not verified for market orders, because we only receive limit orders in the test environment !!!", price.Float64(), qty.Float64())
price = order.PriceAvg
if side == types.SideTypeBuy {
qty, err = processMarketBuyQuantity(order.BaseVolume, order.QuoteVolume, order.PriceAvg, order.Size, order.Status)
if err != nil {
return nil, err
}
}
}

return &types.Order{
Expand Down
63 changes: 62 additions & 1 deletion pkg/exchange/bitget/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func Test_unfilledOrderToGlobalOrder(t *testing.T) {
}
)

t.Run("succeeds", func(t *testing.T) {
t.Run("succeeds with limit order", func(t *testing.T) {
order, err := unfilledOrderToGlobalOrder(unfilledOrder)
assert.NoError(err)
assert.Equal(&types.Order{
Expand All @@ -251,6 +251,67 @@ func Test_unfilledOrderToGlobalOrder(t *testing.T) {
}, order)
})

t.Run("succeeds with market buy order", func(t *testing.T) {
unfilledOrder2 := unfilledOrder
unfilledOrder2.OrderType = v2.OrderTypeMarket
unfilledOrder2.Side = v2.SideTypeBuy
unfilledOrder2.Size = unfilledOrder2.PriceAvg.Mul(unfilledOrder2.Size)
unfilledOrder2.PriceAvg = fixedpoint.Zero
unfilledOrder2.Status = v2.OrderStatusNew

order, err := unfilledOrderToGlobalOrder(unfilledOrder2)
assert.NoError(err)
assert.Equal(&types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: "74b86af3-6098-479c-acac-bfb074c067f3",
Symbol: "BTCUSDT",
Side: types.SideTypeBuy,
Type: types.OrderTypeMarket,
Quantity: fixedpoint.Zero,
Price: fixedpoint.Zero,
TimeInForce: types.TimeInForceGTC,
},
Exchange: types.ExchangeBitget,
OrderID: uint64(orderId),
UUID: strconv.FormatInt(int64(orderId), 10),
Status: types.OrderStatusNew,
ExecutedQuantity: fixedpoint.Zero,
IsWorking: true,
CreationTime: types.Time(types.NewMillisecondTimestampFromInt(1660704288118).Time()),
UpdateTime: types.Time(types.NewMillisecondTimestampFromInt(1660704288118).Time()),
}, order)
})

t.Run("succeeds with market sell order", func(t *testing.T) {
unfilledOrder2 := unfilledOrder
unfilledOrder2.OrderType = v2.OrderTypeMarket
unfilledOrder2.Side = v2.SideTypeSell
unfilledOrder2.PriceAvg = fixedpoint.Zero
unfilledOrder2.Status = v2.OrderStatusNew

order, err := unfilledOrderToGlobalOrder(unfilledOrder2)
assert.NoError(err)
assert.Equal(&types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: "74b86af3-6098-479c-acac-bfb074c067f3",
Symbol: "BTCUSDT",
Side: types.SideTypeSell,
Type: types.OrderTypeMarket,
Quantity: fixedpoint.NewFromInt(5),
Price: fixedpoint.Zero,
TimeInForce: types.TimeInForceGTC,
},
Exchange: types.ExchangeBitget,
OrderID: uint64(orderId),
UUID: strconv.FormatInt(int64(orderId), 10),
Status: types.OrderStatusNew,
ExecutedQuantity: fixedpoint.Zero,
IsWorking: true,
CreationTime: types.Time(types.NewMillisecondTimestampFromInt(1660704288118).Time()),
UpdateTime: types.Time(types.NewMillisecondTimestampFromInt(1660704288118).Time()),
}, order)
})

t.Run("failed to convert side", func(t *testing.T) {
newOrder := unfilledOrder
newOrder.Side = "xxx"
Expand Down
Loading