add dbg package for debugging functions

This commit is contained in:
c9s 2024-10-28 15:03:10 +08:00
parent dbd53429cd
commit 17d57502f1
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 50 additions and 22 deletions

21
pkg/dbg/orders.go Normal file
View File

@ -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())
}

View File

@ -6,10 +6,11 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/retry" "github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/pkg/errors"
) )
var recoverSinceLimit = time.Date(2024, time.January, 29, 12, 0, 0, 0, time.Local) 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 // dca stop at take-profit order stage
if len(currentRound.TakeProfitOrders) > 0 { if len(currentRound.TakeProfitOrders) > 0 {
openedOrders, cancelledOrders, filledOrders, unexpectedOrders := classifyOrders(currentRound.TakeProfitOrders) openedOrders, cancelledOrders, filledOrders, unexpectedOrders := types.ClassifyOrdersByStatus(currentRound.TakeProfitOrders)
if len(unexpectedOrders) > 0 { if len(unexpectedOrders) > 0 {
return None, fmt.Errorf("there is unexpected status in orders %+v", unexpectedOrders) 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 // collect open-position orders' status
openedOrders, cancelledOrders, filledOrders, unexpectedOrders := classifyOrders(currentRound.OpenPositionOrders) openedOrders, cancelledOrders, filledOrders, unexpectedOrders := types.ClassifyOrdersByStatus(currentRound.OpenPositionOrders)
if len(unexpectedOrders) > 0 { if len(unexpectedOrders) > 0 {
return None, fmt.Errorf("there is unexpected status of orders %+v", unexpectedOrders) 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 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 { if position == nil {
return fmt.Errorf("position is nil, please check it") return fmt.Errorf("position is nil, please check it")
} }
@ -191,20 +194,3 @@ func recoverStartTimeOfNextRound(ctx context.Context, currentRound Round, coolDo
return startTimeOfNextRound 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
}

View File

@ -165,7 +165,7 @@ func Test_classifyOrders(t *testing.T) {
types.Order{Status: types.OrderStatusCanceled}, 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, 3, len(opened))
assert.Equal(t, 4, len(cancelled)) assert.Equal(t, 4, len(cancelled))
assert.Equal(t, 2, len(filled)) assert.Equal(t, 2, len(filled))

View File

@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/dbg"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2" indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
"github.com/c9s/bbgo/pkg/strategy/common" "github.com/c9s/bbgo/pkg/strategy/common"
@ -430,6 +431,8 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
orderForms = append(orderForms, askOrders...) orderForms = append(orderForms, askOrders...)
} }
dbg.DebugSubmitOrders(s.logger, orderForms)
createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...) createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...)
if util.LogErr(err, "unable to place liquidity orders") { if util.LogErr(err, "unable to place liquidity orders") {
return return

18
pkg/types/orders.go Normal file
View File

@ -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
}