grid2: check order's original status for updating

This commit is contained in:
c9s 2023-11-17 16:46:16 +08:00
parent 5795a71111
commit e5033c093a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 32 additions and 12 deletions

View File

@ -4,14 +4,16 @@ import (
"context"
"time"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.uber.org/multierr"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/max"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
type SyncActiveOrdersOpts struct {
@ -89,6 +91,11 @@ func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
}
}
func isMaxExchange(ex interface{}) bool {
_, yes := ex.(*max.Exchange)
return yes
}
func syncActiveOrders(ctx context.Context, opts SyncActiveOrdersOpts) error {
opts.logger.Infof("[ActiveOrderRecover] syncActiveOrders")
@ -115,6 +122,7 @@ func syncActiveOrders(ctx context.Context, opts SyncActiveOrdersOpts) error {
var errs error
// update active orders not in open orders
for _, activeOrder := range activeOrders {
if _, exist := openOrdersMap[activeOrder.OrderID]; exist {
// no need to sync active order already in active orderbook, because we only need to know if it filled or not.
delete(openOrdersMap, activeOrder.OrderID)

View File

@ -6,11 +6,13 @@ import (
"strconv"
"time"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/bbgo"
maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/pkg/errors"
)
var syncWindow = -3 * time.Minute
@ -267,24 +269,34 @@ func buildTwinOrderBook(pins []Pin, orders []types.Order) (*TwinOrderBook, error
return book, nil
}
func syncActiveOrder(ctx context.Context, activeOrderBook *bbgo.ActiveOrderBook, orderQueryService types.ExchangeOrderQueryService, orderID uint64, syncBefore time.Time) (bool, error) {
func syncActiveOrder(
ctx context.Context, activeOrderBook *bbgo.ActiveOrderBook, orderQueryService types.ExchangeOrderQueryService,
orderID uint64, syncBefore time.Time,
) (isOrderUpdated bool, err error) {
isMax := isMaxExchange(orderQueryService)
updatedOrder, err := retry.QueryOrderUntilSuccessful(ctx, orderQueryService, types.OrderQuery{
Symbol: activeOrderBook.Symbol,
OrderID: strconv.FormatUint(orderID, 10),
})
isActiveOrderBookUpdated := false
if err != nil {
return isActiveOrderBookUpdated, err
return isOrderUpdated, err
}
isActiveOrderBookUpdated = updatedOrder.UpdateTime.Before(syncBefore)
if isActiveOrderBookUpdated {
// maxapi.OrderStateFinalizing does not mean the fee is calculated
// we should only consider order state done for MAX
if isMax && updatedOrder.OriginalStatus != string(maxapi.OrderStateDone) {
return isOrderUpdated, nil
}
// should only trigger order update when the updated time is old enough
isOrderUpdated = updatedOrder.UpdateTime.Before(syncBefore)
if isOrderUpdated {
activeOrderBook.Update(*updatedOrder)
}
return isActiveOrderBookUpdated, nil
return isOrderUpdated, nil
}
func queryTradesToUpdateTwinOrderBook(