xdepthmaker: fix pricing book copy by avoiding using CopyDepth

This commit is contained in:
c9s 2023-12-11 17:59:16 +08:00
parent 9f14215ce8
commit 8c6724b264
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 41 additions and 25 deletions

View File

@ -25,7 +25,7 @@ var defaultMargin = fixedpoint.NewFromFloat(0.003)
var Two = fixedpoint.NewFromInt(2) var Two = fixedpoint.NewFromInt(2)
const priceUpdateTimeout = 30 * time.Second const priceUpdateTimeout = 5 * time.Minute
const ID = "xdepthmaker" const ID = "xdepthmaker"
@ -644,7 +644,16 @@ func (s *Strategy) generateMakerOrders(
var accumulatedAskQuantity = fixedpoint.Zero var accumulatedAskQuantity = fixedpoint.Zero
var accumulatedBidQuoteQuantity = 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 { if maxLayer == 0 || maxLayer > s.NumLayers {
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", log.WithError(err).Warnf("quote update error, %s price not updating, order book last update: %s ago",
s.Symbol, s.Symbol,
time.Since(bookLastUpdateTime)) time.Since(bookLastUpdateTime))
return
} }
if _, err := s.askPriceHeartBeat.Update(bestAsk); err != nil { 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", log.WithError(err).Warnf("quote update error, %s price not updating, order book last update: %s ago",
s.Symbol, s.Symbol,
time.Since(bookLastUpdateTime)) time.Since(bookLastUpdateTime))
return
} }
balances, err := s.MakerOrderExecutor.Session().Exchange.QueryAccountBalances(ctx) balances, err := s.MakerOrderExecutor.Session().Exchange.QueryAccountBalances(ctx)

View File

@ -45,7 +45,7 @@ func TestStrategy_generateMakerOrders(t *testing.T) {
} }
pricingBook := types.NewStreamBook("BTCUSDT") pricingBook := types.NewStreamBook("BTCUSDT")
pricingBook.OrderBook.Load(types.SliceOrderBook{ pricingBook.Load(types.SliceOrderBook{
Symbol: "BTCUSDT", Symbol: "BTCUSDT",
Bids: types.PriceVolumeSlice{ Bids: types.PriceVolumeSlice{
{Price: Number("25000.00"), Volume: Number("0.1")}, {Price: Number("25000.00"), Volume: Number("0.1")},

View File

@ -26,8 +26,9 @@ type OrderBook interface {
type MutexOrderBook struct { type MutexOrderBook struct {
sync.Mutex sync.Mutex
Symbol string Symbol string
OrderBook OrderBook
orderBook OrderBook
} }
func NewMutexOrderBook(symbol string) *MutexOrderBook { func NewMutexOrderBook(symbol string) *MutexOrderBook {
@ -39,20 +40,27 @@ func NewMutexOrderBook(symbol string) *MutexOrderBook {
return &MutexOrderBook{ return &MutexOrderBook{
Symbol: symbol, Symbol: symbol,
OrderBook: book, orderBook: book,
} }
} }
func (b *MutexOrderBook) IsValid() (ok bool, err error) { func (b *MutexOrderBook) IsValid() (ok bool, err error) {
b.Lock() b.Lock()
ok, err = b.OrderBook.IsValid() ok, err = b.orderBook.IsValid()
b.Unlock() b.Unlock()
return ok, err 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 { func (b *MutexOrderBook) LastUpdateTime() time.Time {
b.Lock() b.Lock()
t := b.OrderBook.LastUpdateTime() t := b.orderBook.LastUpdateTime()
b.Unlock() b.Unlock()
return t return t
} }
@ -60,8 +68,8 @@ func (b *MutexOrderBook) LastUpdateTime() time.Time {
func (b *MutexOrderBook) BestBidAndAsk() (bid, ask PriceVolume, ok bool) { func (b *MutexOrderBook) BestBidAndAsk() (bid, ask PriceVolume, ok bool) {
var ok1, ok2 bool var ok1, ok2 bool
b.Lock() b.Lock()
bid, ok1 = b.OrderBook.BestBid() bid, ok1 = b.orderBook.BestBid()
ask, ok2 = b.OrderBook.BestAsk() ask, ok2 = b.orderBook.BestAsk()
b.Unlock() b.Unlock()
ok = ok1 && ok2 ok = ok1 && ok2
return bid, ask, ok 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) { func (b *MutexOrderBook) BestBid() (pv PriceVolume, ok bool) {
b.Lock() b.Lock()
pv, ok = b.OrderBook.BestBid() pv, ok = b.orderBook.BestBid()
b.Unlock() b.Unlock()
return pv, ok return pv, ok
} }
func (b *MutexOrderBook) BestAsk() (pv PriceVolume, ok bool) { func (b *MutexOrderBook) BestAsk() (pv PriceVolume, ok bool) {
b.Lock() b.Lock()
pv, ok = b.OrderBook.BestAsk() pv, ok = b.orderBook.BestAsk()
b.Unlock() b.Unlock()
return pv, ok return pv, ok
} }
func (b *MutexOrderBook) Load(book SliceOrderBook) { func (b *MutexOrderBook) Load(book SliceOrderBook) {
b.Lock() b.Lock()
b.OrderBook.Load(book) b.orderBook.Load(book)
b.Unlock() b.Unlock()
} }
func (b *MutexOrderBook) Reset() { func (b *MutexOrderBook) Reset() {
b.Lock() b.Lock()
b.OrderBook.Reset() b.orderBook.Reset()
b.Unlock() b.Unlock()
} }
func (b *MutexOrderBook) CopyDepth(depth int) OrderBook { func (b *MutexOrderBook) CopyDepth(depth int) OrderBook {
b.Lock() b.Lock()
book := b.OrderBook.CopyDepth(depth) defer b.Unlock()
b.Unlock()
return book return b.orderBook.CopyDepth(depth)
} }
func (b *MutexOrderBook) Copy() OrderBook { func (b *MutexOrderBook) Copy() OrderBook {
b.Lock() b.Lock()
book := b.OrderBook.Copy() defer b.Unlock()
b.Unlock()
return book return b.orderBook.Copy()
} }
func (b *MutexOrderBook) Update(update SliceOrderBook) { func (b *MutexOrderBook) Update(update SliceOrderBook) {
b.Lock() b.Lock()
b.OrderBook.Update(update) defer b.Unlock()
b.Unlock()
b.orderBook.Update(update)
} }
type BookSignalType string type BookSignalType string

View File

@ -17,7 +17,7 @@ func (p PriceVolume) Equals(b PriceVolume) bool {
} }
func (p PriceVolume) String() string { 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 type PriceVolumeSlice []PriceVolume