mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-24 07:45:15 +00:00
add dbg package for debugging functions
This commit is contained in:
parent
dbd53429cd
commit
17d57502f1
21
pkg/dbg/orders.go
Normal file
21
pkg/dbg/orders.go
Normal 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())
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
|
|
18
pkg/types/orders.go
Normal file
18
pkg/types/orders.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user