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)
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)

View File

@ -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")},

View File

@ -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

View File

@ -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