2023-11-27 09:45:12 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-12-06 10:05:33 +00:00
|
|
|
price = totalAmount.Div(requiredQuantity.Sub(q))
|
2023-11-27 09:45:12 +00:00
|
|
|
return price
|
|
|
|
}
|