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

Refactor: add average depth price method #1642

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions pkg/exchange/max/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,11 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (cr
req.Market(toLocalSymbol(o.Symbol)).
Side(toLocalSideType(o.Side)).
Volume(quantityString).
OrderType(orderType).
ClientOrderID(clientOrderID)
OrderType(orderType)

if clientOrderID != "" {
req.ClientOrderID(clientOrderID)
}

if o.GroupID > 0 {
req.GroupID(strconv.FormatUint(uint64(o.GroupID%math.MaxInt32), 10))
Expand Down
30 changes: 0 additions & 30 deletions pkg/strategy/xdepthmaker/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,2 @@
package xdepthmaker

import (
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

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
}

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

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

price = totalAmount.Div(requiredQuantity.Sub(q))
return price
}
25 changes: 1 addition & 24 deletions pkg/strategy/xdepthmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,7 @@ func (s *Strategy) generateMakerOrders(

log.Infof("side: %s required depth: %f, pvs: %+v", side, requiredDepth.Float64(), pvs)

depthPrice, err := averageDepthPrice(pvs)
if err != nil {
log.WithError(err).Errorf("error aggregating depth price")
continue
}
depthPrice := pvs.AverageDepthPriceByQuote(fixedpoint.Zero, 0)

switch side {
case types.SideTypeBuy:
Expand Down Expand Up @@ -923,25 +919,6 @@ func selectSessions2(
return s1, s2, nil
}

func averageDepthPrice(pvs types.PriceVolumeSlice) (price fixedpoint.Value, err error) {
if len(pvs) == 0 {
return fixedpoint.Zero, fmt.Errorf("empty pv slice")
}

totalQuoteAmount := fixedpoint.Zero
totalQuantity := fixedpoint.Zero

for i := 0; i < len(pvs); i++ {
pv := pvs[i]
quoteAmount := fixedpoint.Mul(pv.Volume, pv.Price)
totalQuoteAmount = totalQuoteAmount.Add(quoteAmount)
totalQuantity = totalQuantity.Add(pv.Volume)
}

price = totalQuoteAmount.Div(totalQuantity)
return price, nil
}

func min(a, b int) int {
if a < b {
return a
Expand Down
53 changes: 53 additions & 0 deletions pkg/types/price_volume_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,56 @@ func ParsePriceVolumeSliceJSON(b []byte) (slice PriceVolumeSlice, err error) {

return slice, nil
}

func (slice PriceVolumeSlice) AverageDepthPriceByQuote(requiredDepthInQuote fixedpoint.Value, maxLevel int) fixedpoint.Value {
if len(slice) == 0 {
return fixedpoint.Zero
}

totalQuoteAmount := fixedpoint.Zero
totalQuantity := fixedpoint.Zero

l := len(slice)
if maxLevel > 0 && l > maxLevel {
l = maxLevel
}

for i := 0; i < l; i++ {
pv := slice[i]
quoteAmount := fixedpoint.Mul(pv.Volume, pv.Price)
totalQuoteAmount = totalQuoteAmount.Add(quoteAmount)
totalQuantity = totalQuantity.Add(pv.Volume)

if requiredDepthInQuote.Sign() > 0 && totalQuoteAmount.Compare(requiredDepthInQuote) > 0 {
return totalQuoteAmount.Div(totalQuantity)
}
}

return totalQuoteAmount.Div(totalQuantity)
}

// AverageDepthPrice uses the required total quantity to calculate the corresponding price
func (slice PriceVolumeSlice) AverageDepthPrice(requiredQuantity fixedpoint.Value) fixedpoint.Value {
// rest quantity
rq := requiredQuantity
totalAmount := fixedpoint.Zero

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

for i := 0; i < len(slice); i++ {
pv := slice[i]
if pv.Volume.Compare(rq) >= 0 {
totalAmount = totalAmount.Add(rq.Mul(pv.Price))
break
}

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

return totalAmount.Div(requiredQuantity.Sub(rq))
}
Loading