xmaker: pull out s.UseDepthPrice dependency

This commit is contained in:
c9s 2024-09-09 15:04:56 +08:00
parent 77dfe213e5
commit 90749f4873
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -203,7 +203,8 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
} }
sourceSession.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{ sourceSession.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{
Depth: s.SourceDepthLevel, // TODO: fix depth20 stream for binance
// Depth: s.SourceDepthLevel,
}) })
sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"}) sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
@ -444,6 +445,10 @@ func (s *Strategy) getInitialLayerQuantity(i int) (fixedpoint.Value, error) {
return q, nil return q, nil
} }
// getLayerPrice returns the price for the layer
// i is the layer index, starting from 0
// side is the side of the order
// sourceBook is the source order book
func (s *Strategy) getLayerPrice( func (s *Strategy) getLayerPrice(
i int, i int,
side types.SideType, side types.SideType,
@ -475,7 +480,7 @@ func (s *Strategy) getLayerPrice(
} }
} }
if s.UseDepthPrice { if requiredDepth.Sign() > 0 {
price = aggregatePrice(sourceBook.SideBook(side), requiredDepth) price = aggregatePrice(sourceBook.SideBook(side), requiredDepth)
price = price.Mul(fixedpoint.One.Add(delta)) price = price.Mul(fixedpoint.One.Add(delta))
if i > 0 { if i > 0 {
@ -796,29 +801,17 @@ func (s *Strategy) updateQuote(ctx context.Context) error {
// for maker bid orders // for maker bid orders
accumulativeBidQuantity = accumulativeBidQuantity.Add(bidQuantity) accumulativeBidQuantity = accumulativeBidQuantity.Add(bidQuantity)
requiredDepth := fixedpoint.Zero
if s.UseDepthPrice { if s.UseDepthPrice {
sideBook := sourceBook.SideBook(types.SideTypeBuy)
if s.DepthQuantity.Sign() > 0 { if s.DepthQuantity.Sign() > 0 {
if i == 0 { requiredDepth = s.DepthQuantity
bidPrice = aggregatePrice(sideBook, s.DepthQuantity)
bidPrice = bidPrice.Mul(fixedpoint.One.Sub(quote.BidMargin))
} else if i > 0 && quote.BidLayerPips.Sign() > 0 {
pips := quote.BidLayerPips.Mul(s.makerMarket.TickSize)
bidPrice = bidPrice.Sub(pips)
}
} else { } else {
bidPrice = aggregatePrice(sideBook, accumulativeBidQuantity) requiredDepth = accumulativeBidQuantity
bidPrice = bidPrice.Mul(fixedpoint.One.Sub(quote.BidMargin))
}
} else {
if i == 0 {
bidPrice = bidPrice.Mul(fixedpoint.One.Sub(quote.BidMargin))
} else if i > 0 && quote.BidLayerPips.Sign() > 0 {
pips := quote.BidLayerPips.Mul(s.makerMarket.TickSize)
bidPrice = bidPrice.Sub(pips)
} }
} }
bidPrice = s.getLayerPrice(i, types.SideTypeBuy, s.sourceBook, quote, requiredDepth)
if i == 0 { if i == 0 {
s.logger.Infof("maker best bid price %f", bidPrice.Float64()) s.logger.Infof("maker best bid price %f", bidPrice.Float64())
makerBestBidPriceMetrics.With(s.metricsLabels).Set(bidPrice.Float64()) makerBestBidPriceMetrics.With(s.metricsLabels).Set(bidPrice.Float64())
@ -857,28 +850,17 @@ func (s *Strategy) updateQuote(ctx context.Context) error {
accumulativeAskQuantity = accumulativeAskQuantity.Add(askQuantity) accumulativeAskQuantity = accumulativeAskQuantity.Add(askQuantity)
requiredDepth := fixedpoint.Zero
if s.UseDepthPrice { if s.UseDepthPrice {
if s.DepthQuantity.Sign() > 0 { if s.DepthQuantity.Sign() > 0 {
if i == 0 { requiredDepth = s.DepthQuantity
askPrice = aggregatePrice(sourceBook.SideBook(types.SideTypeSell), s.DepthQuantity)
askPrice = askPrice.Mul(fixedpoint.One.Add(quote.AskMargin))
} else if i > 0 && quote.AskLayerPips.Sign() > 0 {
pips := quote.AskLayerPips.Mul(s.makerMarket.TickSize)
askPrice = askPrice.Add(pips)
}
} else { } else {
askPrice = aggregatePrice(sourceBook.SideBook(types.SideTypeSell), accumulativeAskQuantity) requiredDepth = accumulativeAskQuantity
askPrice = askPrice.Mul(fixedpoint.One.Add(quote.AskMargin))
}
} else {
if i == 0 {
askPrice = askPrice.Mul(fixedpoint.One.Add(quote.AskMargin))
} else if i > 0 && quote.AskLayerPips.Sign() > 0 {
pips := quote.AskLayerPips.Mul(s.makerMarket.TickSize)
askPrice = askPrice.Add(pips)
} }
} }
askPrice = s.getLayerPrice(i, types.SideTypeSell, s.sourceBook, quote, requiredDepth)
if i == 0 { if i == 0 {
s.logger.Infof("maker best ask price %f", askPrice.Float64()) s.logger.Infof("maker best ask price %f", askPrice.Float64())
makerBestAskPriceMetrics.With(s.metricsLabels).Set(askPrice.Float64()) makerBestAskPriceMetrics.With(s.metricsLabels).Set(askPrice.Float64())
@ -1168,7 +1150,7 @@ func (s *Strategy) Defaults() error {
} }
func (s *Strategy) Validate() error { func (s *Strategy) Validate() error {
if s.Quantity.IsZero() || s.QuantityScale == nil { if s.Quantity.IsZero() && s.QuantityScale == nil {
return errors.New("quantity or quantityScale can not be empty") return errors.New("quantity or quantityScale can not be empty")
} }