bbgo_origin/pkg/bbgo/activeorderbook.go

298 lines
7.3 KiB
Go
Raw Normal View History

package bbgo
import (
"context"
"encoding/json"
"time"
"github.com/pkg/errors"
2020-11-10 06:19:33 +00:00
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/sigchan"
"github.com/c9s/bbgo/pkg/types"
)
const CancelOrderWaitTime = 20 * time.Millisecond
// ActiveOrderBook manages the local active order books.
//go:generate callbackgen -type ActiveOrderBook
type ActiveOrderBook struct {
2022-12-04 03:39:43 +00:00
Symbol string
orders *types.SyncOrderMap
filledCallbacks []func(o types.Order)
canceledCallbacks []func(o types.Order)
// sig is the order update signal
// this signal will be emitted when a new order is added or removed.
C sigchan.Chan
}
func NewActiveOrderBook(symbol string) *ActiveOrderBook {
return &ActiveOrderBook{
Symbol: symbol,
2022-06-05 10:12:26 +00:00
orders: types.NewSyncOrderMap(),
C: sigchan.New(1),
}
}
func (b *ActiveOrderBook) MarshalJSON() ([]byte, error) {
orders := b.Backup()
return json.Marshal(orders)
}
func (b *ActiveOrderBook) Backup() []types.SubmitOrder {
2022-06-05 10:12:26 +00:00
return b.orders.Backup()
}
func (b *ActiveOrderBook) BindStream(stream types.Stream) {
2020-11-11 15:18:53 +00:00
stream.OnOrderUpdate(b.orderUpdateHandler)
}
func (b *ActiveOrderBook) waitClear(ctx context.Context, order types.Order, waitTime, timeout time.Duration) (bool, error) {
if !b.Exists(order) {
return true, nil
}
timeoutC := time.After(timeout)
for {
select {
case <-time.After(waitTime):
case <-b.C:
}
clear := !b.Exists(order)
select {
case <-timeoutC:
return clear, nil
case <-ctx.Done():
return clear, ctx.Err()
default:
if clear {
return clear, nil
}
}
}
}
// waitAllClear waits for the order book be clear (meaning every order is removed)
// if err != nil, it's the context error.
func (b *ActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeout time.Duration) (bool, error) {
clear := b.NumOfOrders() == 0
if clear {
return clear, nil
}
timeoutC := time.After(timeout)
for {
select {
case <-time.After(waitTime):
case <-b.C:
}
// update clear flag
clear = b.NumOfOrders() == 0
select {
case <-timeoutC:
return clear, nil
case <-ctx.Done():
return clear, ctx.Err()
default:
if clear {
return clear, nil
}
}
}
}
2022-11-02 04:27:36 +00:00
// FastCancel cancels the orders without verification
// It calls the exchange cancel order api and then remove the orders from the active orderbook directly.
2022-11-02 04:25:34 +00:00
func (b *ActiveOrderBook) FastCancel(ctx context.Context, ex types.Exchange, orders ...types.Order) error {
// if no orders are given, set to cancelAll
if len(orders) == 0 {
orders = b.Orders()
} else {
// simple check on given input
for _, o := range orders {
2022-11-02 04:27:36 +00:00
if b.Symbol != "" && o.Symbol != b.Symbol {
return errors.New("[ActiveOrderBook] cancel " + b.Symbol + " orderbook with different order symbol: " + o.Symbol)
}
}
}
2022-11-02 04:27:36 +00:00
// optimize order cancel for back-testing
if IsBackTesting {
return ex.CancelOrders(context.Background(), orders...)
}
2022-11-02 04:25:34 +00:00
log.Debugf("[ActiveOrderBook] no wait cancelling %s orders...", b.Symbol)
// since ctx might be canceled, we should use background context here
if err := ex.CancelOrders(context.Background(), orders...); err != nil {
log.WithError(err).Errorf("[ActiveOrderBook] no wait can not cancel %s orders", b.Symbol)
}
2022-11-02 04:25:34 +00:00
for _, o := range orders {
b.Remove(o)
}
return nil
}
// GracefulCancel cancels the active orders gracefully
func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange, orders ...types.Order) error {
// if no orders are given, set to cancelAll
if len(orders) == 0 {
orders = b.Orders()
} else {
// simple check on given input
for _, o := range orders {
if b.Symbol != "" && o.Symbol != b.Symbol {
return errors.New("[ActiveOrderBook] cancel " + b.Symbol + " orderbook with different symbol: " + o.Symbol)
}
}
}
// optimize order cancel for back-testing
if IsBackTesting {
return ex.CancelOrders(context.Background(), orders...)
}
log.Debugf("[ActiveOrderBook] gracefully cancelling %s orders...", b.Symbol)
waitTime := CancelOrderWaitTime
startTime := time.Now()
// ensure every order is cancelled
for {
// Some orders in the variable are not created on the server side yet,
// If we cancel these orders directly, we will get an unsent order error
// We wait here for a while for server to create these orders.
// time.Sleep(SentOrderWaitTime)
// since ctx might be canceled, we should use background context here
if err := ex.CancelOrders(context.Background(), orders...); err != nil {
log.WithError(err).Errorf("[ActiveOrderBook] can not cancel %s orders", b.Symbol)
}
log.Debugf("[ActiveOrderBook] waiting %s for %s orders to be cancelled...", waitTime, b.Symbol)
clear, err := b.waitAllClear(ctx, waitTime, 5*time.Second)
if clear || err != nil {
break
}
log.Warnf("[ActiveOrderBook] %d %s orders are not cancelled yet:", b.NumOfOrders(), b.Symbol)
b.Print()
// verify the current open orders via the RESTful API
log.Warnf("[ActiveOrderBook] using REStful API to verify active orders...")
var symbols = map[string]struct{}{}
for _, order := range orders {
symbols[order.Symbol] = struct{}{}
}
var leftOrders []types.Order
for symbol := range symbols {
openOrders, err := ex.QueryOpenOrders(ctx, symbol)
if err != nil {
log.WithError(err).Errorf("can not query %s open orders", symbol)
continue
}
openOrderStore := NewOrderStore(symbol)
openOrderStore.Add(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) {
b.Remove(o)
} else {
leftOrders = append(leftOrders, o)
}
}
}
orders = leftOrders
}
log.Debugf("[ActiveOrderBook] all %s orders are cancelled successfully in %s", b.Symbol, time.Since(startTime))
return nil
}
func (b *ActiveOrderBook) orderUpdateHandler(order types.Order) {
2022-06-05 10:12:26 +00:00
hasSymbol := len(b.Symbol) > 0
if hasSymbol && order.Symbol != b.Symbol {
return
}
2020-11-11 15:18:53 +00:00
switch order.Status {
case types.OrderStatusFilled:
// make sure we have the order and we remove it
if b.Remove(order) {
b.EmitFilled(order)
}
b.C.Emit()
case types.OrderStatusPartiallyFilled:
b.Update(order)
2020-11-11 15:18:53 +00:00
case types.OrderStatusNew:
2020-11-11 15:18:53 +00:00
b.Update(order)
b.C.Emit()
2020-11-11 15:18:53 +00:00
case types.OrderStatusCanceled, types.OrderStatusRejected:
2022-12-04 03:39:43 +00:00
if order.Status == types.OrderStatusCanceled {
b.EmitCanceled(order)
}
log.Debugf("[ActiveOrderBook] order is %s, removing order %s", order.Status, order)
2020-11-11 15:18:53 +00:00
b.Remove(order)
b.C.Emit()
2021-05-09 11:44:43 +00:00
default:
log.Warnf("unhandled order status: %s", order.Status)
2020-11-11 15:18:53 +00:00
}
}
func (b *ActiveOrderBook) Print() {
2022-06-05 10:12:26 +00:00
for _, o := range b.orders.Orders() {
log.Infof("%s", o)
}
}
func (b *ActiveOrderBook) Update(orders ...types.Order) {
2022-06-05 10:12:26 +00:00
hasSymbol := len(b.Symbol) > 0
2020-11-07 07:07:06 +00:00
for _, order := range orders {
2022-06-05 10:12:26 +00:00
if hasSymbol && b.Symbol == order.Symbol {
b.orders.Update(order)
2020-11-07 07:07:06 +00:00
}
}
}
func (b *ActiveOrderBook) Add(orders ...types.Order) {
2022-06-05 10:12:26 +00:00
hasSymbol := len(b.Symbol) > 0
for _, order := range orders {
2022-06-05 10:12:26 +00:00
if hasSymbol && b.Symbol == order.Symbol {
b.orders.Add(order)
}
}
}
func (b *ActiveOrderBook) Exists(order types.Order) bool {
2022-06-05 10:12:26 +00:00
return b.orders.Exists(order.OrderID)
2021-05-22 09:44:07 +00:00
}
func (b *ActiveOrderBook) Remove(order types.Order) bool {
2022-06-05 10:12:26 +00:00
return b.orders.Remove(order.OrderID)
}
func (b *ActiveOrderBook) NumOfOrders() int {
2022-06-05 10:12:26 +00:00
return b.orders.Len()
2021-05-13 11:41:05 +00:00
}
func (b *ActiveOrderBook) Orders() types.OrderSlice {
2022-06-05 10:12:26 +00:00
return b.orders.Orders()
}