bbgo: move Fast* methods to the FastOrderExecutor

This commit is contained in:
c9s 2023-04-24 16:02:03 +08:00
parent 7aa673c673
commit 9b9d7455ec
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 67 additions and 45 deletions

View File

@ -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
}

View File

@ -190,38 +190,6 @@ func (e *GeneralOrderExecutor) CancelOrders(ctx context.Context, orders ...types
return err 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) { func (e *GeneralOrderExecutor) SetLogger(logger log.FieldLogger) {
e.logger = logger e.logger = logger
} }
@ -467,19 +435,6 @@ func (e *GeneralOrderExecutor) GracefulCancel(ctx context.Context, orders ...typ
return nil 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. // ClosePosition closes the current position by a percentage.
// percentage 0.1 means close 10% position // 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. // 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.