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: [core] fix order update comparison method #1787

Merged
merged 4 commits into from
Oct 22, 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
13 changes: 10 additions & 3 deletions pkg/bbgo/activeorderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const DefaultCancelOrderWaitTime = 20 * time.Millisecond
const DefaultCancelOrderWaitTime = 50 * time.Millisecond
const DefaultOrderCancelTimeout = 5 * time.Second

// ActiveOrderBook manages the local active order books.
Expand Down Expand Up @@ -336,7 +336,7 @@ func (b *ActiveOrderBook) Update(order types.Order) {
// if we can't detect which is newer, isNewerOrderUpdate returns false
// if you pass two same objects to isNewerOrderUpdate, it returns false
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()
return
}
Expand Down Expand Up @@ -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 {
// compare state first
switch a.Status {

case types.OrderStatusCanceled, types.OrderStatusRejected: // canceled is a final state
switch b.Status {
case types.OrderStatusNew, types.OrderStatusPartiallyFilled:

case types.OrderStatusCanceled, types.OrderStatusRejected, types.OrderStatusNew, types.OrderStatusPartiallyFilled:
return true
}

case types.OrderStatusPartiallyFilled:
switch b.Status {

case types.OrderStatusNew:
return true

case types.OrderStatusPartiallyFilled:
// unknown for equal
if a.ExecutedQuantity.Compare(b.ExecutedQuantity) > 0 {
return true
}

if a.UpdateTime.After(b.UpdateTime.Time()) {
return true
}
}

case types.OrderStatusFilled:
Expand Down
1 change: 1 addition & 0 deletions pkg/core/orderstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (s *OrderStore) Prune(expiryDuration time.Duration) {
defer s.mu.Unlock()

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.UpdateTime.Time().Before(cutOffTime) {
continue
Expand Down
13 changes: 5 additions & 8 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1213,16 +1213,13 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
}

lastPrice := s.lastPrice.Get()
sourceBook := s.sourceBook.CopyDepth(1)
switch side {

case types.SideTypeBuy:
if bestAsk, ok := sourceBook.BestAsk(); ok {
bestBid, bestAsk, ok := s.sourceBook.BestBidAndAsk()
if ok {
switch side {
case types.SideTypeBuy:
lastPrice = bestAsk.Price
}

case types.SideTypeSell:
if bestBid, ok := sourceBook.BestBid(); ok {
case types.SideTypeSell:
lastPrice = bestBid.Price
}
}
Expand Down
Loading