Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [xmaker] fix aggregatePrice method #1721

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,23 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
}

func aggregatePrice(pvs types.PriceVolumeSlice, requiredQuantity fixedpoint.Value) (price fixedpoint.Value) {
q := requiredQuantity
totalAmount := fixedpoint.Zero

if len(pvs) == 0 {
price = fixedpoint.Zero
return price
} else if pvs[0].Volume.Compare(requiredQuantity) >= 0 {
return pvs[0].Price
}

sumAmount := fixedpoint.Zero
sumQty := fixedpoint.Zero
for i := 0; i < len(pvs); i++ {
pv := pvs[i]
if pv.Volume.Compare(q) >= 0 {
totalAmount = totalAmount.Add(q.Mul(pv.Price))
sumQty = sumQty.Add(pv.Volume)
sumAmount = sumAmount.Add(pv.Volume.Mul(pv.Price))
if sumQty.Compare(requiredQuantity) >= 0 {
break
}

q = q.Sub(pv.Volume)
totalAmount = totalAmount.Add(pv.Volume.Mul(pv.Price))
}

price = totalAmount.Div(requiredQuantity)
return price
return sumAmount.Div(sumQty)
}

func (s *Strategy) Initialize() error {
Expand Down Expand Up @@ -573,6 +567,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
}

if i == 0 {
s.logger.Infof("maker best bid price %f", bidPrice.Float64())
makerBestBidPriceMetrics.With(labels).Set(bidPrice.Float64())
}

Expand Down Expand Up @@ -640,6 +635,7 @@ func (s *Strategy) updateQuote(ctx context.Context) {
}

if i == 0 {
s.logger.Infof("maker best ask price %f", askPrice.Float64())
makerBestAskPriceMetrics.With(labels).Set(askPrice.Float64())
}

Expand Down
Loading