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: [grid2] fix active orderbook at recovering #1081

Merged
merged 1 commit into from
Mar 5, 2023
Merged
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
22 changes: 16 additions & 6 deletions pkg/strategy/grid2/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,8 +1341,11 @@ func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService
missingPrices := scanMissingPinPrices(orderBook, grid.Pins)
if numMissing := len(missingPrices); numMissing <= 1 {
s.logger.Infof("GRID RECOVER: no missing grid prices, stop re-playing order history")
s.addOrdersToActiveOrderBook(gridOrders)
s.setGrid(grid)
s.EmitGridReady()
s.updateGridNumOfOrdersMetrics()
s.updateOpenOrderPricesMetrics(s.orderExecutor.ActiveMakerOrders().Orders())
return nil
} else {
s.logger.Infof("GRID RECOVER: found missing prices: %v", missingPrices)
Expand Down Expand Up @@ -1384,8 +1387,11 @@ func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService
// if all orders on the order book are active orders, we don't need to recover.
if isCompleteGridOrderBook(orderBook, s.GridNum) {
s.logger.Infof("GRID RECOVER: all orders are active orders, do not need recover")
s.addOrdersToActiveOrderBook(gridOrders)
s.setGrid(grid)
s.EmitGridReady()
s.updateGridNumOfOrdersMetrics()
s.updateOpenOrderPricesMetrics(s.orderExecutor.ActiveMakerOrders().Orders())
return nil
}

Expand All @@ -1407,11 +1413,7 @@ func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService

// before we re-play the orders,
// we need to add these open orders to the active order book
activeOrderBook := s.orderExecutor.ActiveMakerOrders()
for _, gridOrder := range gridOrders {
// put the order back to the active order book so that we can receive order update
activeOrderBook.Add(gridOrder)
}
s.addOrdersToActiveOrderBook(gridOrders)

s.setGrid(grid)
s.EmitGridReady()
Expand All @@ -1435,6 +1437,14 @@ func (s *Strategy) recoverGridWithOpenOrders(ctx context.Context, historyService
return nil
}

func (s *Strategy) addOrdersToActiveOrderBook(gridOrders []types.Order) {
activeOrderBook := s.orderExecutor.ActiveMakerOrders()
for _, gridOrder := range gridOrders {
// put the order back to the active order book so that we can receive order update
activeOrderBook.Add(gridOrder)
}
}

func (s *Strategy) setGrid(grid *Grid) {
s.mu.Lock()
s.grid = grid
Expand Down Expand Up @@ -1857,4 +1867,4 @@ func roundUpMarketQuantity(market types.Market, v fixedpoint.Value, c string) (f
}

return v.Round(prec, fixedpoint.Up), prec
}
}