Merge pull request #1118 from c9s/feature/grids/recover-from-trades

MINOR: use Debug config for debug log
This commit is contained in:
Yo-An Lin 2023-03-16 16:55:17 +08:00 committed by GitHub
commit 035735f95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 10 deletions

View File

@ -1,6 +1,9 @@
package grid2 package grid2
import ( import (
"fmt"
"strings"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -32,3 +35,14 @@ func (m PinOrderMap) SyncOrderMap() *types.SyncOrderMap {
return orderMap return orderMap
} }
func (m PinOrderMap) String() string {
var sb strings.Builder
sb.WriteString("================== PIN ORDER MAP ==================\n")
for pin, order := range m {
sb.WriteString(fmt.Sprintf("%s -> %s\n", pin, order.String()))
}
sb.WriteString("================== END OF PIN ORDER MAP ==================\n")
return sb.String()
}

View File

@ -1217,6 +1217,13 @@ func (s *Strategy) debugOrders(desc string, orders []types.Order) {
s.logger.Infof(sb.String()) s.logger.Infof(sb.String())
} }
func (s *Strategy) debugLog(format string, args ...interface{}) {
if !s.Debug {
return
}
s.logger.Infof(format, args...)
}
func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoint.Value) ([]types.SubmitOrder, error) { func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoint.Value) ([]types.SubmitOrder, error) {
var pins = s.grid.Pins var pins = s.grid.Pins
var usedBase = fixedpoint.Zero var usedBase = fixedpoint.Zero
@ -1400,7 +1407,7 @@ func (s *Strategy) recoverGridWithOpenOrdersByScanningTrades(ctx context.Context
expectedOrderNums := s.GridNum - 1 expectedOrderNums := s.GridNum - 1
openOrdersOnGridNums := int64(len(openOrdersOnGrid)) openOrdersOnGridNums := int64(len(openOrdersOnGrid))
s.logger.Debugf("open orders nums: %d, expected nums: %d", openOrdersOnGridNums, expectedOrderNums) s.debugLog("open orders nums: %d, expected nums: %d", openOrdersOnGridNums, expectedOrderNums)
if expectedOrderNums == openOrdersOnGridNums { if expectedOrderNums == openOrdersOnGridNums {
// no need to recover // no need to recover
return nil return nil
@ -1458,8 +1465,8 @@ func (s *Strategy) recoverGridWithOpenOrdersByScanningTrades(ctx context.Context
} }
func (s *Strategy) verifyFilledGrid(pins []Pin, pinOrders PinOrderMap, filledOrders []types.Order) error { func (s *Strategy) verifyFilledGrid(pins []Pin, pinOrders PinOrderMap, filledOrders []types.Order) error {
s.logger.Debugf("pins: %+v", pins) s.debugLog("pins: %+v", pins)
s.logger.Debugf("open pin orders: %+v", pinOrders) s.debugLog("open pin orders:\n%s", pinOrders.String())
s.debugOrders("filled orders", filledOrders) s.debugOrders("filled orders", filledOrders)
for _, filledOrder := range filledOrders { for _, filledOrder := range filledOrders {
@ -1473,7 +1480,7 @@ func (s *Strategy) verifyFilledGrid(pins []Pin, pinOrders PinOrderMap, filledOrd
} }
} }
s.logger.Debugf("filled pin orders: %+v", pinOrders) s.debugLog("filled pin orders:\n%+v", pinOrders.String())
side := types.SideTypeBuy side := types.SideTypeBuy
for _, pin := range pins { for _, pin := range pins {
@ -1557,10 +1564,10 @@ func (s *Strategy) buildFilledPinOrderMapFromTrades(ctx context.Context, history
return nil, errors.Wrapf(err, "failed to query trades to recover the grid with open orders") return nil, errors.Wrapf(err, "failed to query trades to recover the grid with open orders")
} }
s.logger.Debugf("QueryTrades return %d trades", len(trades)) s.debugLog("QueryTrades return %d trades", len(trades))
for _, trade := range trades { for _, trade := range trades {
s.logger.Debugf(trade.String()) s.debugLog(trade.String())
if existedOrders.Exists(trade.OrderID) { if existedOrders.Exists(trade.OrderID) {
// already queries, skip // already queries, skip
continue continue
@ -1574,7 +1581,7 @@ func (s *Strategy) buildFilledPinOrderMapFromTrades(ctx context.Context, history
return nil, errors.Wrapf(err, "failed to query order by trade") return nil, errors.Wrapf(err, "failed to query order by trade")
} }
s.logger.Debugf("%s (group_id: %d)", order.String(), order.GroupID) s.debugLog("%s (group_id: %d)", order.String(), order.GroupID)
// avoid query this order again // avoid query this order again
existedOrders.Add(*order) existedOrders.Add(*order)
@ -2148,11 +2155,11 @@ func (s *Strategy) startProcess(ctx context.Context, session *bbgo.ExchangeSessi
func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSession) error { func (s *Strategy) recoverGrid(ctx context.Context, session *bbgo.ExchangeSession) error {
if s.RecoverGridByScanningTrades { if s.RecoverGridByScanningTrades {
s.logger.Debugf("recover grid by scanning trades") s.debugLog("recover grid by scanning trades")
return s.recoverGridByScanningTrades(ctx, session) return s.recoverGridByScanningTrades(ctx, session)
} }
s.logger.Debugf("recover grid by scanning orders") s.debugLog("recover grid by scanning orders")
return s.recoverGridByScanningOrders(ctx, session) return s.recoverGridByScanningOrders(ctx, session)
} }
@ -2197,7 +2204,7 @@ func (s *Strategy) recoverGridByScanningTrades(ctx context.Context, session *bbg
s.logger.Infof("found %d open orders left on the %s order book", len(openOrders), s.Symbol) s.logger.Infof("found %d open orders left on the %s order book", len(openOrders), s.Symbol)
s.logger.Debugf("recover grid with group id: %d", s.OrderGroupID) s.debugLog("recover grid with group id: %d", s.OrderGroupID)
// filter out the order with the group id belongs to this grid // filter out the order with the group id belongs to this grid
var openOrdersOnGrid []types.Order var openOrdersOnGrid []types.Order
for _, order := range openOrders { for _, order := range openOrders {