From 4e7c1a327b2b3c2bb1798050e4e3f421d1f38fcc Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 26 Oct 2020 18:17:18 +0800 Subject: [PATCH] pull out order formatter --- pkg/bbgo/order_execution.go | 65 +++++++++++++++++++++++-------------- pkg/bbgo/trader.go | 1 - pkg/config/loader.go | 10 +----- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/pkg/bbgo/order_execution.go b/pkg/bbgo/order_execution.go index 44a206cd8..63709e73c 100644 --- a/pkg/bbgo/order_execution.go +++ b/pkg/bbgo/order_execution.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" + "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" ) @@ -20,50 +21,66 @@ func (e *ExchangeOrderExecutionRouter) SubmitOrdersTo(ctx context.Context, sessi return nil, errors.Errorf("exchange session %s not found", session) } - var formattedOrders []types.SubmitOrder - for _, order := range orders { - market, ok := es.Market(order.Symbol) - if !ok { - return nil, errors.Errorf("market is not defined: %s", order.Symbol) - } - - order.Market = market - order.PriceString = market.FormatPrice(order.Price) - order.QuantityString = market.FormatVolume(order.Quantity) - formattedOrders = append(formattedOrders, order) + formattedOrders, err := formatOrders(orders, es) + if err != nil { + return nil, err } // e.Notify(":memo: Submitting order to %s %s %s %s with quantity: %s", session, order.Symbol, order.Type, order.Side, order.QuantityString, order) - return es.Exchange.SubmitOrders(ctx, formattedOrders...) } // ExchangeOrderExecutor is an order executor wrapper for single exchange instance. type ExchangeOrderExecutor struct { - Notifiability + Notifiability `json:"-"` - session *ExchangeSession -} - -func (e *ExchangeOrderExecutor) Session() *ExchangeSession { - return e.session + session *ExchangeSession `json:"-"` } func (e *ExchangeOrderExecutor) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) ([]types.Order, error) { - var formattedOrders []types.SubmitOrder + formattedOrders, err := formatOrders(orders, e.session) + if err != nil { + return nil, err + } + + // e.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order) + + return e.session.Exchange.SubmitOrders(ctx, formattedOrders...) +} + +type RiskControlOrderExecutor struct { + Notifiability `json:"-"` + + MinQuoteBalance fixedpoint.Value `json:"minQuoteBalance,omitempty"` + MaxAssetBalance fixedpoint.Value `json:"maxBaseAssetBalance,omitempty"` + MinAssetBalance fixedpoint.Value `json:"minBaseAssetBalance,omitempty"` + MaxOrderAmount fixedpoint.Value `json:"maxOrderAmount,omitempty"` + + session *ExchangeSession `json:"-"` +} + +func (e *RiskControlOrderExecutor) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) ([]types.Order, error) { + formattedOrders, err := formatOrders(orders, e.session) + if err != nil { + return nil, err + } + + // e.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order) + return e.session.Exchange.SubmitOrders(ctx, formattedOrders...) +} + +func formatOrders(orders []types.SubmitOrder, session *ExchangeSession) (formattedOrders []types.SubmitOrder, err error) { for _, order := range orders { - market, ok := e.session.Market(order.Symbol) + market, ok := session.Market(order.Symbol) if !ok { - return nil, errors.Errorf("market is not defined: %s", order.Symbol) + return formattedOrders, errors.Errorf("market is not defined: %s", order.Symbol) } order.Market = market order.PriceString = market.FormatPrice(order.Price) order.QuantityString = market.FormatVolume(order.Quantity) formattedOrders = append(formattedOrders, order) - - // e.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order) } - return e.session.Exchange.SubmitOrders(ctx, formattedOrders...) + return formattedOrders, err } diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 2ac70a220..9b5ce1cf2 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -225,7 +225,6 @@ func (trader *Trader) ReportPnL(notifier Notifier) *PnLReporterManager { } type OrderExecutor interface { - Session() *ExchangeSession SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders []types.Order, err error) } diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 72f4b2b15..d3637cd61 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -9,7 +9,6 @@ import ( "gopkg.in/yaml.v3" "github.com/c9s/bbgo/pkg/bbgo" - "github.com/c9s/bbgo/pkg/fixedpoint" ) type SingleExchangeStrategyConfig struct { @@ -23,15 +22,8 @@ type PnLReporter struct { When StringSlice `json:"when" yaml:"when"` } -type RiskControlOrderExecutor struct { - MinQuoteBalance fixedpoint.Value `json:"minQuoteBalance,omitempty"` - MaxAssetBalance fixedpoint.Value `json:"maxBaseAssetBalance,omitempty"` - MinAssetBalance fixedpoint.Value `json:"minBaseAssetBalance,omitempty"` - MaxOrderAmount fixedpoint.Value `json:"maxOrderAmount,omitempty"` -} - type SymbolBasedOrderExecutor struct { - RiskControlOrderExecutor *RiskControlOrderExecutor `json:"RiskControlOrderExecutor,omitempty"` + RiskControlOrderExecutor *bbgo.RiskControlOrderExecutor `json:"RiskControlOrderExecutor,omitempty"` } type OrderExecutor struct {