2024-03-15 10:41:25 +00:00
|
|
|
package dca2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-15 08:25:34 +00:00
|
|
|
"fmt"
|
2024-03-15 10:41:25 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/retry"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-05-21 08:06:02 +00:00
|
|
|
type Round struct {
|
|
|
|
OpenPositionOrders []types.Order
|
|
|
|
TakeProfitOrder types.Order
|
|
|
|
}
|
|
|
|
|
2024-04-16 05:37:53 +00:00
|
|
|
type Collector struct {
|
2024-03-15 10:41:25 +00:00
|
|
|
logger *logrus.Entry
|
|
|
|
symbol string
|
|
|
|
groupID uint32
|
|
|
|
|
|
|
|
// service
|
2024-04-15 08:25:34 +00:00
|
|
|
ex types.Exchange
|
|
|
|
historyService types.ExchangeTradeHistoryService
|
|
|
|
queryService types.ExchangeOrderQueryService
|
|
|
|
tradeService types.ExchangeTradeService
|
|
|
|
queryClosedOrderDesc descendingClosedOrderQueryService
|
2024-03-15 10:41:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 05:37:53 +00:00
|
|
|
func NewCollector(logger *logrus.Entry, symbol string, groupID uint32, ex types.Exchange) *Collector {
|
2024-03-15 10:41:25 +00:00
|
|
|
historyService, ok := ex.(types.ExchangeTradeHistoryService)
|
|
|
|
if !ok {
|
|
|
|
logger.Errorf("exchange %s doesn't support ExchangeTradeHistoryService", ex.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
queryService, ok := ex.(types.ExchangeOrderQueryService)
|
|
|
|
if !ok {
|
|
|
|
logger.Errorf("exchange %s doesn't support ExchangeOrderQueryService", ex.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-15 08:25:34 +00:00
|
|
|
tradeService, ok := ex.(types.ExchangeTradeService)
|
|
|
|
if !ok {
|
|
|
|
logger.Errorf("exchange %s doesn't support ExchangeTradeService", ex.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
queryClosedOrderDesc, ok := ex.(descendingClosedOrderQueryService)
|
|
|
|
if !ok {
|
|
|
|
logger.Errorf("exchange %s doesn't support query closed orders desc", ex.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-16 05:37:53 +00:00
|
|
|
return &Collector{
|
2024-04-15 08:25:34 +00:00
|
|
|
logger: logger,
|
|
|
|
symbol: symbol,
|
|
|
|
groupID: groupID,
|
|
|
|
ex: ex,
|
|
|
|
historyService: historyService,
|
|
|
|
queryService: queryService,
|
|
|
|
tradeService: tradeService,
|
|
|
|
queryClosedOrderDesc: queryClosedOrderDesc,
|
2024-03-15 10:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 05:37:53 +00:00
|
|
|
func (rc Collector) CollectCurrentRound(ctx context.Context) (Round, error) {
|
2024-04-15 08:25:34 +00:00
|
|
|
openOrders, err := retry.QueryOpenOrdersUntilSuccessful(ctx, rc.ex, rc.symbol)
|
|
|
|
if err != nil {
|
|
|
|
return Round{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var closedOrders []types.Order
|
|
|
|
var op = func() (err2 error) {
|
|
|
|
closedOrders, err2 = rc.queryClosedOrderDesc.QueryClosedOrdersDesc(ctx, rc.symbol, recoverSinceLimit, time.Now(), 0)
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
if err := retry.GeneralBackoff(ctx, op); err != nil {
|
|
|
|
return Round{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
openPositionSide := types.SideTypeBuy
|
|
|
|
takeProfitSide := types.SideTypeSell
|
|
|
|
|
|
|
|
var allOrders []types.Order
|
|
|
|
allOrders = append(allOrders, openOrders...)
|
|
|
|
allOrders = append(allOrders, closedOrders...)
|
|
|
|
|
|
|
|
types.SortOrdersDescending(allOrders)
|
|
|
|
|
|
|
|
var currentRound Round
|
|
|
|
lastSide := takeProfitSide
|
|
|
|
for _, order := range allOrders {
|
|
|
|
// group id filter is used for debug when local running
|
|
|
|
if order.GroupID != rc.groupID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if order.Side == takeProfitSide && lastSide == openPositionSide {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch order.Side {
|
|
|
|
case openPositionSide:
|
|
|
|
currentRound.OpenPositionOrders = append(currentRound.OpenPositionOrders, order)
|
|
|
|
case takeProfitSide:
|
|
|
|
if currentRound.TakeProfitOrder.OrderID != 0 {
|
|
|
|
return currentRound, fmt.Errorf("there are two take-profit orders in one round, please check it")
|
|
|
|
}
|
|
|
|
currentRound.TakeProfitOrder = order
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSide = order.Side
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentRound, nil
|
|
|
|
}
|
|
|
|
|
2024-04-16 05:37:53 +00:00
|
|
|
func (rc *Collector) CollectFinishRounds(ctx context.Context, fromOrderID uint64) ([]Round, error) {
|
2024-03-15 10:41:25 +00:00
|
|
|
// TODO: pagination for it
|
|
|
|
// query the orders
|
|
|
|
rc.logger.Infof("query %s closed orders from order id #%d", rc.symbol, fromOrderID)
|
|
|
|
orders, err := retry.QueryClosedOrdersUntilSuccessfulLite(ctx, rc.historyService, rc.symbol, time.Time{}, time.Time{}, fromOrderID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rc.logger.Infof("there are %d closed orders from order id #%d", len(orders), fromOrderID)
|
|
|
|
|
|
|
|
var rounds []Round
|
|
|
|
var round Round
|
|
|
|
for _, order := range orders {
|
|
|
|
// skip not this strategy order
|
|
|
|
if order.GroupID != rc.groupID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch order.Side {
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
round.OpenPositionOrders = append(round.OpenPositionOrders, order)
|
|
|
|
case types.SideTypeSell:
|
2024-04-16 05:37:53 +00:00
|
|
|
if order.Status != types.OrderStatusFilled {
|
|
|
|
rc.logger.Infof("take-profit order is %s not filled, so this round is not finished. Skip it", order.Status)
|
|
|
|
continue
|
2024-03-15 10:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
round.TakeProfitOrder = order
|
|
|
|
rounds = append(rounds, round)
|
|
|
|
round = Round{}
|
|
|
|
default:
|
|
|
|
rc.logger.Errorf("there is order with unsupported side")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rounds, nil
|
|
|
|
}
|
|
|
|
|
2024-04-29 07:51:58 +00:00
|
|
|
// CollectRoundTrades collect the trades of the orders in the given round. The trades' fee are processed (feeProcessing = false)
|
2024-04-16 05:37:53 +00:00
|
|
|
func (rc *Collector) CollectRoundTrades(ctx context.Context, round Round) ([]types.Trade, error) {
|
2024-03-15 10:41:25 +00:00
|
|
|
debugRoundOrders(rc.logger, "collect round trades", round)
|
|
|
|
|
|
|
|
var roundTrades []types.Trade
|
|
|
|
var roundOrders []types.Order = round.OpenPositionOrders
|
2024-04-15 08:25:34 +00:00
|
|
|
|
|
|
|
// if the take-profit order's OrderID == 0 -> no take-profit order.
|
|
|
|
if round.TakeProfitOrder.OrderID != 0 {
|
|
|
|
roundOrders = append(roundOrders, round.TakeProfitOrder)
|
|
|
|
}
|
2024-03-15 10:41:25 +00:00
|
|
|
|
|
|
|
for _, order := range roundOrders {
|
2024-04-15 08:25:34 +00:00
|
|
|
if order.ExecutedQuantity.IsZero() {
|
2024-03-15 10:41:25 +00:00
|
|
|
rc.logger.Info("collect trads from order but no executed quantity ", order.String())
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
rc.logger.Info("collect trades from order ", order.String())
|
|
|
|
}
|
|
|
|
|
2024-04-29 07:51:58 +00:00
|
|
|
// QueryOrderTradesUntilSuccessful will query trades and their feeProcessing = false
|
|
|
|
trades, err := retry.QueryOrderTradesUntilSuccessful(ctx, rc.queryService, types.OrderQuery{
|
2024-03-15 10:41:25 +00:00
|
|
|
Symbol: order.Symbol,
|
|
|
|
OrderID: strconv.FormatUint(order.OrderID, 10),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
roundTrades = append(roundTrades, trades...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return roundTrades, nil
|
|
|
|
}
|