mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
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:
commit
fe2fa1912a
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user