improve/dynamic_quantity: fix dynamic qty logic

This commit is contained in:
Andy Cheng 2022-12-14 12:28:39 +08:00
parent 2b8a5fe755
commit d510c37e91
2 changed files with 16 additions and 10 deletions

View File

@ -19,10 +19,10 @@ func (d *DynamicQuantitySet) Initialize(symbol string, session *bbgo.ExchangeSes
} }
// GetQuantity returns the quantity // GetQuantity returns the quantity
func (d *DynamicQuantitySet) GetQuantity() (fixedpoint.Value, error) { func (d *DynamicQuantitySet) GetQuantity(reverse bool) (fixedpoint.Value, error) {
quantity := fixedpoint.Zero quantity := fixedpoint.Zero
for i := range *d { for i := range *d {
v, err := (*d)[i].getQuantity() v, err := (*d)[i].getQuantity(reverse)
if err != nil { if err != nil {
return fixedpoint.Zero, err return fixedpoint.Zero, err
} }
@ -50,10 +50,10 @@ func (d *DynamicQuantity) IsEnabled() bool {
} }
// getQuantity returns quantity // getQuantity returns quantity
func (d *DynamicQuantity) getQuantity() (fixedpoint.Value, error) { func (d *DynamicQuantity) getQuantity(reverse bool) (fixedpoint.Value, error) {
switch { switch {
case d.LinRegDynamicQuantity != nil: case d.LinRegDynamicQuantity != nil:
return d.LinRegDynamicQuantity.getQuantity() return d.LinRegDynamicQuantity.getQuantity(reverse)
default: default:
return fixedpoint.Zero, errors.New("dynamic quantity is not enabled") return fixedpoint.Zero, errors.New("dynamic quantity is not enabled")
} }
@ -84,8 +84,14 @@ func (d *DynamicQuantityLinReg) initialize(symbol string, session *bbgo.Exchange
} }
// getQuantity returns quantity // getQuantity returns quantity
func (d *DynamicQuantityLinReg) getQuantity() (fixedpoint.Value, error) { func (d *DynamicQuantityLinReg) getQuantity(reverse bool) (fixedpoint.Value, error) {
v, err := d.DynamicQuantityLinRegScale.Scale(d.QuantityLinReg.LastRatio()) var linregRatio float64
if reverse {
linregRatio = -d.QuantityLinReg.LastRatio()
} else {
linregRatio = d.QuantityLinReg.LastRatio()
}
v, err := d.DynamicQuantityLinRegScale.Scale(linregRatio)
if err != nil { if err != nil {
return fixedpoint.Zero, err return fixedpoint.Zero, err
} }

View File

@ -334,7 +334,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp
switch { switch {
case s.mainTrendCurrent == types.DirectionUp: case s.mainTrendCurrent == types.DirectionUp:
if len(s.DynamicQuantityIncrease) > 0 { if len(s.DynamicQuantityIncrease) > 0 {
qty, err := s.DynamicQuantityIncrease.GetQuantity() qty, err := s.DynamicQuantityIncrease.GetQuantity(false)
if err == nil { if err == nil {
buyQuantity = qty buyQuantity = qty
} else { } else {
@ -343,7 +343,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp
} }
} }
if len(s.DynamicQuantityDecrease) > 0 { if len(s.DynamicQuantityDecrease) > 0 {
qty, err := s.DynamicQuantityDecrease.GetQuantity() qty, err := s.DynamicQuantityDecrease.GetQuantity(false)
if err == nil { if err == nil {
sellQuantity = qty sellQuantity = qty
} else { } else {
@ -353,7 +353,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp
} }
case s.mainTrendCurrent == types.DirectionDown: case s.mainTrendCurrent == types.DirectionDown:
if len(s.DynamicQuantityIncrease) > 0 { if len(s.DynamicQuantityIncrease) > 0 {
qty, err := s.DynamicQuantityIncrease.GetQuantity() qty, err := s.DynamicQuantityIncrease.GetQuantity(true)
if err == nil { if err == nil {
sellQuantity = qty sellQuantity = qty
} else { } else {
@ -362,7 +362,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp
} }
} }
if len(s.DynamicQuantityDecrease) > 0 { if len(s.DynamicQuantityDecrease) > 0 {
qty, err := s.DynamicQuantityDecrease.GetQuantity() qty, err := s.DynamicQuantityDecrease.GetQuantity(true)
if err == nil { if err == nil {
buyQuantity = qty buyQuantity = qty
} else { } else {