Skip to content

Commit

Permalink
add dbg package for debugging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Oct 28, 2024
1 parent dbd5342 commit 17d5750
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
21 changes: 21 additions & 0 deletions pkg/dbg/orders.go
Original file line number Diff line number Diff line change
@@ -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())
}
28 changes: 7 additions & 21 deletions pkg/strategy/dca2/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/strategy/dca2/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions pkg/strategy/liquiditymaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions pkg/types/orders.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 17d5750

Please sign in to comment.