From dbf5d27f30b33855f382b2e8face87da25eb5f5c Mon Sep 17 00:00:00 2001 From: Larry850806 Date: Fri, 2 Apr 2021 10:12:55 +0800 Subject: [PATCH] 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) }