From 17d57502f138f900770f4141b6c969ea0a00c6ec Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 28 Oct 2024 15:03:10 +0800 Subject: [PATCH] add dbg package for debugging functions --- pkg/dbg/orders.go | 21 +++++++++++++++++++ pkg/strategy/dca2/recover.go | 28 +++++++------------------ pkg/strategy/dca2/recover_test.go | 2 +- pkg/strategy/liquiditymaker/strategy.go | 3 +++ pkg/types/orders.go | 18 ++++++++++++++++ 5 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 pkg/dbg/orders.go create mode 100644 pkg/types/orders.go diff --git a/pkg/dbg/orders.go b/pkg/dbg/orders.go new file mode 100644 index 000000000..38c6f66a8 --- /dev/null +++ b/pkg/dbg/orders.go @@ -0,0 +1,21 @@ +package dbg + +import ( + "fmt" + "strings" + + "github.com/sirupsen/logrus" + + types2 "github.com/c9s/bbgo/pkg/types" +) + +func DebugSubmitOrders(logger logrus.FieldLogger, submitOrders []types2.SubmitOrder) { + var sb strings.Builder + sb.WriteString("SubmitOrders[\n") + for i, order := range submitOrders { + sb.WriteString(fmt.Sprintf("%3d) ", i+1) + order.String() + "\n") + } + sb.WriteString("] End of SubmitOrders") + + logger.Info(sb.String()) +} diff --git a/pkg/strategy/dca2/recover.go b/pkg/strategy/dca2/recover.go index b3c0c2093..c9889a946 100644 --- a/pkg/strategy/dca2/recover.go +++ b/pkg/strategy/dca2/recover.go @@ -6,10 +6,11 @@ import ( "strconv" "time" + "github.com/pkg/errors" + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/exchange/retry" "github.com/c9s/bbgo/pkg/types" - "github.com/pkg/errors" ) var recoverSinceLimit = time.Date(2024, time.January, 29, 12, 0, 0, 0, time.Local) @@ -65,7 +66,7 @@ func recoverState(ctx context.Context, maxOrderCount int, currentRound Round, or // dca stop at take-profit order stage if len(currentRound.TakeProfitOrders) > 0 { - openedOrders, cancelledOrders, filledOrders, unexpectedOrders := classifyOrders(currentRound.TakeProfitOrders) + openedOrders, cancelledOrders, filledOrders, unexpectedOrders := types.ClassifyOrdersByStatus(currentRound.TakeProfitOrders) if len(unexpectedOrders) > 0 { return None, fmt.Errorf("there is unexpected status in orders %+v", unexpectedOrders) @@ -96,7 +97,7 @@ func recoverState(ctx context.Context, maxOrderCount int, currentRound Round, or } // collect open-position orders' status - openedOrders, cancelledOrders, filledOrders, unexpectedOrders := classifyOrders(currentRound.OpenPositionOrders) + openedOrders, cancelledOrders, filledOrders, unexpectedOrders := types.ClassifyOrdersByStatus(currentRound.OpenPositionOrders) if len(unexpectedOrders) > 0 { return None, fmt.Errorf("there is unexpected status of orders %+v", unexpectedOrders) } @@ -124,7 +125,9 @@ func recoverState(ctx context.Context, maxOrderCount int, currentRound Round, or return OpenPositionOrdersCancelling, nil } -func recoverPosition(ctx context.Context, position *types.Position, currentRound Round, queryService types.ExchangeOrderQueryService) error { +func recoverPosition( + ctx context.Context, position *types.Position, currentRound Round, queryService types.ExchangeOrderQueryService, +) error { if position == nil { return fmt.Errorf("position is nil, please check it") } @@ -191,20 +194,3 @@ func recoverStartTimeOfNextRound(ctx context.Context, currentRound Round, coolDo return startTimeOfNextRound } - -func classifyOrders(orders []types.Order) (opened, cancelled, filled, unexpected []types.Order) { - for _, order := range orders { - switch order.Status { - case types.OrderStatusNew, types.OrderStatusPartiallyFilled: - opened = append(opened, order) - case types.OrderStatusFilled: - filled = append(filled, order) - case types.OrderStatusCanceled: - cancelled = append(cancelled, order) - default: - unexpected = append(unexpected, order) - } - } - - return opened, cancelled, filled, unexpected -} diff --git a/pkg/strategy/dca2/recover_test.go b/pkg/strategy/dca2/recover_test.go index c26205c2d..3c09937e9 100644 --- a/pkg/strategy/dca2/recover_test.go +++ b/pkg/strategy/dca2/recover_test.go @@ -165,7 +165,7 @@ func Test_classifyOrders(t *testing.T) { types.Order{Status: types.OrderStatusCanceled}, } - opened, cancelled, filled, unexpected := classifyOrders(orders) + opened, cancelled, filled, unexpected := types.ClassifyOrdersByStatus(orders) assert.Equal(t, 3, len(opened)) assert.Equal(t, 4, len(cancelled)) assert.Equal(t, 2, len(filled)) diff --git a/pkg/strategy/liquiditymaker/strategy.go b/pkg/strategy/liquiditymaker/strategy.go index 778335651..817596409 100644 --- a/pkg/strategy/liquiditymaker/strategy.go +++ b/pkg/strategy/liquiditymaker/strategy.go @@ -8,6 +8,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/c9s/bbgo/pkg/bbgo" + "github.com/c9s/bbgo/pkg/dbg" "github.com/c9s/bbgo/pkg/fixedpoint" indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2" "github.com/c9s/bbgo/pkg/strategy/common" @@ -430,6 +431,8 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { orderForms = append(orderForms, askOrders...) } + dbg.DebugSubmitOrders(s.logger, orderForms) + createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...) if util.LogErr(err, "unable to place liquidity orders") { return diff --git a/pkg/types/orders.go b/pkg/types/orders.go new file mode 100644 index 000000000..34078dd29 --- /dev/null +++ b/pkg/types/orders.go @@ -0,0 +1,18 @@ +package types + +func ClassifyOrdersByStatus(orders []Order) (opened, cancelled, filled, unexpected []Order) { + for _, order := range orders { + switch order.Status { + case OrderStatusNew, OrderStatusPartiallyFilled: + opened = append(opened, order) + case OrderStatusFilled: + filled = append(filled, order) + case OrderStatusCanceled: + cancelled = append(cancelled, order) + default: + unexpected = append(unexpected, order) + } + } + + return opened, cancelled, filled, unexpected +}