From 731fa9af7e61bb6e9814e0bdd20a34441f15288d Mon Sep 17 00:00:00 2001 From: narumi Date: Wed, 21 Aug 2024 13:28:22 +0800 Subject: [PATCH 1/3] fix error when bollinger settings is not set --- pkg/strategy/autobuy/strategy.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/strategy/autobuy/strategy.go b/pkg/strategy/autobuy/strategy.go index 13a143161..1fe8beff4 100644 --- a/pkg/strategy/autobuy/strategy.go +++ b/pkg/strategy/autobuy/strategy.go @@ -67,6 +67,16 @@ func (s *Strategy) Validate() error { if err := s.QuantityOrAmount.Validate(); err != nil { return err } + + if s.Bollinger != nil { + if s.Bollinger.Interval == "" { + return fmt.Errorf("bollinger interval is required") + } + + if s.Bollinger.BandWidth <= 0 { + return fmt.Errorf("bollinger band width must be greater than 0") + } + } return nil } @@ -78,13 +88,17 @@ func (s *Strategy) Defaults() error { } func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { - session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Bollinger.Interval}) + if s.Bollinger != nil { + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Bollinger.Interval}) + } } func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID()) - s.boll = session.Indicators(s.Symbol).BOLL(s.Bollinger.IntervalWindow, s.Bollinger.BandWidth) + if s.Bollinger != nil { + s.boll = session.Indicators(s.Symbol).BOLL(s.Bollinger.IntervalWindow, s.Bollinger.BandWidth) + } s.OrderExecutor.ActiveMakerOrders().OnFilled(func(order types.Order) { s.autobuy(ctx) @@ -130,7 +144,7 @@ func (s *Strategy) autobuy(ctx context.Context) { side := types.SideTypeBuy price := s.PriceType.Map(ticker, side) - if price.Float64() > s.boll.UpBand.Last(0) { + if s.boll != nil && price.Float64() > s.boll.UpBand.Last(0) { log.Infof("price %s is higher than upper band %f, skip", price.String(), s.boll.UpBand.Last(0)) return } From b820fccce13699a804778cf57666aa8d2a32164a Mon Sep 17 00:00:00 2001 From: narumi Date: Wed, 21 Aug 2024 13:38:54 +0800 Subject: [PATCH 2/3] add order type to config --- pkg/strategy/autobuy/strategy.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/strategy/autobuy/strategy.go b/pkg/strategy/autobuy/strategy.go index 1fe8beff4..06dadb86c 100644 --- a/pkg/strategy/autobuy/strategy.go +++ b/pkg/strategy/autobuy/strategy.go @@ -31,6 +31,7 @@ type Strategy struct { Symbol string `json:"symbol"` Schedule string `json:"schedule"` MinBaseBalance fixedpoint.Value `json:"minBaseBalance"` + OrderType types.OrderType `json:"orderType"` PriceType types.PriceType `json:"priceType"` Bollinger *types.BollingerSetting `json:"bollinger"` DryRun bool `json:"dryRun"` @@ -81,6 +82,10 @@ func (s *Strategy) Validate() error { } func (s *Strategy) Defaults() error { + if s.OrderType == "" { + s.OrderType = types.OrderTypeLimit + } + if s.PriceType == "" { s.PriceType = types.PriceTypeMaker } @@ -159,7 +164,7 @@ func (s *Strategy) autobuy(ctx context.Context) { submitOrder := types.SubmitOrder{ Symbol: s.Symbol, Side: side, - Type: types.OrderTypeLimitMaker, + Type: s.OrderType, Quantity: quantity, Price: price, } From 1b06fcc96112dbff191175bf3169bc5bdb86854b Mon Sep 17 00:00:00 2001 From: narumi Date: Wed, 21 Aug 2024 13:42:19 +0800 Subject: [PATCH 3/3] validate parameters --- pkg/strategy/autobuy/strategy.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/strategy/autobuy/strategy.go b/pkg/strategy/autobuy/strategy.go index 06dadb86c..85ef0b316 100644 --- a/pkg/strategy/autobuy/strategy.go +++ b/pkg/strategy/autobuy/strategy.go @@ -65,6 +65,18 @@ func (s *Strategy) InstanceID() string { } func (s *Strategy) Validate() error { + if s.Symbol == "" { + return fmt.Errorf("symbol is required") + } + + if s.Schedule == "" { + return fmt.Errorf("schedule is required") + } + + if s.MinBaseBalance.Sign() <= 0 { + return fmt.Errorf("minBaseBalance must be greater than 0") + } + if err := s.QuantityOrAmount.Validate(); err != nil { return err }