From d510c37e911fe7fb50a5b8372ecb24179dac90d3 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Wed, 14 Dec 2022 12:28:39 +0800 Subject: [PATCH] improve/dynamic_quantity: fix dynamic qty logic --- pkg/dynamicmetric/dynamic_quantity.go | 18 ++++++++++++------ pkg/strategy/linregmaker/strategy.go | 8 ++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/dynamicmetric/dynamic_quantity.go b/pkg/dynamicmetric/dynamic_quantity.go index 70e44797d..fa3182ada 100644 --- a/pkg/dynamicmetric/dynamic_quantity.go +++ b/pkg/dynamicmetric/dynamic_quantity.go @@ -19,10 +19,10 @@ func (d *DynamicQuantitySet) Initialize(symbol string, session *bbgo.ExchangeSes } // GetQuantity returns the quantity -func (d *DynamicQuantitySet) GetQuantity() (fixedpoint.Value, error) { +func (d *DynamicQuantitySet) GetQuantity(reverse bool) (fixedpoint.Value, error) { quantity := fixedpoint.Zero for i := range *d { - v, err := (*d)[i].getQuantity() + v, err := (*d)[i].getQuantity(reverse) if err != nil { return fixedpoint.Zero, err } @@ -50,10 +50,10 @@ func (d *DynamicQuantity) IsEnabled() bool { } // getQuantity returns quantity -func (d *DynamicQuantity) getQuantity() (fixedpoint.Value, error) { +func (d *DynamicQuantity) getQuantity(reverse bool) (fixedpoint.Value, error) { switch { case d.LinRegDynamicQuantity != nil: - return d.LinRegDynamicQuantity.getQuantity() + return d.LinRegDynamicQuantity.getQuantity(reverse) default: 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 -func (d *DynamicQuantityLinReg) getQuantity() (fixedpoint.Value, error) { - v, err := d.DynamicQuantityLinRegScale.Scale(d.QuantityLinReg.LastRatio()) +func (d *DynamicQuantityLinReg) getQuantity(reverse bool) (fixedpoint.Value, error) { + var linregRatio float64 + if reverse { + linregRatio = -d.QuantityLinReg.LastRatio() + } else { + linregRatio = d.QuantityLinReg.LastRatio() + } + v, err := d.DynamicQuantityLinRegScale.Scale(linregRatio) if err != nil { return fixedpoint.Zero, err } diff --git a/pkg/strategy/linregmaker/strategy.go b/pkg/strategy/linregmaker/strategy.go index d7342050e..478f6e168 100644 --- a/pkg/strategy/linregmaker/strategy.go +++ b/pkg/strategy/linregmaker/strategy.go @@ -334,7 +334,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp switch { case s.mainTrendCurrent == types.DirectionUp: if len(s.DynamicQuantityIncrease) > 0 { - qty, err := s.DynamicQuantityIncrease.GetQuantity() + qty, err := s.DynamicQuantityIncrease.GetQuantity(false) if err == nil { buyQuantity = qty } else { @@ -343,7 +343,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp } } if len(s.DynamicQuantityDecrease) > 0 { - qty, err := s.DynamicQuantityDecrease.GetQuantity() + qty, err := s.DynamicQuantityDecrease.GetQuantity(false) if err == nil { sellQuantity = qty } else { @@ -353,7 +353,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp } case s.mainTrendCurrent == types.DirectionDown: if len(s.DynamicQuantityIncrease) > 0 { - qty, err := s.DynamicQuantityIncrease.GetQuantity() + qty, err := s.DynamicQuantityIncrease.GetQuantity(true) if err == nil { sellQuantity = qty } else { @@ -362,7 +362,7 @@ func (s *Strategy) getOrderQuantities(askPrice fixedpoint.Value, bidPrice fixedp } } if len(s.DynamicQuantityDecrease) > 0 { - qty, err := s.DynamicQuantityDecrease.GetQuantity() + qty, err := s.DynamicQuantityDecrease.GetQuantity(true) if err == nil { buyQuantity = qty } else {