all: move Initialize() call out, call it before the LoadState

This commit is contained in:
c9s 2023-12-18 12:09:03 +08:00
parent 0466165258
commit 3e6d6e10b3
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 21 additions and 9 deletions

View File

@ -109,6 +109,11 @@ func main() {
return
}
if err := trader.Initialize(ctx); err != nil {
log.WithError(err).Error("failed to initialize strategies")
return
}
if err := trader.LoadState(ctx); err != nil {
log.WithError(err).Error("failed to load strategy states")
return

View File

@ -16,8 +16,8 @@ import (
)
// Strategy method calls:
// -> Defaults() (optional method)
// -> Initialize() (optional method)
// -> Defaults() (optional method)
// -> Validate() (optional method)
// -> Run() (optional method)
// -> Shutdown(shutdownCtx context.Context, wg *sync.WaitGroup)
@ -244,12 +244,6 @@ func (trader *Trader) injectFieldsAndSubscribe(ctx context.Context) error {
}
}
if initializer, ok := strategy.(StrategyInitializer); ok {
if err := initializer.Initialize(); err != nil {
panic(err)
}
}
if subscriber, ok := strategy.(ExchangeSessionSubscriber); ok {
subscriber.Subscribe(session)
} else {
@ -362,17 +356,26 @@ func (trader *Trader) Run(ctx context.Context) error {
return trader.environment.Connect(ctx)
}
func (trader *Trader) Initialize(ctx context.Context) error {
log.Infof("initializing strategies...")
return trader.IterateStrategies(func(strategy StrategyID) error {
if initializer, ok := strategy.(StrategyInitializer); ok {
return initializer.Initialize()
}
return nil
})
}
func (trader *Trader) LoadState(ctx context.Context) error {
if trader.environment.BacktestService != nil {
return nil
}
isolation := GetIsolationFromContext(ctx)
ps := isolation.persistenceServiceFacade.Get()
log.Infof("loading strategies states...")
return trader.IterateStrategies(func(strategy StrategyID) error {
id := dynamic.CallID(strategy)
return loadPersistenceFields(strategy, id, ps)

View File

@ -163,6 +163,10 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
return err
}
if err := trader.Initialize(tradingCtx); err != nil {
return err
}
if err := trader.LoadState(tradingCtx); err != nil {
return err
}