mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
Merge pull request #189 from LarryLuTW/validator
This commit is contained in:
commit
4ed95e6070
|
@ -35,6 +35,10 @@ type CrossExchangeStrategy interface {
|
||||||
CrossRun(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
|
CrossRun(ctx context.Context, orderExecutionRouter OrderExecutionRouter, sessions map[string]*ExchangeSession) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Validator interface {
|
||||||
|
Validate() error
|
||||||
|
}
|
||||||
|
|
||||||
//go:generate callbackgen -type Graceful
|
//go:generate callbackgen -type Graceful
|
||||||
type Graceful struct {
|
type Graceful struct {
|
||||||
shutdownCallbacks []func(ctx context.Context, wg *sync.WaitGroup)
|
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)
|
return strategy.Run(ctx, orderExecutor, session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,18 @@ func (s *Strategy) ID() string {
|
||||||
return ID
|
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) {
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
if s.Interval == "" {
|
if s.Interval == "" {
|
||||||
panic("bollgrid interval can not be empty")
|
panic("bollgrid interval can not be empty")
|
||||||
|
|
|
@ -106,6 +106,29 @@ func (s *Strategy) ID() string {
|
||||||
return ID
|
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) {
|
func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]types.SubmitOrder, error) {
|
||||||
currentPriceFloat, ok := session.LastPrice(s.Symbol)
|
currentPriceFloat, ok := session.LastPrice(s.Symbol)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -487,18 +510,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.Side = types.SideTypeBoth
|
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)
|
instanceID := fmt.Sprintf("grid-%s-%d-%d-%d", s.Symbol, s.GridNum, s.UpperPrice, s.LowerPrice)
|
||||||
s.groupID = max.GenerateGroupID(instanceID)
|
s.groupID = max.GenerateGroupID(instanceID)
|
||||||
log.Infof("using group id %d from fnv(%s)", s.groupID, instanceID)
|
log.Infof("using group id %d from fnv(%s)", s.groupID, instanceID)
|
||||||
|
|
|
@ -44,6 +44,18 @@ func (s *Strategy) ID() string {
|
||||||
return ID
|
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) {
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)})
|
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
|
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%
|
// buy when price drops -8%
|
||||||
market, ok := session.Market(s.Symbol)
|
market, ok := session.Market(s.Symbol)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user