Skip to content

Commit

Permalink
FEATURE: [dca2] change state recovery logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kbearXD committed May 22, 2024
1 parent 5f1ece2 commit c2e19ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
14 changes: 5 additions & 9 deletions pkg/strategy/dca2/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ func recoverState(ctx context.Context, maxOrderCount int, currentRound Round, or
}
}

// all open-position orders are still not filled -> OpenPositionReady
if filledCnt == 0 && cancelledCnt == 0 {
// no order is filled -> OpenPositionReady
if filledCnt == 0 {
return OpenPositionReady, nil
}

// there are at least one open-position orders filled
if filledCnt > 0 && cancelledCnt == 0 {
if cancelledCnt == 0 {
if openedCnt > 0 {
return OpenPositionOrderFilled, nil
} else {
Expand All @@ -128,12 +128,8 @@ func recoverState(ctx context.Context, maxOrderCount int, currentRound Round, or
}
}

// there are at last one open-position orders cancelled ->
if cancelledCnt > 0 {
return OpenPositionOrdersCancelling, nil
}

return None, fmt.Errorf("unexpected order status combination (opened, filled, cancelled) = (%d, %d, %d)", openedCnt, filledCnt, cancelledCnt)
// there are at last one open-position orders cancelled and at least one filled order -> open position order cancelling
return OpenPositionOrdersCancelling, nil
}

func recoverPosition(ctx context.Context, position *types.Position, currentRound Round, queryService types.ExchangeOrderQueryService) error {
Expand Down
21 changes: 21 additions & 0 deletions pkg/strategy/dca2/take_profit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ func (s *Strategy) placeTakeProfitOrders(ctx context.Context) error {
s.logger.Infof("position of this round before place the take-profit order: %s", roundPosition.String())

order := generateTakeProfitOrder(s.Market, s.TakeProfitRatio, roundPosition, s.OrderGroupID)

// verify the volume of order
bals, err := s.ExchangeSession.Exchange.QueryAccountBalances(ctx)
if err != nil {
return errors.Wrapf(err, "failed to query balance to verify")
}

bal, exist := bals[s.Market.BaseCurrency]
if !exist {
return fmt.Errorf("there is no %s in the balances %+v", s.Market.BaseCurrency, bals)
}

QuantityDiff := bal.Available.Sub(order.Quantity)
if QuantityDiff.Sign() < 0 {
return fmt.Errorf("the balance (%s) is not enough for the order (%s)", bal.Available.String(), order.Quantity.String())
}

if QuantityDiff.Compare(s.Market.MinQuantity) > 0 {
s.logger.Warnf("the diff between balance (%s) and the take-profit order (%s) is larger than min quantity %s", bal.Available.String(), order.Quantity.String(), s.Market.MinQuantity.String())
}

createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, order)
if err != nil {
return err
Expand Down

0 comments on commit c2e19ab

Please sign in to comment.