From 3786fc64f120b54487dd3d1c5035a3863b62c552 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 6 Jun 2022 05:43:38 +0800 Subject: [PATCH] rename LocalActiveOrderBook to just ActiveOrderBook --- .../{active_book.go => activeorderbook.go} | 52 +++++++++---------- pkg/bbgo/localactiveorderbook_callbacks.go | 17 ------ pkg/bbgo/twap_order_executor.go | 2 +- pkg/strategy/bollgrid/strategy.go | 4 +- pkg/strategy/bollmaker/strategy.go | 2 +- pkg/strategy/ewoDgtrd/strategy.go | 2 +- pkg/strategy/factorzoo/strategy.go | 2 +- pkg/strategy/flashcrash/strategy.go | 2 +- pkg/strategy/grid/strategy.go | 2 +- pkg/strategy/pivotshort/strategy.go | 2 +- pkg/strategy/rebalance/strategy.go | 4 +- pkg/strategy/support/strategy.go | 2 +- pkg/strategy/wall/strategy.go | 4 +- pkg/strategy/xmaker/strategy.go | 2 +- 14 files changed, 41 insertions(+), 58 deletions(-) rename pkg/bbgo/{active_book.go => activeorderbook.go} (61%) delete mode 100644 pkg/bbgo/localactiveorderbook_callbacks.go diff --git a/pkg/bbgo/active_book.go b/pkg/bbgo/activeorderbook.go similarity index 61% rename from pkg/bbgo/active_book.go rename to pkg/bbgo/activeorderbook.go index 587605c16..97a1cc589 100644 --- a/pkg/bbgo/active_book.go +++ b/pkg/bbgo/activeorderbook.go @@ -12,35 +12,35 @@ import ( const CancelOrderWaitTime = 20 * time.Millisecond -// LocalActiveOrderBook manages the local active order books. -//go:generate callbackgen -type LocalActiveOrderBook -type LocalActiveOrderBook struct { +// ActiveOrderBook manages the local active order books. +//go:generate callbackgen -type ActiveOrderBook +type ActiveOrderBook struct { Symbol string orders *types.SyncOrderMap filledCallbacks []func(o types.Order) } -func NewLocalActiveOrderBook(symbol string) *LocalActiveOrderBook { - return &LocalActiveOrderBook{ +func NewLocalActiveOrderBook(symbol string) *ActiveOrderBook { + return &ActiveOrderBook{ Symbol: symbol, orders: types.NewSyncOrderMap(), } } -func (b *LocalActiveOrderBook) MarshalJSON() ([]byte, error) { +func (b *ActiveOrderBook) MarshalJSON() ([]byte, error) { orders := b.Backup() return json.Marshal(orders) } -func (b *LocalActiveOrderBook) Backup() []types.SubmitOrder { +func (b *ActiveOrderBook) Backup() []types.SubmitOrder { return b.orders.Backup() } -func (b *LocalActiveOrderBook) BindStream(stream types.Stream) { +func (b *ActiveOrderBook) BindStream(stream types.Stream) { stream.OnOrderUpdate(b.orderUpdateHandler) } -func (b *LocalActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeout time.Duration) (bool, error) { +func (b *ActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeout time.Duration) (bool, error) { numOfOrders := b.NumOfOrders() clear := numOfOrders == 0 if clear { @@ -68,8 +68,8 @@ func (b *LocalActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeo } // GracefulCancel cancels the active orders gracefully -func (b *LocalActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error { - log.Debugf("[LocalActiveOrderBook] gracefully cancelling %s orders...", b.Symbol) +func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error { + log.Debugf("[ActiveOrderBook] gracefully cancelling %s orders...", b.Symbol) startTime := time.Now() // ensure every order is cancelled @@ -83,21 +83,21 @@ func (b *LocalActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exch // since ctx might be canceled, we should use background context here if err := ex.CancelOrders(context.Background(), orders...); err != nil { - log.WithError(err).Errorf("[LocalActiveOrderBook] can not cancel %s orders", b.Symbol) + log.WithError(err).Errorf("[ActiveOrderBook] can not cancel %s orders", b.Symbol) } - log.Debugf("[LocalActiveOrderBook] waiting %s for %s orders to be cancelled...", CancelOrderWaitTime, b.Symbol) + log.Debugf("[ActiveOrderBook] waiting %s for %s orders to be cancelled...", CancelOrderWaitTime, b.Symbol) clear, err := b.waitAllClear(ctx, CancelOrderWaitTime, 5*time.Second) if clear || err != nil { break } - log.Warnf("[LocalActiveOrderBook] %d %s orders are not cancelled yet:", b.NumOfOrders(), b.Symbol) + 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("[LocalActiveOrderBook] using REStful API to verify active orders...") + log.Warnf("[ActiveOrderBook] using REStful API to verify active orders...") openOrders, err := ex.QueryOpenOrders(ctx, b.Symbol) if err != nil { log.WithError(err).Errorf("can not query %s open orders", b.Symbol) @@ -114,17 +114,17 @@ func (b *LocalActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exch } } - log.Debugf("[LocalActiveOrderBook] all %s orders are cancelled successfully in %s", b.Symbol, time.Since(startTime)) + log.Debugf("[ActiveOrderBook] all %s orders are cancelled successfully in %s", b.Symbol, time.Since(startTime)) return nil } -func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) { +func (b *ActiveOrderBook) 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("[ActiveOrderBook] received order update: %+v", order) switch order.Status { case types.OrderStatusFilled: @@ -137,7 +137,7 @@ func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) { b.Update(order) case types.OrderStatusCanceled, types.OrderStatusRejected: - log.Debugf("[LocalActiveOrderBook] order status %s, removing order %s", order.Status, order) + log.Debugf("[ActiveOrderBook] order status %s, removing order %s", order.Status, order) b.Remove(order) default: @@ -145,13 +145,13 @@ func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) { } } -func (b *LocalActiveOrderBook) Print() { +func (b *ActiveOrderBook) Print() { for _, o := range b.orders.Orders() { log.Infof("%s", o) } } -func (b *LocalActiveOrderBook) Update(orders ...types.Order) { +func (b *ActiveOrderBook) Update(orders ...types.Order) { hasSymbol := len(b.Symbol) > 0 for _, order := range orders { if hasSymbol && b.Symbol == order.Symbol { @@ -160,7 +160,7 @@ func (b *LocalActiveOrderBook) Update(orders ...types.Order) { } } -func (b *LocalActiveOrderBook) Add(orders ...types.Order) { +func (b *ActiveOrderBook) Add(orders ...types.Order) { hasSymbol := len(b.Symbol) > 0 for _, order := range orders { if hasSymbol && b.Symbol == order.Symbol { @@ -169,18 +169,18 @@ func (b *LocalActiveOrderBook) Add(orders ...types.Order) { } } -func (b *LocalActiveOrderBook) Exists(order types.Order) bool { +func (b *ActiveOrderBook) Exists(order types.Order) bool { return b.orders.Exists(order.OrderID) } -func (b *LocalActiveOrderBook) Remove(order types.Order) bool { +func (b *ActiveOrderBook) Remove(order types.Order) bool { return b.orders.Remove(order.OrderID) } -func (b *LocalActiveOrderBook) NumOfOrders() int { +func (b *ActiveOrderBook) NumOfOrders() int { return b.orders.Len() } -func (b *LocalActiveOrderBook) Orders() types.OrderSlice { +func (b *ActiveOrderBook) Orders() types.OrderSlice { return b.orders.Orders() } diff --git a/pkg/bbgo/localactiveorderbook_callbacks.go b/pkg/bbgo/localactiveorderbook_callbacks.go deleted file mode 100644 index cd58cb5d2..000000000 --- a/pkg/bbgo/localactiveorderbook_callbacks.go +++ /dev/null @@ -1,17 +0,0 @@ -// Code generated by "callbackgen -type LocalActiveOrderBook"; DO NOT EDIT. - -package bbgo - -import ( - "github.com/c9s/bbgo/pkg/types" -) - -func (b *LocalActiveOrderBook) OnFilled(cb func(o types.Order)) { - b.filledCallbacks = append(b.filledCallbacks, cb) -} - -func (b *LocalActiveOrderBook) EmitFilled(o types.Order) { - for _, cb := range b.filledCallbacks { - cb(o) - } -} diff --git a/pkg/bbgo/twap_order_executor.go b/pkg/bbgo/twap_order_executor.go index 6f8fad5a0..7aeb6fafb 100644 --- a/pkg/bbgo/twap_order_executor.go +++ b/pkg/bbgo/twap_order_executor.go @@ -36,7 +36,7 @@ type TwapExecution struct { currentPrice fixedpoint.Value activePosition fixedpoint.Value - activeMakerOrders *LocalActiveOrderBook + activeMakerOrders *ActiveOrderBook orderStore *OrderStore position *types.Position diff --git a/pkg/strategy/bollgrid/strategy.go b/pkg/strategy/bollgrid/strategy.go index c657df16b..c93c94e04 100644 --- a/pkg/strategy/bollgrid/strategy.go +++ b/pkg/strategy/bollgrid/strategy.go @@ -74,9 +74,9 @@ type Strategy struct { Quantity fixedpoint.Value `json:"quantity"` // activeOrders is the locally maintained active order book of the maker orders. - activeOrders *bbgo.LocalActiveOrderBook + activeOrders *bbgo.ActiveOrderBook - profitOrders *bbgo.LocalActiveOrderBook + profitOrders *bbgo.ActiveOrderBook orders *bbgo.OrderStore diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 8de786300..ceceab076 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -156,7 +156,7 @@ type Strategy struct { Position *types.Position `json:"position,omitempty" persistence:"position"` ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` - activeMakerOrders *bbgo.LocalActiveOrderBook + activeMakerOrders *bbgo.ActiveOrderBook orderStore *bbgo.OrderStore tradeCollector *bbgo.TradeCollector diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 4c3defc07..65725254e 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -54,7 +54,7 @@ type Strategy struct { *bbgo.Graceful bbgo.StrategyController - activeMakerOrders *bbgo.LocalActiveOrderBook + activeMakerOrders *bbgo.ActiveOrderBook orderStore *bbgo.OrderStore tradeCollector *bbgo.TradeCollector entryPrice fixedpoint.Value diff --git a/pkg/strategy/factorzoo/strategy.go b/pkg/strategy/factorzoo/strategy.go index 2cf131fdf..7c7d9bbb9 100644 --- a/pkg/strategy/factorzoo/strategy.go +++ b/pkg/strategy/factorzoo/strategy.go @@ -34,7 +34,7 @@ type Strategy struct { Position *types.Position `json:"position,omitempty"` - activeMakerOrders *bbgo.LocalActiveOrderBook + activeMakerOrders *bbgo.ActiveOrderBook orderStore *bbgo.OrderStore tradeCollector *bbgo.TradeCollector diff --git a/pkg/strategy/flashcrash/strategy.go b/pkg/strategy/flashcrash/strategy.go index dcc1cc028..35ad26da1 100644 --- a/pkg/strategy/flashcrash/strategy.go +++ b/pkg/strategy/flashcrash/strategy.go @@ -37,7 +37,7 @@ type Strategy struct { BaseQuantity fixedpoint.Value `json:"baseQuantity"` // activeOrders is the locally maintained active order book of the maker orders. - activeOrders *bbgo.LocalActiveOrderBook + activeOrders *bbgo.ActiveOrderBook // Injection fields start // -------------------------- diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index af5a2bcab..19b74f490 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -100,7 +100,7 @@ type Strategy struct { orderStore *bbgo.OrderStore // activeOrders is the locally maintained active order book of the maker orders. - activeOrders *bbgo.LocalActiveOrderBook + activeOrders *bbgo.ActiveOrderBook tradeCollector *bbgo.TradeCollector diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index b9b3a659f..caa3b8acf 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -60,7 +60,7 @@ type Strategy struct { Entry Entry Exit Exit - activeMakerOrders *bbgo.LocalActiveOrderBook + activeMakerOrders *bbgo.ActiveOrderBook orderStore *bbgo.OrderStore tradeCollector *bbgo.TradeCollector diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index 6939c48a8..484e08bc4 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -36,7 +36,7 @@ type Strategy struct { currencies []string - activeOrderBooks map[string]*bbgo.LocalActiveOrderBook + activeOrderBooks map[string]*bbgo.ActiveOrderBook } func (s *Strategy) Initialize() error { @@ -81,7 +81,7 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { } func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { - s.activeOrderBooks = make(map[string]*bbgo.LocalActiveOrderBook) + s.activeOrderBooks = make(map[string]*bbgo.ActiveOrderBook) for _, symbol := range s.getSymbols() { activeOrderBook := bbgo.NewLocalActiveOrderBook(symbol) activeOrderBook.BindStream(session.UserDataStream) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index 83768c56b..f4e843430 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -171,7 +171,7 @@ type Strategy struct { tradeCollector *bbgo.TradeCollector orderStore *bbgo.OrderStore - activeOrders *bbgo.LocalActiveOrderBook + activeOrders *bbgo.ActiveOrderBook state *State triggerEMA *indicator.EWMA diff --git a/pkg/strategy/wall/strategy.go b/pkg/strategy/wall/strategy.go index 7fed0615c..061ff3c28 100644 --- a/pkg/strategy/wall/strategy.go +++ b/pkg/strategy/wall/strategy.go @@ -67,8 +67,8 @@ type Strategy struct { Position *types.Position `json:"position,omitempty" persistence:"position"` ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` - activeAdjustmentOrders *bbgo.LocalActiveOrderBook - activeWallOrders *bbgo.LocalActiveOrderBook + activeAdjustmentOrders *bbgo.ActiveOrderBook + activeWallOrders *bbgo.ActiveOrderBook orderStore *bbgo.OrderStore tradeCollector *bbgo.TradeCollector diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 915b5f5ce..51122eed3 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -103,7 +103,7 @@ type Strategy struct { CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"` book *types.StreamOrderBook - activeMakerOrders *bbgo.LocalActiveOrderBook + activeMakerOrders *bbgo.ActiveOrderBook hedgeErrorLimiter *rate.Limiter hedgeErrorRateReservation *rate.Reservation