From d00a91441c518c1e5b534cc499844ce01343d9ed Mon Sep 17 00:00:00 2001 From: chiahung Date: Tue, 18 Apr 2023 15:12:50 +0800 Subject: [PATCH] FEATURE: move metrics to defer funciton --- pkg/strategy/grid2/recover.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/strategy/grid2/recover.go b/pkg/strategy/grid2/recover.go index 877cd24c2..0e9a43cf2 100644 --- a/pkg/strategy/grid2/recover.go +++ b/pkg/strategy/grid2/recover.go @@ -14,6 +14,11 @@ import ( ) func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.ExchangeSession) error { + defer func() { + s.updateGridNumOfOrdersMetricsWithLock() + s.updateOpenOrderPricesMetrics(s.orderExecutor.ActiveMakerOrders().Orders()) + }() + historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService) // if the exchange doesn't support ExchangeTradeHistoryService, do not run recover if !implemented { @@ -64,8 +69,6 @@ func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.Ex debugGrid(s.logger, s.grid, s.orderExecutor.ActiveMakerOrders()) // emit ready after recover s.EmitGridReady() - s.updateGridNumOfOrdersMetricsWithLock() - s.updateOpenOrderPricesMetrics(s.orderExecutor.ActiveMakerOrders().Orders()) return nil } @@ -79,9 +82,6 @@ func (s *Strategy) recoverWithOpenOrdersByScanningTrades(ctx context.Context, hi grid := s.newGrid() s.setGrid(grid) - // add open orders to active order book - s.addOrdersToActiveOrderBook(openOrdersOnGrid) - expectedNumOfOrders := s.GridNum - 1 numGridOpenOrders := int64(len(openOrdersOnGrid)) s.debugLog("open orders nums: %d, expected nums: %d", numGridOpenOrders, expectedNumOfOrders) @@ -121,9 +121,11 @@ func (s *Strategy) recoverWithOpenOrdersByScanningTrades(ctx context.Context, hi return errors.Wrapf(err, "verify grid with error") } - s.debugOrders("emit filled orders", filledOrders) + // 5. add open orders to active order book + s.addOrdersToActiveOrderBook(openOrdersOnGrid) - // 5. emit the filled orders + // 6. emit the filled orders + s.debugOrders("emit filled orders", filledOrders) activeOrderBook := s.orderExecutor.ActiveMakerOrders() for _, filledOrder := range filledOrders { activeOrderBook.EmitFilled(filledOrder)