Skip to content

Commit 9fee507

Browse files
committed
🔄 refactor(xalign): streamline order placement logic by removing nested if
1 parent 8244d41 commit 9fee507

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

‎pkg/strategy/xalign/strategy.go‎

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -768,48 +768,52 @@ func (s *Strategy) align(ctx context.Context, sessions bbgo.ExchangeSessionMap)
768768
if s.DryRun {
769769
return activeTransferExists
770770
}
771+
772+
// place the order immediately if one of the conditions met:
773+
// 1. the amount is small enough (<= InstantAlignAmount)
774+
// 2. interactive order is not enabled
771775
if isInstantAmount || !s.isInteractiveOrderEnabled {
772-
// place the order immediately if one of the conditions met:
773-
// 1. the amount is small enough (<= InstantAlignAmount)
774-
// 2. interactive order is not enabled
775776
createdOrder, err := selectedSession.Exchange.SubmitOrder(ctx, *submitOrder)
776777
s.onSubmittedOrderCallback(selectedSession, submitOrder, createdOrder, err)
777-
} else {
778-
if amount.Compare(s.InterativeOrderConfig.MinAmount) >= 0 {
779-
// submit the order via interactive order with slack confirmation
780-
// first we check if there is already a delayed interactive order for the same symbol and slack event ID
781-
foundDelayedOrder := false
782-
interactOrderRegistry.Range(func(k any, v any) bool {
783-
o, ok := v.(*InteractiveSubmitOrder)
784-
if ok {
785-
if o.submitOrder.Symbol == submitOrder.Symbol && o.slackEvtID == s.slackEvtID {
786-
foundDelayedOrder = true
787-
return false
788-
}
778+
// continue to the next currency
779+
continue
780+
}
781+
782+
// check if we need to place interactive order
783+
if amount.Compare(s.InterativeOrderConfig.MinAmount) >= 0 {
784+
// submit the order via interactive order with slack confirmation
785+
// first we check if there is already a delayed interactive order for the same symbol and slack event ID
786+
foundDelayedOrder := false
787+
interactOrderRegistry.Range(func(k any, v any) bool {
788+
o, ok := v.(*InteractiveSubmitOrder)
789+
if ok {
790+
if o.submitOrder.Symbol == submitOrder.Symbol && o.slackEvtID == s.slackEvtID {
791+
foundDelayedOrder = true
792+
return false
789793
}
790-
return true
791-
})
792-
if foundDelayedOrder {
793-
bbgo.Notify("found existing delayed interactive order for %s, skip placing another one", submitOrder.Symbol)
794-
continue
795794
}
796-
// place interactive order with slack confirmation
797-
// the order will be placed after confirmed in slack or after delay timeout
798-
mentions := append([]string{}, s.SlackNotifyMentions...)
799-
if s.LargeAmountAlert != nil && s.LargeAmountAlert.Slack != nil {
800-
mentions = append(mentions, s.LargeAmountAlert.Slack.Mentions...)
801-
}
802-
itOrder := NewInteractiveSubmitOrder(
803-
*submitOrder,
804-
s.InterativeOrderConfig.Delay.Duration(),
805-
mentions,
806-
s.slackEvtID,
807-
)
808-
itOrder.AsyncSubmit(ctx, selectedSession, s.onSubmittedOrderCallback)
809-
} else {
810-
createdOrder, err := selectedSession.Exchange.SubmitOrder(ctx, *submitOrder)
811-
s.onSubmittedOrderCallback(selectedSession, submitOrder, createdOrder, err)
795+
return true
796+
})
797+
if foundDelayedOrder {
798+
bbgo.Notify("found existing delayed interactive order for %s, skip placing another one", submitOrder.Symbol)
799+
continue
812800
}
801+
// place interactive order with slack confirmation
802+
// the order will be placed after confirmed in slack or after delay timeout
803+
mentions := append([]string{}, s.SlackNotifyMentions...)
804+
if s.LargeAmountAlert != nil && s.LargeAmountAlert.Slack != nil {
805+
mentions = append(mentions, s.LargeAmountAlert.Slack.Mentions...)
806+
}
807+
itOrder := NewInteractiveSubmitOrder(
808+
*submitOrder,
809+
s.InterativeOrderConfig.Delay.Duration(),
810+
mentions,
811+
s.slackEvtID,
812+
)
813+
itOrder.AsyncSubmit(ctx, selectedSession, s.onSubmittedOrderCallback)
814+
} else {
815+
createdOrder, err := selectedSession.Exchange.SubmitOrder(ctx, *submitOrder)
816+
s.onSubmittedOrderCallback(selectedSession, submitOrder, createdOrder, err)
813817
}
814818
}
815819
}

0 commit comments

Comments
 (0)