From 8c6724b2648b0659ae99a04d11288e05468f0360 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 11 Dec 2023 17:59:16 +0800 Subject: [PATCH] xdepthmaker: fix pricing book copy by avoiding using CopyDepth --- pkg/strategy/xdepthmaker/strategy.go | 15 ++++++-- pkg/strategy/xdepthmaker/strategy_test.go | 2 +- pkg/types/orderbook.go | 47 ++++++++++++++--------- pkg/types/price_volume_slice.go | 2 +- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index d6bfc2b6f..7173cec99 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -25,7 +25,7 @@ var defaultMargin = fixedpoint.NewFromFloat(0.003) var Two = fixedpoint.NewFromInt(2) -const priceUpdateTimeout = 30 * time.Second +const priceUpdateTimeout = 5 * time.Minute const ID = "xdepthmaker" @@ -644,7 +644,16 @@ func (s *Strategy) generateMakerOrders( var accumulatedAskQuantity = fixedpoint.Zero var accumulatedBidQuoteQuantity = fixedpoint.Zero - dupPricingBook := pricingBook.CopyDepth(0) + // copy the pricing book because during the generation the book data could change + dupPricingBook := pricingBook.Copy() + + log.Infof("dupPricingBook: \n\tbids: %+v \n\tasks: %+v", + dupPricingBook.SideBook(types.SideTypeBuy), + dupPricingBook.SideBook(types.SideTypeSell)) + + log.Infof("pricingBook: \n\tbids: %+v \n\tasks: %+v", + pricingBook.SideBook(types.SideTypeBuy), + pricingBook.SideBook(types.SideTypeSell)) if maxLayer == 0 || maxLayer > s.NumLayers { maxLayer = s.NumLayers @@ -831,14 +840,12 @@ func (s *Strategy) updateQuote(ctx context.Context, maxLayer int) { log.WithError(err).Warnf("quote update error, %s price not updating, order book last update: %s ago", s.Symbol, time.Since(bookLastUpdateTime)) - return } if _, err := s.askPriceHeartBeat.Update(bestAsk); err != nil { log.WithError(err).Warnf("quote update error, %s price not updating, order book last update: %s ago", s.Symbol, time.Since(bookLastUpdateTime)) - return } balances, err := s.MakerOrderExecutor.Session().Exchange.QueryAccountBalances(ctx) diff --git a/pkg/strategy/xdepthmaker/strategy_test.go b/pkg/strategy/xdepthmaker/strategy_test.go index faa3cf2ba..2df6424c0 100644 --- a/pkg/strategy/xdepthmaker/strategy_test.go +++ b/pkg/strategy/xdepthmaker/strategy_test.go @@ -45,7 +45,7 @@ func TestStrategy_generateMakerOrders(t *testing.T) { } pricingBook := types.NewStreamBook("BTCUSDT") - pricingBook.OrderBook.Load(types.SliceOrderBook{ + pricingBook.Load(types.SliceOrderBook{ Symbol: "BTCUSDT", Bids: types.PriceVolumeSlice{ {Price: Number("25000.00"), Volume: Number("0.1")}, diff --git a/pkg/types/orderbook.go b/pkg/types/orderbook.go index 3d32989c8..6fbc92ba9 100644 --- a/pkg/types/orderbook.go +++ b/pkg/types/orderbook.go @@ -26,8 +26,9 @@ type OrderBook interface { type MutexOrderBook struct { sync.Mutex - Symbol string - OrderBook OrderBook + Symbol string + + orderBook OrderBook } func NewMutexOrderBook(symbol string) *MutexOrderBook { @@ -39,20 +40,27 @@ func NewMutexOrderBook(symbol string) *MutexOrderBook { return &MutexOrderBook{ Symbol: symbol, - OrderBook: book, + orderBook: book, } } func (b *MutexOrderBook) IsValid() (ok bool, err error) { b.Lock() - ok, err = b.OrderBook.IsValid() + ok, err = b.orderBook.IsValid() b.Unlock() return ok, err } +func (b *MutexOrderBook) SideBook(sideType SideType) PriceVolumeSlice { + b.Lock() + sideBook := b.orderBook.SideBook(sideType) + b.Unlock() + return sideBook +} + func (b *MutexOrderBook) LastUpdateTime() time.Time { b.Lock() - t := b.OrderBook.LastUpdateTime() + t := b.orderBook.LastUpdateTime() b.Unlock() return t } @@ -60,8 +68,8 @@ func (b *MutexOrderBook) LastUpdateTime() time.Time { func (b *MutexOrderBook) BestBidAndAsk() (bid, ask PriceVolume, ok bool) { var ok1, ok2 bool b.Lock() - bid, ok1 = b.OrderBook.BestBid() - ask, ok2 = b.OrderBook.BestAsk() + bid, ok1 = b.orderBook.BestBid() + ask, ok2 = b.orderBook.BestAsk() b.Unlock() ok = ok1 && ok2 return bid, ask, ok @@ -69,48 +77,49 @@ func (b *MutexOrderBook) BestBidAndAsk() (bid, ask PriceVolume, ok bool) { func (b *MutexOrderBook) BestBid() (pv PriceVolume, ok bool) { b.Lock() - pv, ok = b.OrderBook.BestBid() + pv, ok = b.orderBook.BestBid() b.Unlock() return pv, ok } func (b *MutexOrderBook) BestAsk() (pv PriceVolume, ok bool) { b.Lock() - pv, ok = b.OrderBook.BestAsk() + pv, ok = b.orderBook.BestAsk() b.Unlock() return pv, ok } func (b *MutexOrderBook) Load(book SliceOrderBook) { b.Lock() - b.OrderBook.Load(book) + b.orderBook.Load(book) b.Unlock() } func (b *MutexOrderBook) Reset() { b.Lock() - b.OrderBook.Reset() + b.orderBook.Reset() b.Unlock() } func (b *MutexOrderBook) CopyDepth(depth int) OrderBook { b.Lock() - book := b.OrderBook.CopyDepth(depth) - b.Unlock() - return book + defer b.Unlock() + + return b.orderBook.CopyDepth(depth) } func (b *MutexOrderBook) Copy() OrderBook { b.Lock() - book := b.OrderBook.Copy() - b.Unlock() - return book + defer b.Unlock() + + return b.orderBook.Copy() } func (b *MutexOrderBook) Update(update SliceOrderBook) { b.Lock() - b.OrderBook.Update(update) - b.Unlock() + defer b.Unlock() + + b.orderBook.Update(update) } type BookSignalType string diff --git a/pkg/types/price_volume_slice.go b/pkg/types/price_volume_slice.go index 53fb7d913..03a6c8b00 100644 --- a/pkg/types/price_volume_slice.go +++ b/pkg/types/price_volume_slice.go @@ -17,7 +17,7 @@ func (p PriceVolume) Equals(b PriceVolume) bool { } func (p PriceVolume) String() string { - return fmt.Sprintf("PriceVolume{ price: %s, volume: %s }", p.Price.String(), p.Volume.String()) + return fmt.Sprintf("PriceVolume{ Price: %s, Volume: %s }", p.Price.String(), p.Volume.String()) } type PriceVolumeSlice []PriceVolume