mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
integrate aggregatePrice method
This commit is contained in:
parent
1c567d7146
commit
99edfb61bf
|
@ -1,32 +1,2 @@
|
||||||
package xdepthmaker
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -712,7 +712,7 @@ func (s *Strategy) generateMakerOrders(
|
||||||
|
|
||||||
log.Infof("side: %s required depth: %f, pvs: %+v", side, requiredDepth.Float64(), pvs)
|
log.Infof("side: %s required depth: %f, pvs: %+v", side, requiredDepth.Float64(), pvs)
|
||||||
|
|
||||||
depthPrice := pvs.AverageDepthPrice(fixedpoint.Zero, 0)
|
depthPrice := pvs.AverageDepthPriceByQuote(fixedpoint.Zero, 0)
|
||||||
|
|
||||||
switch side {
|
switch side {
|
||||||
case types.SideTypeBuy:
|
case types.SideTypeBuy:
|
||||||
|
|
|
@ -207,7 +207,11 @@ func ParsePriceVolumeSliceJSON(b []byte) (slice PriceVolumeSlice, err error) {
|
||||||
return slice, nil
|
return slice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slice PriceVolumeSlice) AverageDepthPrice(requiredDepthInQuote fixedpoint.Value, maxLevel int) fixedpoint.Value {
|
func (slice PriceVolumeSlice) AverageDepthPriceByQuote(requiredDepthInQuote fixedpoint.Value, maxLevel int) fixedpoint.Value {
|
||||||
|
if len(slice) == 0 {
|
||||||
|
return fixedpoint.Zero
|
||||||
|
}
|
||||||
|
|
||||||
totalQuoteAmount := fixedpoint.Zero
|
totalQuoteAmount := fixedpoint.Zero
|
||||||
totalQuantity := fixedpoint.Zero
|
totalQuantity := fixedpoint.Zero
|
||||||
|
|
||||||
|
@ -229,3 +233,29 @@ func (slice PriceVolumeSlice) AverageDepthPrice(requiredDepthInQuote fixedpoint.
|
||||||
|
|
||||||
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…
Reference in New Issue
Block a user