Merge pull request #1700 from c9s/narumi/autobuy-boll

Fix: [autobuy] fix error when bollinger settings is not set
This commit is contained in:
c9s 2024-08-23 13:05:18 +08:00 committed by GitHub
commit b2f1f7d735
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,6 +32,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"`
@ -65,13 +66,39 @@ 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
}
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
}
func (s *Strategy) Defaults() error {
if s.OrderType == "" {
s.OrderType = types.OrderTypeLimit
}
if s.PriceType == "" {
s.PriceType = types.PriceTypeMaker
}
@ -79,13 +106,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)
@ -131,7 +162,7 @@ func (s *Strategy) autobuy(ctx context.Context) {
side := types.SideTypeBuy
price := s.PriceType.GetPrice(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
}
@ -146,7 +177,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,
}