mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
rename LocalActiveOrderBook to just ActiveOrderBook
This commit is contained in:
parent
1e27caa5e2
commit
3786fc64f1
|
@ -12,35 +12,35 @@ import (
|
||||||
|
|
||||||
const CancelOrderWaitTime = 20 * time.Millisecond
|
const CancelOrderWaitTime = 20 * time.Millisecond
|
||||||
|
|
||||||
// LocalActiveOrderBook manages the local active order books.
|
// ActiveOrderBook manages the local active order books.
|
||||||
//go:generate callbackgen -type LocalActiveOrderBook
|
//go:generate callbackgen -type ActiveOrderBook
|
||||||
type LocalActiveOrderBook struct {
|
type ActiveOrderBook struct {
|
||||||
Symbol string
|
Symbol string
|
||||||
orders *types.SyncOrderMap
|
orders *types.SyncOrderMap
|
||||||
filledCallbacks []func(o types.Order)
|
filledCallbacks []func(o types.Order)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalActiveOrderBook(symbol string) *LocalActiveOrderBook {
|
func NewLocalActiveOrderBook(symbol string) *ActiveOrderBook {
|
||||||
return &LocalActiveOrderBook{
|
return &ActiveOrderBook{
|
||||||
Symbol: symbol,
|
Symbol: symbol,
|
||||||
orders: types.NewSyncOrderMap(),
|
orders: types.NewSyncOrderMap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) MarshalJSON() ([]byte, error) {
|
func (b *ActiveOrderBook) MarshalJSON() ([]byte, error) {
|
||||||
orders := b.Backup()
|
orders := b.Backup()
|
||||||
return json.Marshal(orders)
|
return json.Marshal(orders)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Backup() []types.SubmitOrder {
|
func (b *ActiveOrderBook) Backup() []types.SubmitOrder {
|
||||||
return b.orders.Backup()
|
return b.orders.Backup()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) BindStream(stream types.Stream) {
|
func (b *ActiveOrderBook) BindStream(stream types.Stream) {
|
||||||
stream.OnOrderUpdate(b.orderUpdateHandler)
|
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()
|
numOfOrders := b.NumOfOrders()
|
||||||
clear := numOfOrders == 0
|
clear := numOfOrders == 0
|
||||||
if clear {
|
if clear {
|
||||||
|
@ -68,8 +68,8 @@ func (b *LocalActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeo
|
||||||
}
|
}
|
||||||
|
|
||||||
// GracefulCancel cancels the active orders gracefully
|
// GracefulCancel cancels the active orders gracefully
|
||||||
func (b *LocalActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error {
|
func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error {
|
||||||
log.Debugf("[LocalActiveOrderBook] gracefully cancelling %s orders...", b.Symbol)
|
log.Debugf("[ActiveOrderBook] gracefully cancelling %s orders...", b.Symbol)
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
// ensure every order is cancelled
|
// 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
|
// since ctx might be canceled, we should use background context here
|
||||||
if err := ex.CancelOrders(context.Background(), orders...); err != nil {
|
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)
|
clear, err := b.waitAllClear(ctx, CancelOrderWaitTime, 5*time.Second)
|
||||||
if clear || err != nil {
|
if clear || err != nil {
|
||||||
break
|
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()
|
b.Print()
|
||||||
|
|
||||||
// verify the current open orders via the RESTful API
|
// 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)
|
openOrders, err := ex.QueryOpenOrders(ctx, b.Symbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("can not query %s open orders", b.Symbol)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) {
|
func (b *ActiveOrderBook) orderUpdateHandler(order types.Order) {
|
||||||
hasSymbol := len(b.Symbol) > 0
|
hasSymbol := len(b.Symbol) > 0
|
||||||
if hasSymbol && order.Symbol != b.Symbol {
|
if hasSymbol && order.Symbol != b.Symbol {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("[LocalActiveOrderBook] received order update: %+v", order)
|
log.Debugf("[ActiveOrderBook] received order update: %+v", order)
|
||||||
|
|
||||||
switch order.Status {
|
switch order.Status {
|
||||||
case types.OrderStatusFilled:
|
case types.OrderStatusFilled:
|
||||||
|
@ -137,7 +137,7 @@ func (b *LocalActiveOrderBook) orderUpdateHandler(order types.Order) {
|
||||||
b.Update(order)
|
b.Update(order)
|
||||||
|
|
||||||
case types.OrderStatusCanceled, types.OrderStatusRejected:
|
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)
|
b.Remove(order)
|
||||||
|
|
||||||
default:
|
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() {
|
for _, o := range b.orders.Orders() {
|
||||||
log.Infof("%s", o)
|
log.Infof("%s", o)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Update(orders ...types.Order) {
|
func (b *ActiveOrderBook) Update(orders ...types.Order) {
|
||||||
hasSymbol := len(b.Symbol) > 0
|
hasSymbol := len(b.Symbol) > 0
|
||||||
for _, order := range orders {
|
for _, order := range orders {
|
||||||
if hasSymbol && b.Symbol == order.Symbol {
|
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
|
hasSymbol := len(b.Symbol) > 0
|
||||||
for _, order := range orders {
|
for _, order := range orders {
|
||||||
if hasSymbol && b.Symbol == order.Symbol {
|
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)
|
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)
|
return b.orders.Remove(order.OrderID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) NumOfOrders() int {
|
func (b *ActiveOrderBook) NumOfOrders() int {
|
||||||
return b.orders.Len()
|
return b.orders.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Orders() types.OrderSlice {
|
func (b *ActiveOrderBook) Orders() types.OrderSlice {
|
||||||
return b.orders.Orders()
|
return b.orders.Orders()
|
||||||
}
|
}
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -36,7 +36,7 @@ type TwapExecution struct {
|
||||||
currentPrice fixedpoint.Value
|
currentPrice fixedpoint.Value
|
||||||
activePosition fixedpoint.Value
|
activePosition fixedpoint.Value
|
||||||
|
|
||||||
activeMakerOrders *LocalActiveOrderBook
|
activeMakerOrders *ActiveOrderBook
|
||||||
orderStore *OrderStore
|
orderStore *OrderStore
|
||||||
position *types.Position
|
position *types.Position
|
||||||
|
|
||||||
|
|
|
@ -74,9 +74,9 @@ type Strategy struct {
|
||||||
Quantity fixedpoint.Value `json:"quantity"`
|
Quantity fixedpoint.Value `json:"quantity"`
|
||||||
|
|
||||||
// activeOrders is the locally maintained active order book of the maker orders.
|
// 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
|
orders *bbgo.OrderStore
|
||||||
|
|
||||||
|
|
|
@ -156,7 +156,7 @@ type Strategy struct {
|
||||||
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
||||||
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
||||||
|
|
||||||
activeMakerOrders *bbgo.LocalActiveOrderBook
|
activeMakerOrders *bbgo.ActiveOrderBook
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ type Strategy struct {
|
||||||
*bbgo.Graceful
|
*bbgo.Graceful
|
||||||
bbgo.StrategyController
|
bbgo.StrategyController
|
||||||
|
|
||||||
activeMakerOrders *bbgo.LocalActiveOrderBook
|
activeMakerOrders *bbgo.ActiveOrderBook
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
entryPrice fixedpoint.Value
|
entryPrice fixedpoint.Value
|
||||||
|
|
|
@ -34,7 +34,7 @@ type Strategy struct {
|
||||||
|
|
||||||
Position *types.Position `json:"position,omitempty"`
|
Position *types.Position `json:"position,omitempty"`
|
||||||
|
|
||||||
activeMakerOrders *bbgo.LocalActiveOrderBook
|
activeMakerOrders *bbgo.ActiveOrderBook
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ type Strategy struct {
|
||||||
BaseQuantity fixedpoint.Value `json:"baseQuantity"`
|
BaseQuantity fixedpoint.Value `json:"baseQuantity"`
|
||||||
|
|
||||||
// activeOrders is the locally maintained active order book of the maker orders.
|
// activeOrders is the locally maintained active order book of the maker orders.
|
||||||
activeOrders *bbgo.LocalActiveOrderBook
|
activeOrders *bbgo.ActiveOrderBook
|
||||||
|
|
||||||
// Injection fields start
|
// Injection fields start
|
||||||
// --------------------------
|
// --------------------------
|
||||||
|
|
|
@ -100,7 +100,7 @@ type Strategy struct {
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
|
|
||||||
// activeOrders is the locally maintained active order book of the maker orders.
|
// activeOrders is the locally maintained active order book of the maker orders.
|
||||||
activeOrders *bbgo.LocalActiveOrderBook
|
activeOrders *bbgo.ActiveOrderBook
|
||||||
|
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ type Strategy struct {
|
||||||
Entry Entry
|
Entry Entry
|
||||||
Exit Exit
|
Exit Exit
|
||||||
|
|
||||||
activeMakerOrders *bbgo.LocalActiveOrderBook
|
activeMakerOrders *bbgo.ActiveOrderBook
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ type Strategy struct {
|
||||||
|
|
||||||
currencies []string
|
currencies []string
|
||||||
|
|
||||||
activeOrderBooks map[string]*bbgo.LocalActiveOrderBook
|
activeOrderBooks map[string]*bbgo.ActiveOrderBook
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) Initialize() error {
|
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 {
|
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() {
|
for _, symbol := range s.getSymbols() {
|
||||||
activeOrderBook := bbgo.NewLocalActiveOrderBook(symbol)
|
activeOrderBook := bbgo.NewLocalActiveOrderBook(symbol)
|
||||||
activeOrderBook.BindStream(session.UserDataStream)
|
activeOrderBook.BindStream(session.UserDataStream)
|
||||||
|
|
|
@ -171,7 +171,7 @@ type Strategy struct {
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
activeOrders *bbgo.LocalActiveOrderBook
|
activeOrders *bbgo.ActiveOrderBook
|
||||||
state *State
|
state *State
|
||||||
|
|
||||||
triggerEMA *indicator.EWMA
|
triggerEMA *indicator.EWMA
|
||||||
|
|
|
@ -67,8 +67,8 @@ type Strategy struct {
|
||||||
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
Position *types.Position `json:"position,omitempty" persistence:"position"`
|
||||||
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
ProfitStats *types.ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
|
||||||
|
|
||||||
activeAdjustmentOrders *bbgo.LocalActiveOrderBook
|
activeAdjustmentOrders *bbgo.ActiveOrderBook
|
||||||
activeWallOrders *bbgo.LocalActiveOrderBook
|
activeWallOrders *bbgo.ActiveOrderBook
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
|
|
|
@ -103,7 +103,7 @@ type Strategy struct {
|
||||||
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"`
|
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"`
|
||||||
|
|
||||||
book *types.StreamOrderBook
|
book *types.StreamOrderBook
|
||||||
activeMakerOrders *bbgo.LocalActiveOrderBook
|
activeMakerOrders *bbgo.ActiveOrderBook
|
||||||
|
|
||||||
hedgeErrorLimiter *rate.Limiter
|
hedgeErrorLimiter *rate.Limiter
|
||||||
hedgeErrorRateReservation *rate.Reservation
|
hedgeErrorRateReservation *rate.Reservation
|
||||||
|
|
Loading…
Reference in New Issue
Block a user