activeOrderBook: use orderMap instead of orderStore

This commit is contained in:
c9s 2023-12-06 17:55:25 +08:00
parent d14527b5cf
commit f03ac52ce5
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 14 additions and 6 deletions

View File

@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/sigchan"
"github.com/c9s/bbgo/pkg/types"
)
@ -218,11 +217,10 @@ func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange,
continue
}
openOrderStore := core.NewOrderStore(symbol)
openOrderStore.Add(openOrders...)
orderMap := types.NewOrderMap(openOrders...)
for _, o := range orders {
// if it's not on the order book (open orders), we should remove it from our local side
if !openOrderStore.Exists(o.OrderID) {
if !orderMap.Exists(o.OrderID) {
b.Remove(o)
} else {
leftOrders = append(leftOrders, o)

View File

@ -8,6 +8,14 @@ import (
// OrderMap is used for storing orders by their order id
type OrderMap map[uint64]Order
func NewOrderMap(os ...Order) OrderMap {
m := OrderMap{}
if len(os) > 0 {
m.Add(os...)
}
return m
}
func (m OrderMap) Backup() (orderForms []SubmitOrder) {
for _, order := range m {
orderForms = append(orderForms, order.Backup())
@ -17,8 +25,10 @@ func (m OrderMap) Backup() (orderForms []SubmitOrder) {
}
// Add the order the the map
func (m OrderMap) Add(o Order) {
m[o.OrderID] = o
func (m OrderMap) Add(os ...Order) {
for _, o := range os {
m[o.OrderID] = o
}
}
// Update only updates the order when the order ID exists in the map