diff --git a/pkg/bbgo/active_book.go b/pkg/bbgo/active_book.go index b0fd94104..662b60eb5 100644 --- a/pkg/bbgo/active_book.go +++ b/pkg/bbgo/active_book.go @@ -22,6 +22,10 @@ func NewLocalActiveOrderBook() *LocalActiveOrderBook { } } +func (b *LocalActiveOrderBook) Backup() []types.SubmitOrder { + return append(b.Bids.Backup(), b.Asks.Backup()...) +} + func (b *LocalActiveOrderBook) BindStream(stream types.Stream) { stream.OnOrderUpdate(b.orderUpdateHandler) } diff --git a/pkg/types/order.go b/pkg/types/order.go index b5f1e3c2c..6abf47b4a 100644 --- a/pkg/types/order.go +++ b/pkg/types/order.go @@ -155,6 +155,14 @@ type Order struct { IsIsolated bool `json:"isIsolated" db:"is_isolated"` } +// Backup backs up the current order quantity to a SubmitOrder object +// so that we can post the order later when we want to restore the orders. +func (o Order) Backup() SubmitOrder { + so := o.SubmitOrder + so.Quantity = o.Quantity - o.ExecutedQuantity + return so +} + func (o Order) String() string { return fmt.Sprintf("order %s %f/%f at %f -> %s", o.Side, o.ExecutedQuantity, o.Quantity, o.Price, o.Status) } diff --git a/pkg/types/ordermap.go b/pkg/types/ordermap.go index d021f2652..401bf482b 100644 --- a/pkg/types/ordermap.go +++ b/pkg/types/ordermap.go @@ -5,6 +5,14 @@ import "sync" // OrderMap is used for storing orders by their order id type OrderMap map[uint64]Order +func (m OrderMap) Backup() (orderForms []SubmitOrder) { + for _, order := range m { + orderForms = append(orderForms, order.Backup()) + } + + return orderForms +} + func (m OrderMap) Add(o Order) { m[o.OrderID] = o } @@ -70,6 +78,12 @@ func NewSyncOrderMap() *SyncOrderMap { } } +func (m *SyncOrderMap) Backup() []SubmitOrder { + m.Lock() + defer m.Unlock() + return m.orders.Backup() +} + func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) { m.Lock() defer m.Unlock()