add book Update method

This commit is contained in:
c9s 2020-11-07 15:07:06 +08:00
parent 1e925cac6e
commit 0d8fa08171
3 changed files with 32 additions and 34 deletions

View File

@ -29,6 +29,20 @@ func (b *LocalActiveOrderBook) Print() {
}
}
func (b *LocalActiveOrderBook) Update(orders ...types.Order) {
for _, order := range orders {
switch order.Side {
case types.SideTypeBuy:
b.Bids.Update(order)
case types.SideTypeSell:
b.Asks.Update(order)
}
}
}
func (b *LocalActiveOrderBook) Add(orders ...types.Order) {
for _, order := range orders {
switch order.Side {

View File

@ -216,15 +216,14 @@ func (s *Strategy) orderUpdateHandler(order types.Order) {
switch order.Status {
case types.OrderStatusFilled:
s.WriteOff(order)
s.activeOrders.Delete(order)
case types.OrderStatusPartiallyFilled, types.OrderStatusNew:
s.activeOrders.Update(order)
case types.OrderStatusCanceled, types.OrderStatusRejected:
log.Infof("order status %s, removing %d from the active order pool...", order.Status, order.OrderID)
s.activeOrders.Delete(order)
default:
log.Infof("order status %s, updating %d to the active order pool...", order.Status, order.OrderID)
s.activeOrders.Add(order)
}
}
@ -271,32 +270,3 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return nil
}
// WriteOff writes off the filled order on the opposite side.
// This method does not write off order by order amount or order quantity.
func (s *Strategy) WriteOff(order types.Order) bool {
b := s.activeOrders
if order.Status != types.OrderStatusFilled {
return false
}
switch order.Side {
case types.SideTypeSell:
// find the filled bid to remove
if filledOrder, ok := b.Bids.AnyFilled(); ok {
b.Bids.Delete(filledOrder.OrderID)
b.Asks.Delete(order.OrderID)
return true
}
case types.SideTypeBuy:
// find the filled ask order to remove
if filledOrder, ok := b.Asks.AnyFilled(); ok {
b.Asks.Delete(filledOrder.OrderID)
b.Bids.Delete(order.OrderID)
return true
}
}
return false
}

View File

@ -9,6 +9,13 @@ func (m OrderMap) Add(o Order) {
m[o.OrderID] = o
}
// Update only updates the order when the order exists in the map
func (m OrderMap) Update(o Order) {
if _, ok := m[o.OrderID] ; ok {
m[o.OrderID] = o
}
}
func (m OrderMap) Delete(orderID uint64) {
delete(m, orderID)
}
@ -77,6 +84,13 @@ func (m *SyncOrderMap) Add(o Order) {
m.orders.Add(o)
}
func (m *SyncOrderMap) Update(o Order) {
m.Lock()
defer m.Unlock()
m.orders.Update(o)
}
func (m *SyncOrderMap) Iterate(it func(id uint64, order Order) bool) {
m.Lock()
defer m.Unlock()