pull out order formatter

This commit is contained in:
c9s 2020-10-26 18:17:18 +08:00
parent a4b6a5f923
commit 4e7c1a327b
3 changed files with 42 additions and 34 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "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) return nil, errors.Errorf("exchange session %s not found", session)
} }
var formattedOrders []types.SubmitOrder formattedOrders, err := formatOrders(orders, es)
for _, order := range orders { if err != nil {
market, ok := es.Market(order.Symbol) return nil, err
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)
} }
// e.Notify(":memo: Submitting order to %s %s %s %s with quantity: %s", session, order.Symbol, order.Type, order.Side, order.QuantityString, order) // 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...) return es.Exchange.SubmitOrders(ctx, formattedOrders...)
} }
// ExchangeOrderExecutor is an order executor wrapper for single exchange instance. // ExchangeOrderExecutor is an order executor wrapper for single exchange instance.
type ExchangeOrderExecutor struct { type ExchangeOrderExecutor struct {
Notifiability Notifiability `json:"-"`
session *ExchangeSession session *ExchangeSession `json:"-"`
}
func (e *ExchangeOrderExecutor) Session() *ExchangeSession {
return e.session
} }
func (e *ExchangeOrderExecutor) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) ([]types.Order, error) { 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 { for _, order := range orders {
market, ok := e.session.Market(order.Symbol) market, ok := session.Market(order.Symbol)
if !ok { 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.Market = market
order.PriceString = market.FormatPrice(order.Price) order.PriceString = market.FormatPrice(order.Price)
order.QuantityString = market.FormatVolume(order.Quantity) order.QuantityString = market.FormatVolume(order.Quantity)
formattedOrders = append(formattedOrders, order) 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
} }

View File

@ -225,7 +225,6 @@ func (trader *Trader) ReportPnL(notifier Notifier) *PnLReporterManager {
} }
type OrderExecutor interface { type OrderExecutor interface {
Session() *ExchangeSession
SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders []types.Order, err error) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders []types.Order, err error)
} }

View File

@ -9,7 +9,6 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
) )
type SingleExchangeStrategyConfig struct { type SingleExchangeStrategyConfig struct {
@ -23,15 +22,8 @@ type PnLReporter struct {
When StringSlice `json:"when" yaml:"when"` 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 { type SymbolBasedOrderExecutor struct {
RiskControlOrderExecutor *RiskControlOrderExecutor `json:"RiskControlOrderExecutor,omitempty"` RiskControlOrderExecutor *bbgo.RiskControlOrderExecutor `json:"RiskControlOrderExecutor,omitempty"`
} }
type OrderExecutor struct { type OrderExecutor struct {