Merge pull request #1787 from c9s/c9s/fix-order-compare

FIX: fix order update comparison method
This commit is contained in:
c9s 2024-10-22 12:18:53 +08:00 committed by GitHub
commit dcf2f52873
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 11 deletions

View File

@ -16,7 +16,7 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
const DefaultCancelOrderWaitTime = 20 * time.Millisecond const DefaultCancelOrderWaitTime = 50 * time.Millisecond
const DefaultOrderCancelTimeout = 5 * time.Second const DefaultOrderCancelTimeout = 5 * time.Second
// ActiveOrderBook manages the local active order books. // ActiveOrderBook manages the local active order books.
@ -336,7 +336,7 @@ func (b *ActiveOrderBook) Update(order types.Order) {
// if we can't detect which is newer, isNewerOrderUpdate returns false // if we can't detect which is newer, isNewerOrderUpdate returns false
// if you pass two same objects to isNewerOrderUpdate, it returns false // if you pass two same objects to isNewerOrderUpdate, it returns false
if !isNewerOrderUpdate(order, previousOrder) { if !isNewerOrderUpdate(order, previousOrder) {
log.Infof("[ActiveOrderBook] order #%d updateTime %s is out of date, skip it", order.OrderID, order.UpdateTime) log.Infof("[ActiveOrderBook] order #%d (update time %s) is out of date, skip it", order.OrderID, order.UpdateTime)
b.mu.Unlock() b.mu.Unlock()
return return
} }
@ -393,26 +393,33 @@ func (b *ActiveOrderBook) Add(orders ...types.Order) {
} }
} }
// isNewerOrderUpdate checks if the order a is newer than the order b.
func isNewerOrderUpdate(a, b types.Order) bool { func isNewerOrderUpdate(a, b types.Order) bool {
// compare state first // compare state first
switch a.Status { switch a.Status {
case types.OrderStatusCanceled, types.OrderStatusRejected: // canceled is a final state case types.OrderStatusCanceled, types.OrderStatusRejected: // canceled is a final state
switch b.Status { switch b.Status {
case types.OrderStatusNew, types.OrderStatusPartiallyFilled:
case types.OrderStatusCanceled, types.OrderStatusRejected, types.OrderStatusNew, types.OrderStatusPartiallyFilled:
return true return true
} }
case types.OrderStatusPartiallyFilled: case types.OrderStatusPartiallyFilled:
switch b.Status { switch b.Status {
case types.OrderStatusNew: case types.OrderStatusNew:
return true return true
case types.OrderStatusPartiallyFilled: case types.OrderStatusPartiallyFilled:
// unknown for equal // unknown for equal
if a.ExecutedQuantity.Compare(b.ExecutedQuantity) > 0 { if a.ExecutedQuantity.Compare(b.ExecutedQuantity) > 0 {
return true return true
} }
if a.UpdateTime.After(b.UpdateTime.Time()) {
return true
}
} }
case types.OrderStatusFilled: case types.OrderStatusFilled:

View File

@ -147,6 +147,7 @@ func (s *OrderStore) Prune(expiryDuration time.Duration) {
defer s.mu.Unlock() defer s.mu.Unlock()
for idx, o := range s.orders { for idx, o := range s.orders {
// if the order is canceled or filled, we should remove the order if the update time is before the cut off time
if o.Status == types.OrderStatusCanceled || o.Status == types.OrderStatusFilled { if o.Status == types.OrderStatusCanceled || o.Status == types.OrderStatusFilled {
if o.UpdateTime.Time().Before(cutOffTime) { if o.UpdateTime.Time().Before(cutOffTime) {
continue continue

View File

@ -1213,16 +1213,13 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
} }
lastPrice := s.lastPrice.Get() lastPrice := s.lastPrice.Get()
sourceBook := s.sourceBook.CopyDepth(1)
bestBid, bestAsk, ok := s.sourceBook.BestBidAndAsk()
if ok {
switch side { switch side {
case types.SideTypeBuy: case types.SideTypeBuy:
if bestAsk, ok := sourceBook.BestAsk(); ok {
lastPrice = bestAsk.Price lastPrice = bestAsk.Price
}
case types.SideTypeSell: case types.SideTypeSell:
if bestBid, ok := sourceBook.BestBid(); ok {
lastPrice = bestBid.Price lastPrice = bestBid.Price
} }
} }