Merge pull request #189 from LarryLuTW/validator

This commit is contained in:
Yo-An Lin 2021-04-02 20:21:51 +08:00 committed by GitHub
commit 4ed95e6070
4 changed files with 58 additions and 20 deletions

View File

@ -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)
}

View File

@ -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")

View File

@ -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)

View File

@ -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 {