From dbf5d27f30b33855f382b2e8face87da25eb5f5c Mon Sep 17 00:00:00 2001 From: Larry850806 Date: Fri, 2 Apr 2021 10:12:55 +0800 Subject: [PATCH 1/4] Add a validator interface to validate strategy before run --- pkg/bbgo/trader.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 64be7ca42..d2665663d 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -35,6 +35,10 @@ type CrossExchangeStrategy interface { CrossRun(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error } +type Validator interface { + Validate() error +} + //go:generate callbackgen -type Graceful type Graceful struct { shutdownCallbacks []func(ctx context.Context, wg *sync.WaitGroup) @@ -235,6 +239,13 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si } } + // If the strategy has Validate() method, run it and check the error + if v, ok := strategy.(Validator); ok { + if err := v.Validate(); err != nil { + return fmt.Errorf("failed to validate the config: %w", err) + } + } + return strategy.Run(ctx, orderExecutor, session) } From 2c41ec28ae06fa3a8208d4ca70b003d722c42bba Mon Sep 17 00:00:00 2001 From: Larry850806 Date: Fri, 2 Apr 2021 10:22:03 +0800 Subject: [PATCH 2/4] Add validation for bollgrid strategy --- pkg/strategy/bollgrid/strategy.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/strategy/bollgrid/strategy.go b/pkg/strategy/bollgrid/strategy.go index 078d8ec05..4c4d1c529 100644 --- a/pkg/strategy/bollgrid/strategy.go +++ b/pkg/strategy/bollgrid/strategy.go @@ -88,6 +88,18 @@ func (s *Strategy) ID() string { return ID } +func (s *Strategy) Validate() error { + if s.ProfitSpread <= 0 { + // If profitSpread is empty or its value is negative + return fmt.Errorf("profit spread should bigger than 0") + } + if s.Quantity <= 0 { + // If quantity is empty or its value is negative + return fmt.Errorf("quantity should bigger than 0") + } + return nil +} + func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { if s.Interval == "" { panic("bollgrid interval can not be empty") From 53133851cc8e9fbdc1c742fbde53eced97e1b6ce Mon Sep 17 00:00:00 2001 From: Larry850806 Date: Fri, 2 Apr 2021 10:27:20 +0800 Subject: [PATCH 3/4] Add validation for grid strategy --- pkg/strategy/grid/strategy.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index 89f4895a4..cc37fc238 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -106,6 +106,29 @@ func (s *Strategy) ID() string { return ID } +func (s *Strategy) Validate() error { + if s.UpperPrice == 0 { + return errors.New("upperPrice can not be zero, you forgot to set?") + } + if s.LowerPrice == 0 { + return errors.New("lowerPrice can not be zero, you forgot to set?") + } + if s.UpperPrice <= s.LowerPrice { + return fmt.Errorf("upperPrice (%f) should not be less than or equal to lowerPrice (%f)", s.UpperPrice.Float64(), s.LowerPrice.Float64()) + } + + if s.ProfitSpread <= 0 { + // If profitSpread is empty or its value is negative + return fmt.Errorf("profit spread should bigger than 0") + } + + if s.Quantity == 0 && s.ScaleQuantity == nil { + return fmt.Errorf("quantity or scaleQuantity can not be zero") + } + + return nil +} + func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) { currentPriceFloat, ok := session.LastPrice(s.Symbol) if !ok { @@ -487,18 +510,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.Side = types.SideTypeBoth } - if s.UpperPrice == 0 { - return errors.New("upperPrice can not be zero, you forgot to set?") - } - - if s.LowerPrice == 0 { - return errors.New("lowerPrice can not be zero, you forgot to set?") - } - - if s.UpperPrice <= s.LowerPrice { - return fmt.Errorf("upperPrice (%f) should not be less than or equal to lowerPrice (%f)", s.UpperPrice.Float64(), s.LowerPrice.Float64()) - } - instanceID := fmt.Sprintf("grid-%s-%d-%d-%d", s.Symbol, s.GridNum, s.UpperPrice, s.LowerPrice) s.groupID = max.GenerateGroupID(instanceID) log.Infof("using group id %d from fnv(%s)", s.groupID, instanceID) From 6718aace8c39c636d2d46eeae65ebe0f14b21577 Mon Sep 17 00:00:00 2001 From: Larry850806 Date: Fri, 2 Apr 2021 10:32:24 +0800 Subject: [PATCH 4/4] Add validation for support strategy --- pkg/strategy/support/strategy.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index 9918ec529..bfb5d3e5d 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -44,6 +44,18 @@ func (s *Strategy) ID() string { return ID } +func (s *Strategy) Validate() error { + if s.Quantity == 0 && s.ScaleQuantity == nil { + return fmt.Errorf("quantity or scaleQuantity can not be zero") + } + + if s.MinVolume == 0 { + return fmt.Errorf("minVolume can not be zero") + } + + return nil +} + func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)}) } @@ -58,14 +70,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.MovingAverageWindow = 99 } - if s.Quantity == 0 && s.ScaleQuantity == nil { - return fmt.Errorf("quantity or scaleQuantity can not be zero") - } - - if s.MinVolume == 0 { - return fmt.Errorf("minVolume can not be zero") - } - // buy when price drops -8% market, ok := session.Market(s.Symbol) if !ok {