FEATURE: move metrics to defer funciton

This commit is contained in:
chiahung 2023-04-18 15:12:50 +08:00
parent 68f54c032a
commit d00a91441c

View File

@ -14,6 +14,11 @@ import (
) )
func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.ExchangeSession) error { 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) historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
// if the exchange doesn't support ExchangeTradeHistoryService, do not run recover // if the exchange doesn't support ExchangeTradeHistoryService, do not run recover
if !implemented { if !implemented {
@ -64,8 +69,6 @@ func (s *Strategy) recoverByScanningTrades(ctx context.Context, session *bbgo.Ex
debugGrid(s.logger, s.grid, s.orderExecutor.ActiveMakerOrders()) debugGrid(s.logger, s.grid, s.orderExecutor.ActiveMakerOrders())
// emit ready after recover // emit ready after recover
s.EmitGridReady() s.EmitGridReady()
s.updateGridNumOfOrdersMetricsWithLock()
s.updateOpenOrderPricesMetrics(s.orderExecutor.ActiveMakerOrders().Orders())
return nil return nil
} }
@ -79,9 +82,6 @@ func (s *Strategy) recoverWithOpenOrdersByScanningTrades(ctx context.Context, hi
grid := s.newGrid() grid := s.newGrid()
s.setGrid(grid) s.setGrid(grid)
// add open orders to active order book
s.addOrdersToActiveOrderBook(openOrdersOnGrid)
expectedNumOfOrders := s.GridNum - 1 expectedNumOfOrders := s.GridNum - 1
numGridOpenOrders := int64(len(openOrdersOnGrid)) numGridOpenOrders := int64(len(openOrdersOnGrid))
s.debugLog("open orders nums: %d, expected nums: %d", numGridOpenOrders, expectedNumOfOrders) 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") 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() activeOrderBook := s.orderExecutor.ActiveMakerOrders()
for _, filledOrder := range filledOrders { for _, filledOrder := range filledOrders {
activeOrderBook.EmitFilled(filledOrder) activeOrderBook.EmitFilled(filledOrder)