qbtrade/pkg/strategy/rebalance/order_executor_map.go

72 lines
2.0 KiB
Go
Raw Normal View History

2024-06-27 14:42:38 +00:00
package rebalance
import (
"context"
"fmt"
"git.qtrade.icu/lychiyu/qbtrade/pkg/qbtrade"
"git.qtrade.icu/lychiyu/qbtrade/pkg/types"
)
type GeneralOrderExecutorMap map[string]*qbtrade.GeneralOrderExecutor
func NewGeneralOrderExecutorMap(session *qbtrade.ExchangeSession, strategyID string, instanceID string, positionMap PositionMap) GeneralOrderExecutorMap {
m := make(GeneralOrderExecutorMap)
for symbol, position := range positionMap {
log.Infof("creating order executor for symbol %s", symbol)
orderExecutor := qbtrade.NewGeneralOrderExecutor(session, symbol, strategyID, instanceID, position)
m[symbol] = orderExecutor
}
return m
}
func (m GeneralOrderExecutorMap) BindEnvironment(environ *qbtrade.Environment) {
for _, orderExecutor := range m {
orderExecutor.BindEnvironment(environ)
}
}
func (m GeneralOrderExecutorMap) BindProfitStats(profitStatsMap ProfitStatsMap) {
for symbol, orderExecutor := range m {
log.Infof("binding profit stats for symbol %s", symbol)
orderExecutor.BindProfitStats(profitStatsMap[symbol])
}
}
func (m GeneralOrderExecutorMap) Bind() {
for _, orderExecutor := range m {
orderExecutor.Bind()
}
}
func (m GeneralOrderExecutorMap) SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error) {
var allCreatedOrders types.OrderSlice
for _, submitOrder := range submitOrders {
log.Infof("submitting order: %+v", submitOrder)
orderExecutor, ok := m[submitOrder.Symbol]
if !ok {
return nil, fmt.Errorf("order executor not found for symbol %s", submitOrder.Symbol)
}
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil {
return nil, err
}
allCreatedOrders = append(allCreatedOrders, createdOrders...)
}
return allCreatedOrders, nil
}
func (m GeneralOrderExecutorMap) GracefulCancel(ctx context.Context) error {
for _, orderExecutor := range m {
err := orderExecutor.GracefulCancel(ctx)
if err != nil {
return err
}
}
return nil
}