Merge pull request #1153 from c9s/grid2/feature/move-metrics

FEATURE: move metrics to defer funciton
This commit is contained in:
gx578007 2023-04-18 17:16:41 +08:00 committed by GitHub
commit a06ed9e5af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,14 +82,12 @@ 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)
if expectedNumOfOrders == numGridOpenOrders {
// no need to recover
// no need to recover, only need to add open orders back to active order book
s.addOrdersToActiveOrderBook(openOrdersOnGrid)
return nil
} else if expectedNumOfOrders < numGridOpenOrders {
return fmt.Errorf("amount of grid's open orders should not > amount of expected grid's orders")
@ -121,9 +122,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)