forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_executor_map.go
71 lines (58 loc) · 1.92 KB
/
order_executor_map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package rebalance
import (
"context"
"fmt"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
)
type GeneralOrderExecutorMap map[string]*bbgo.GeneralOrderExecutor
func NewGeneralOrderExecutorMap(session *bbgo.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 := bbgo.NewGeneralOrderExecutor(session, symbol, strategyID, instanceID, position)
m[symbol] = orderExecutor
}
return m
}
func (m GeneralOrderExecutorMap) BindEnvironment(environ *bbgo.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
}