Merge pull request #1455 from c9s/c9s/fix-persistence-loading

FIX: move Initialize() call out, call it before the LoadState
This commit is contained in:
kbearXD 2023-12-18 13:42:15 +08:00 committed by GitHub
commit fe2fa1912a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

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

View File

@ -16,8 +16,8 @@ import (
) )
// Strategy method calls: // Strategy method calls:
// -> Defaults() (optional method)
// -> Initialize() (optional method) // -> Initialize() (optional method)
// -> Defaults() (optional method)
// -> 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)
@ -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 { if subscriber, ok := strategy.(ExchangeSessionSubscriber); ok {
subscriber.Subscribe(session) subscriber.Subscribe(session)
} else { } else {
@ -362,17 +356,26 @@ func (trader *Trader) Run(ctx context.Context) error {
return trader.environment.Connect(ctx) 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 { func (trader *Trader) LoadState(ctx context.Context) error {
if trader.environment.BacktestService != nil { if trader.environment.BacktestService != nil {
return nil return nil
} }
isolation := GetIsolationFromContext(ctx) isolation := GetIsolationFromContext(ctx)
ps := isolation.persistenceServiceFacade.Get() ps := isolation.persistenceServiceFacade.Get()
log.Infof("loading strategies states...") log.Infof("loading strategies states...")
return trader.IterateStrategies(func(strategy StrategyID) error { return trader.IterateStrategies(func(strategy StrategyID) error {
id := dynamic.CallID(strategy) id := dynamic.CallID(strategy)
return loadPersistenceFields(strategy, id, ps) return loadPersistenceFields(strategy, id, ps)

View File

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