mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
add Backup method to the local active order book
This commit is contained in:
parent
46c59f5009
commit
c3996aee2b
|
@ -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) {
|
func (b *LocalActiveOrderBook) BindStream(stream types.Stream) {
|
||||||
stream.OnOrderUpdate(b.orderUpdateHandler)
|
stream.OnOrderUpdate(b.orderUpdateHandler)
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,6 +155,14 @@ type Order struct {
|
||||||
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
|
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 {
|
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)
|
return fmt.Sprintf("order %s %f/%f at %f -> %s", o.Side, o.ExecutedQuantity, o.Quantity, o.Price, o.Status)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,14 @@ import "sync"
|
||||||
// OrderMap is used for storing orders by their order id
|
// OrderMap is used for storing orders by their order id
|
||||||
type OrderMap map[uint64]Order
|
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) {
|
func (m OrderMap) Add(o Order) {
|
||||||
m[o.OrderID] = o
|
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) {
|
func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user