-
-
Notifications
You must be signed in to change notification settings - Fork 302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: add universal cancel all orders api helper #1545
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package tradingutil | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/c9s/bbgo/pkg/exchange/retry" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
type CancelAllOrdersService interface { | ||
CancelAllOrders(ctx context.Context) ([]types.Order, error) | ||
} | ||
|
||
type CancelAllOrdersBySymbolService interface { | ||
CancelOrdersBySymbol(ctx context.Context, symbol string) ([]types.Order, error) | ||
} | ||
|
||
type CancelAllOrdersByGroupIDService interface { | ||
CancelOrdersByGroupID(ctx context.Context, groupID uint32) ([]types.Order, error) | ||
} | ||
|
||
// UniversalCancelAllOrders checks if the exchange instance supports the best order cancel strategy | ||
// it tries the first interface CancelAllOrdersService that does not need any existing order information or symbol information. | ||
// | ||
// if CancelAllOrdersService is not supported, then it tries CancelAllOrdersBySymbolService which needs at least one symbol | ||
// for the cancel api request. | ||
func UniversalCancelAllOrders(ctx context.Context, exchange types.Exchange, openOrders []types.Order) error { | ||
if service, ok := exchange.(CancelAllOrdersService); ok { | ||
if _, err := service.CancelAllOrders(ctx); err == nil { | ||
return nil | ||
} else { | ||
log.WithError(err).Errorf("unable to cancel all orders") | ||
} | ||
} | ||
|
||
if len(openOrders) == 0 { | ||
return errors.New("to cancel all orders, openOrders can not be empty") | ||
} | ||
|
||
var anyErr error | ||
if service, ok := exchange.(CancelAllOrdersBySymbolService); ok { | ||
var symbols = CollectOrderSymbols(openOrders) | ||
for _, symbol := range symbols { | ||
_, err := service.CancelOrdersBySymbol(ctx, symbol) | ||
if err != nil { | ||
anyErr = err | ||
} | ||
} | ||
|
||
if anyErr == nil { | ||
return nil | ||
} | ||
} | ||
|
||
if service, ok := exchange.(CancelAllOrdersByGroupIDService); ok { | ||
var groupIds = CollectOrderGroupIds(openOrders) | ||
for _, groupId := range groupIds { | ||
if _, err := service.CancelOrdersByGroupID(ctx, groupId); err != nil { | ||
anyErr = err | ||
} | ||
} | ||
|
||
if anyErr == nil { | ||
return nil | ||
} | ||
} | ||
|
||
if anyErr != nil { | ||
return anyErr | ||
} | ||
|
||
return fmt.Errorf("unable to cancel all orders, openOrders:%+v", openOrders) | ||
} | ||
|
||
func CollectOrderGroupIds(orders []types.Order) (groupIds []uint32) { | ||
groupIdMap := map[uint32]struct{}{} | ||
for _, o := range orders { | ||
if o.GroupID > 0 { | ||
groupIdMap[o.GroupID] = struct{}{} | ||
} | ||
} | ||
|
||
for id := range groupIdMap { | ||
groupIds = append(groupIds, id) | ||
} | ||
|
||
return groupIds | ||
} | ||
|
||
func CollectOrderSymbols(orders []types.Order) (symbols []string) { | ||
symbolMap := map[string]struct{}{} | ||
for _, o := range orders { | ||
symbolMap[o.Symbol] = struct{}{} | ||
} | ||
|
||
for s := range symbolMap { | ||
symbols = append(symbols, s) | ||
} | ||
|
||
return symbols | ||
} | ||
|
||
func CollectOpenOrders(ctx context.Context, ex types.Exchange, symbols ...string) ([]types.Order, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this method get called? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be used from xtri |
||
var collectedOrders []types.Order | ||
for _, symbol := range symbols { | ||
openOrders, err := retry.QueryOpenOrdersUntilSuccessful(ctx, ex, symbol) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
collectedOrders = append(collectedOrders, openOrders...) | ||
} | ||
|
||
return collectedOrders, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this hit api rate limit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have the protection in the exchange api layer