Skip to content

Commit

Permalink
Merge pull request c9s#1581 from c9s/edwin/bybit/return-order-directl…
Browse files Browse the repository at this point in the history
…y-after-placing-the-order

MINOR: [okx][bitget] remove the query after place order
  • Loading branch information
bailantaotao authored Mar 14, 2024
2 parents 747b75f + d75e7eb commit 3a98660
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 270 deletions.
21 changes: 0 additions & 21 deletions pkg/exchange/bitget/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,27 +225,6 @@ func unfilledOrderToGlobalOrder(order v2.UnfilledOrder) (*types.Order, error) {
}, nil
}

func fallbackPostOnlyOrder(order types.SubmitOrder, orderId string) (*types.Order, error) {
intOrderId, err := strconv.ParseUint(orderId, 10, 64)
if err != nil {
return nil, err
}
now := time.Now()

return &types.Order{
SubmitOrder: order,
Exchange: types.ExchangeBitget,
OrderID: intOrderId,
UUID: orderId,
Status: types.OrderStatusNew,
OriginalStatus: "FALLBACK_STATUS",
ExecutedQuantity: fixedpoint.Zero,
IsWorking: true,
CreationTime: types.Time(now),
UpdateTime: types.Time(now),
}, nil
}

func toGlobalOrder(order v2.OrderDetail) (*types.Order, error) {
side, err := toGlobalSideType(order.Side)
if err != nil {
Expand Down
50 changes: 15 additions & 35 deletions pkg/exchange/bitget/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

Expand Down Expand Up @@ -344,6 +345,7 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (cr
return nil, fmt.Errorf("place order rate limiter wait error: %w", err)
}

timeNow := time.Now()
res, err := req.Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to place order, order: %#v, err: %w", order, err)
Expand All @@ -355,44 +357,22 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (cr
return nil, fmt.Errorf("unexpected order id, resp: %#v, order: %#v", res, order)
}

orderId := res.OrderId

debugf("fetching unfilled order info for order #%s", orderId)
ordersResp, err := e.v2client.NewGetUnfilledOrdersRequest().OrderId(orderId).Do(ctx)
intOrderId, err := strconv.ParseUint(res.OrderId, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to query open order by order id: %s, err: %w", orderId, err)
}

debugf("unfilled order response for order#%s: %+v", orderId, ordersResp)

if len(ordersResp) == 1 {
// 2023/11/05 The market order will be executed immediately, so we cannot retrieve it through the NewGetUnfilledOrdersRequest API.
// Try to get the order from the NewGetHistoryOrdersRequest API.
// 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.
return unfilledOrderToGlobalOrder(ordersResp[0])
} else if len(ordersResp) == 0 {
ordersResp, err := e.v2client.NewGetHistoryOrdersRequest().OrderId(orderId).Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to query history order by order id: %s, err: %w", orderId, err)
}

if len(ordersResp) != 1 {
// 2023/03/12 If it's a maker order and there is a corresponding order to be executed, then the order will be canceled,
// you can receive the status immediately from the websocket, but the RestAPI requires at least 200ms waiting time.
//
// Therefore, We don't want to waste time waiting for him, so we choose to manually enter the order
// information and send it back.
if order.Type == types.OrderTypeLimitMaker {
return fallbackPostOnlyOrder(order, orderId)
}
return nil, fmt.Errorf("unexpected length of history orders, expecting: 1, given: %d, ids: %s", len(ordersResp), orderId)
}

return toGlobalOrder(ordersResp[0])
return nil, err
}

return nil, fmt.Errorf("unexpected length of unfilled orders, expecting: 1, given: %d, ids: %s", len(ordersResp), orderId)
return &types.Order{
SubmitOrder: order,
Exchange: types.ExchangeBitget,
OrderID: intOrderId,
UUID: res.OrderId,
Status: types.OrderStatusNew,
ExecutedQuantity: fixedpoint.Zero,
IsWorking: true,
CreationTime: types.Time(timeNow),
UpdateTime: types.Time(timeNow),
}, nil
}

func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
Expand Down
Loading

0 comments on commit 3a98660

Please sign in to comment.