mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
xmaker: fix aggregatePrice function
This commit is contained in:
parent
e187614179
commit
8de0c67503
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user