2020-10-26 13:36:47 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SymbolBasedOrderExecutor struct {
|
|
|
|
BasicRiskControlOrderExecutor *BasicRiskControlOrderExecutor `json:"basic,omitempty" yaml:"basic,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RiskControlOrderExecutors struct {
|
2020-10-27 01:58:21 +00:00
|
|
|
*ExchangeOrderExecutor
|
2020-10-26 13:36:47 +00:00
|
|
|
|
|
|
|
// Symbol => Executor config
|
|
|
|
BySymbol map[string]*SymbolBasedOrderExecutor `json:"bySymbol,omitempty" yaml:"bySymbol,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *RiskControlOrderExecutors) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) ([]types.Order, error) {
|
|
|
|
var symbolOrders = make(map[string][]types.SubmitOrder, len(orders))
|
|
|
|
for _, order := range orders {
|
|
|
|
symbolOrders[order.Symbol] = append(symbolOrders[order.Symbol], order)
|
|
|
|
}
|
|
|
|
|
|
|
|
var retOrders []types.Order
|
2020-10-27 01:58:21 +00:00
|
|
|
|
2020-10-26 13:36:47 +00:00
|
|
|
for symbol, orders := range symbolOrders {
|
2020-10-27 01:58:21 +00:00
|
|
|
var err error
|
|
|
|
var retOrders2 []types.Order
|
2020-10-26 13:36:47 +00:00
|
|
|
if exec, ok := e.BySymbol[symbol]; ok && exec.BasicRiskControlOrderExecutor != nil {
|
2020-10-27 01:58:21 +00:00
|
|
|
retOrders2, err = exec.BasicRiskControlOrderExecutor.SubmitOrders(ctx, orders...)
|
|
|
|
if err != nil {
|
|
|
|
return retOrders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
retOrders2, err = e.ExchangeOrderExecutor.SubmitOrders(ctx, orders...)
|
2020-10-26 13:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return retOrders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-10-27 01:58:21 +00:00
|
|
|
retOrders = append(retOrders, retOrders2...)
|
2020-10-26 13:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retOrders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SessionBasedRiskControl struct {
|
|
|
|
OrderExecutor *RiskControlOrderExecutors `json:"orderExecutors,omitempty" yaml:"orderExecutors"`
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
func (control *SessionBasedRiskControl) SetBaseOrderExecutor(executor *ExchangeOrderExecutor) {
|
2020-10-26 13:36:47 +00:00
|
|
|
if control.OrderExecutor == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
control.OrderExecutor.ExchangeOrderExecutor = executor
|
2020-10-26 13:36:47 +00:00
|
|
|
|
|
|
|
if control.OrderExecutor.BySymbol == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, exec := range control.OrderExecutor.BySymbol {
|
|
|
|
if exec.BasicRiskControlOrderExecutor != nil {
|
2020-10-27 01:58:21 +00:00
|
|
|
exec.BasicRiskControlOrderExecutor.ExchangeOrderExecutor = executor
|
2020-10-26 13:36:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RiskControls struct {
|
|
|
|
SessionBasedRiskControl map[string]*SessionBasedRiskControl `json:"sessionBased,omitempty" yaml:"sessionBased,omitempty"`
|
|
|
|
}
|