bbgo: fix the defaults / initialize steps
This commit is contained in:
parent
d002e98271
commit
53f9d523b0
|
@ -16,8 +16,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Strategy method calls:
|
// Strategy method calls:
|
||||||
// -> Initialize() (optional method)
|
|
||||||
// -> Defaults() (optional method)
|
// -> Defaults() (optional method)
|
||||||
|
//
|
||||||
|
// setup default static values from constants
|
||||||
|
//
|
||||||
|
// -> Initialize() (optional method)
|
||||||
|
//
|
||||||
|
// initialize dynamic runtime objects
|
||||||
|
//
|
||||||
|
// -> Subscribe()
|
||||||
|
//
|
||||||
|
// register the subscriptions
|
||||||
|
//
|
||||||
// -> Validate() (optional method)
|
// -> Validate() (optional method)
|
||||||
// -> Run() (optional method)
|
// -> Run() (optional method)
|
||||||
// -> Shutdown(shutdownCtx context.Context, wg *sync.WaitGroup)
|
// -> Shutdown(shutdownCtx context.Context, wg *sync.WaitGroup)
|
||||||
|
@ -171,12 +181,6 @@ func (trader *Trader) SetRiskControls(riskControls *RiskControls) {
|
||||||
func (trader *Trader) RunSingleExchangeStrategy(
|
func (trader *Trader) RunSingleExchangeStrategy(
|
||||||
ctx context.Context, strategy SingleExchangeStrategy, session *ExchangeSession, orderExecutor OrderExecutor,
|
ctx context.Context, strategy SingleExchangeStrategy, session *ExchangeSession, orderExecutor OrderExecutor,
|
||||||
) error {
|
) error {
|
||||||
if v, ok := strategy.(StrategyValidator); ok {
|
|
||||||
if err := v.Validate(); err != nil {
|
|
||||||
return fmt.Errorf("failed to validate the config: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if shutdown, ok := strategy.(StrategyShutdown); ok {
|
if shutdown, ok := strategy.(StrategyShutdown); ok {
|
||||||
trader.gracefulShutdown.OnShutdown(shutdown.Shutdown)
|
trader.gracefulShutdown.OnShutdown(shutdown.Shutdown)
|
||||||
}
|
}
|
||||||
|
@ -238,12 +242,6 @@ func (trader *Trader) injectFieldsAndSubscribe(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaulter, ok := strategy.(StrategyDefaulter); ok {
|
|
||||||
if err := defaulter.Defaults(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if subscriber, ok := strategy.(ExchangeSessionSubscriber); ok {
|
if subscriber, ok := strategy.(ExchangeSessionSubscriber); ok {
|
||||||
subscriber.Subscribe(session)
|
subscriber.Subscribe(session)
|
||||||
} else {
|
} else {
|
||||||
|
@ -304,12 +302,6 @@ func (trader *Trader) injectFieldsAndSubscribe(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if initializer, ok := strategy.(StrategyInitializer); ok {
|
|
||||||
if err := initializer.Initialize(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if subscriber, ok := strategy.(CrossExchangeSessionSubscriber); ok {
|
if subscriber, ok := strategy.(CrossExchangeSessionSubscriber); ok {
|
||||||
subscriber.CrossSubscribe(trader.environment.sessions)
|
subscriber.CrossSubscribe(trader.environment.sessions)
|
||||||
} else {
|
} else {
|
||||||
|
@ -356,8 +348,23 @@ func (trader *Trader) Run(ctx context.Context) error {
|
||||||
return trader.environment.Connect(ctx)
|
return trader.environment.Connect(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize initializes the strategies, this method is called before the Run method.
|
||||||
|
// It sets the default values and validates the strategy configurations.
|
||||||
|
// And calls the Initialize method if the strategy implements the Initialize method.
|
||||||
func (trader *Trader) Initialize(ctx context.Context) error {
|
func (trader *Trader) Initialize(ctx context.Context) error {
|
||||||
return trader.IterateStrategies(func(strategy StrategyID) error {
|
return trader.IterateStrategies(func(strategy StrategyID) error {
|
||||||
|
if defaulter, ok := strategy.(StrategyDefaulter); ok {
|
||||||
|
if err := defaulter.Defaults(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := strategy.(StrategyValidator); ok {
|
||||||
|
if err := v.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("found invalid strategy config: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if initializer, ok := strategy.(StrategyInitializer); ok {
|
if initializer, ok := strategy.(StrategyInitializer); ok {
|
||||||
return initializer.Initialize()
|
return initializer.Initialize()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user