From 9b9d7455ec2b7198041dc29fdf136624c3c54a03 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 24 Apr 2023 16:02:03 +0800 Subject: [PATCH] bbgo: move Fast* methods to the FastOrderExecutor --- pkg/bbgo/order_executor_fast.go | 67 ++++++++++++++++++++++++++++++ pkg/bbgo/order_executor_general.go | 45 -------------------- 2 files changed, 67 insertions(+), 45 deletions(-) create mode 100644 pkg/bbgo/order_executor_fast.go diff --git a/pkg/bbgo/order_executor_fast.go b/pkg/bbgo/order_executor_fast.go new file mode 100644 index 000000000..ef8d40762 --- /dev/null +++ b/pkg/bbgo/order_executor_fast.go @@ -0,0 +1,67 @@ +package bbgo + +import ( + "context" + + "github.com/pkg/errors" + + "github.com/c9s/bbgo/pkg/types" +) + +// FastOrderExecutor provides shorter submit order / cancel order round-trip time +// for strategies that need to response more faster, e.g. 1s kline or market trades related strategies. +type FastOrderExecutor struct { + *GeneralOrderExecutor +} + +func NewFastOrderExecutor(session *ExchangeSession, symbol, strategy, strategyInstanceID string, position *types.Position) *FastOrderExecutor { + oe := NewGeneralOrderExecutor(session, symbol, strategy, strategyInstanceID, position) + return &FastOrderExecutor{ + GeneralOrderExecutor: oe, + } +} + +// SubmitOrders sends []types.SubmitOrder directly to the exchange without blocking wait on the status update. +// This is a faster version of GeneralOrderExecutor.SubmitOrders(). Created orders will be consumed in newly created goroutine (in non-backteset session). +// @param ctx: golang context type. +// @param submitOrders: Lists of types.SubmitOrder to be sent to the exchange. +// @return *types.SubmitOrder: SubmitOrder with calculated quantity and price. +// @return error: Error message. +func (e *FastOrderExecutor) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) { + formattedOrders, err := e.session.FormatOrders(submitOrders) + if err != nil { + return nil, err + } + + createdOrders, errIdx, err := BatchPlaceOrder(ctx, e.session.Exchange, nil, formattedOrders...) + if len(errIdx) > 0 { + return nil, err + } + + if IsBackTesting { + e.orderStore.Add(createdOrders...) + e.activeMakerOrders.Add(createdOrders...) + e.tradeCollector.Process() + } else { + go func() { + e.orderStore.Add(createdOrders...) + e.activeMakerOrders.Add(createdOrders...) + e.tradeCollector.Process() + }() + } + return createdOrders, err + +} + +// Cancel cancels all active maker orders if orders is not given, otherwise cancel the given orders +func (e *FastOrderExecutor) Cancel(ctx context.Context, orders ...types.Order) error { + if e.activeMakerOrders.NumOfOrders() == 0 { + return nil + } + + if err := e.activeMakerOrders.FastCancel(ctx, e.session.Exchange, orders...); err != nil { + return errors.Wrap(err, "fast cancel order error") + } + + return nil +} diff --git a/pkg/bbgo/order_executor_general.go b/pkg/bbgo/order_executor_general.go index a845ef499..8faaaaa1f 100644 --- a/pkg/bbgo/order_executor_general.go +++ b/pkg/bbgo/order_executor_general.go @@ -190,38 +190,6 @@ func (e *GeneralOrderExecutor) CancelOrders(ctx context.Context, orders ...types return err } -// FastSubmitOrders send []types.SubmitOrder directly to the exchange without blocking wait on the status update. -// This is a faster version of SubmitOrders(). Created orders will be consumed in newly created goroutine (in non-backteset session). -// @param ctx: golang context type. -// @param submitOrders: Lists of types.SubmitOrder to be sent to the exchange. -// @return *types.SubmitOrder: SubmitOrder with calculated quantity and price. -// @return error: Error message. -func (e *GeneralOrderExecutor) FastSubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) { - formattedOrders, err := e.session.FormatOrders(submitOrders) - if err != nil { - return nil, err - } - - createdOrders, errIdx, err := BatchPlaceOrder(ctx, e.session.Exchange, nil, formattedOrders...) - if len(errIdx) > 0 { - return nil, err - } - - if IsBackTesting { - e.orderStore.Add(createdOrders...) - e.activeMakerOrders.Add(createdOrders...) - e.tradeCollector.Process() - } else { - go func() { - e.orderStore.Add(createdOrders...) - e.activeMakerOrders.Add(createdOrders...) - e.tradeCollector.Process() - }() - } - return createdOrders, err - -} - func (e *GeneralOrderExecutor) SetLogger(logger log.FieldLogger) { e.logger = logger } @@ -467,19 +435,6 @@ func (e *GeneralOrderExecutor) GracefulCancel(ctx context.Context, orders ...typ return nil } -// FastCancel cancels all active maker orders if orders is not given, otherwise cancel the given orders -func (e *GeneralOrderExecutor) FastCancel(ctx context.Context, orders ...types.Order) error { - if e.activeMakerOrders.NumOfOrders() == 0 { - return nil - } - - if err := e.activeMakerOrders.FastCancel(ctx, e.session.Exchange, orders...); err != nil { - return errors.Wrap(err, "fast cancel order error") - } - - return nil -} - // ClosePosition closes the current position by a percentage. // percentage 0.1 means close 10% position // tag is the order tag you want to attach, you may pass multiple tags, the tags will be combined into one tag string by commas.