mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
Merge pull request #672 from c9s/fix/local-active-book
fix and simplify LocalActiveOrderBook
This commit is contained in:
commit
0478993e3e
|
@ -10,22 +10,20 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const SentOrderWaitTime = 50 * time.Millisecond
|
|
||||||
const CancelOrderWaitTime = 20 * time.Millisecond
|
const CancelOrderWaitTime = 20 * time.Millisecond
|
||||||
|
|
||||||
// LocalActiveOrderBook manages the local active order books.
|
// LocalActiveOrderBook manages the local active order books.
|
||||||
//go:generate callbackgen -type LocalActiveOrderBook
|
//go:generate callbackgen -type LocalActiveOrderBook
|
||||||
type LocalActiveOrderBook struct {
|
type LocalActiveOrderBook struct {
|
||||||
Symbol string
|
Symbol string
|
||||||
Asks, Bids *types.SyncOrderMap
|
orders *types.SyncOrderMap
|
||||||
filledCallbacks []func(o types.Order)
|
filledCallbacks []func(o types.Order)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalActiveOrderBook(symbol string) *LocalActiveOrderBook {
|
func NewLocalActiveOrderBook(symbol string) *LocalActiveOrderBook {
|
||||||
return &LocalActiveOrderBook{
|
return &LocalActiveOrderBook{
|
||||||
Symbol: symbol,
|
Symbol: symbol,
|
||||||
Bids: types.NewSyncOrderMap(),
|
orders: types.NewSyncOrderMap(),
|
||||||
Asks: types.NewSyncOrderMap(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +33,7 @@ func (b *LocalActiveOrderBook) MarshalJSON() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Backup() []types.SubmitOrder {
|
func (b *LocalActiveOrderBook) Backup() []types.SubmitOrder {
|
||||||
return append(b.Bids.Backup(), b.Asks.Backup()...)
|
return b.orders.Backup()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) BindStream(stream types.Stream) {
|
func (b *LocalActiveOrderBook) BindStream(stream types.Stream) {
|
||||||
|
@ -121,6 +119,11 @@ func (b *LocalActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) {
|
func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) {
|
||||||
|
hasSymbol := len(b.Symbol) > 0
|
||||||
|
if hasSymbol && order.Symbol != b.Symbol {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("[LocalActiveOrderBook] received order update: %+v", order)
|
log.Debugf("[LocalActiveOrderBook] received order update: %+v", order)
|
||||||
|
|
||||||
switch order.Status {
|
switch order.Status {
|
||||||
|
@ -143,112 +146,41 @@ func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Print() {
|
func (b *LocalActiveOrderBook) Print() {
|
||||||
for _, o := range b.Bids.Orders() {
|
for _, o := range b.orders.Orders() {
|
||||||
log.Infof("%s bid order: %d @ %v -> %s", o.Symbol, o.OrderID, o.Price, o.Status)
|
log.Infof("%s", o)
|
||||||
}
|
|
||||||
|
|
||||||
for _, o := range b.Asks.Orders() {
|
|
||||||
log.Infof("%s ask order: %d @ %v -> %s", o.Symbol, o.OrderID, o.Price, o.Status)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Update(orders ...types.Order) {
|
func (b *LocalActiveOrderBook) Update(orders ...types.Order) {
|
||||||
|
hasSymbol := len(b.Symbol) > 0
|
||||||
for _, order := range orders {
|
for _, order := range orders {
|
||||||
switch order.Side {
|
if hasSymbol && b.Symbol == order.Symbol {
|
||||||
case types.SideTypeBuy:
|
b.orders.Update(order)
|
||||||
b.Bids.Update(order)
|
|
||||||
|
|
||||||
case types.SideTypeSell:
|
|
||||||
b.Asks.Update(order)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Add(orders ...types.Order) {
|
func (b *LocalActiveOrderBook) Add(orders ...types.Order) {
|
||||||
|
hasSymbol := len(b.Symbol) > 0
|
||||||
for _, order := range orders {
|
for _, order := range orders {
|
||||||
switch order.Side {
|
if hasSymbol && b.Symbol == order.Symbol {
|
||||||
case types.SideTypeBuy:
|
b.orders.Add(order)
|
||||||
b.Bids.Add(order)
|
|
||||||
|
|
||||||
case types.SideTypeSell:
|
|
||||||
b.Asks.Add(order)
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Errorf("unexpected order side %s, order: %#v", order.Side, order)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) NumOfBids() int {
|
|
||||||
return b.Bids.Len()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) NumOfAsks() int {
|
|
||||||
return b.Asks.Len()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Exists(order types.Order) bool {
|
func (b *LocalActiveOrderBook) Exists(order types.Order) bool {
|
||||||
|
return b.orders.Exists(order.OrderID)
|
||||||
switch order.Side {
|
|
||||||
|
|
||||||
case types.SideTypeBuy:
|
|
||||||
return b.Bids.Exists(order.OrderID)
|
|
||||||
|
|
||||||
case types.SideTypeSell:
|
|
||||||
return b.Asks.Exists(order.OrderID)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Remove(order types.Order) bool {
|
func (b *LocalActiveOrderBook) Remove(order types.Order) bool {
|
||||||
switch order.Side {
|
return b.orders.Remove(order.OrderID)
|
||||||
case types.SideTypeBuy:
|
|
||||||
return b.Bids.Remove(order.OrderID)
|
|
||||||
|
|
||||||
case types.SideTypeSell:
|
|
||||||
return b.Asks.Remove(order.OrderID)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteOff writes off the filled order on the opposite side.
|
|
||||||
// This method does not write off order by order amount or order quantity.
|
|
||||||
func (b *LocalActiveOrderBook) WriteOff(order types.Order) bool {
|
|
||||||
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.Remove(filledOrder.OrderID)
|
|
||||||
b.Asks.Remove(order.OrderID)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
case types.SideTypeBuy:
|
|
||||||
// find the filled ask order to remove
|
|
||||||
if filledOrder, ok := b.Asks.AnyFilled(); ok {
|
|
||||||
b.Asks.Remove(filledOrder.OrderID)
|
|
||||||
b.Bids.Remove(order.OrderID)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) NumOfOrders() int {
|
func (b *LocalActiveOrderBook) NumOfOrders() int {
|
||||||
return b.Asks.Len() + b.Bids.Len()
|
return b.orders.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Orders() types.OrderSlice {
|
func (b *LocalActiveOrderBook) Orders() types.OrderSlice {
|
||||||
return append(b.Asks.Orders(), b.Bids.Orders()...)
|
return b.orders.Orders()
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ func (s *Strategy) ID() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
|
func (s *Strategy) updateOrders(orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
|
||||||
if err := orderExecutor.CancelOrders(context.Background(), s.activeOrders.Bids.Orders()...); err != nil {
|
if err := s.activeOrders.GracefulCancel(context.Background(), session.Exchange); err != nil {
|
||||||
log.WithError(err).Errorf("cancel order error")
|
log.WithError(err).Errorf("cancel order error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,7 +176,7 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.activeMakerOrders.NumOfAsks() > 0 || s.activeMakerOrders.NumOfBids() > 0 {
|
if s.activeMakerOrders.NumOfOrders() > 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user