mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
cmd: call SaveState and LoadState for normal run
This commit is contained in:
parent
57c43936d6
commit
7378c63cb7
|
@ -102,6 +102,11 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := trader.LoadState() ; err != nil {
|
||||||
|
log.WithError(err).Error("failed to load strategy states")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// for setup mode, we don't start the trader
|
// for setup mode, we don't start the trader
|
||||||
go func() {
|
go func() {
|
||||||
if err := trader.Run(ctx); err != nil {
|
if err := trader.Run(ctx); err != nil {
|
||||||
|
|
|
@ -110,6 +110,11 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := trader.LoadState() ; err != nil {
|
||||||
|
log.WithError(err).Error("failed to load strategy states")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// for setup mode, we don't start the trader
|
// for setup mode, we don't start the trader
|
||||||
if err := trader.Run(ctx); err != nil {
|
if err := trader.Run(ctx); err != nil {
|
||||||
log.WithError(err).Error("failed to start trader")
|
log.WithError(err).Error("failed to start trader")
|
||||||
|
|
|
@ -248,15 +248,6 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before we run the strategy we need to load the state from the persistence layer:
|
|
||||||
// 1) scan the struct fields and find the persistence field
|
|
||||||
// 2) load the data and set the value into the persistence field.
|
|
||||||
|
|
||||||
ps := trader.environment.PersistenceServiceFacade.Get()
|
|
||||||
if err := loadPersistenceFields(strategy, strategy.ID(), ps); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return strategy.Run(ctx, orderExecutor, session)
|
return strategy.Run(ctx, orderExecutor, session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,7 +313,6 @@ func (trader *Trader) Run(ctx context.Context) error {
|
||||||
router.executors[sessionID] = orderExecutor
|
router.executors[sessionID] = orderExecutor
|
||||||
}
|
}
|
||||||
|
|
||||||
ps := trader.environment.PersistenceServiceFacade.Get()
|
|
||||||
for _, strategy := range trader.crossExchangeStrategies {
|
for _, strategy := range trader.crossExchangeStrategies {
|
||||||
rs := reflect.ValueOf(strategy)
|
rs := reflect.ValueOf(strategy)
|
||||||
|
|
||||||
|
@ -336,10 +326,6 @@ func (trader *Trader) Run(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := loadPersistenceFields(strategy, strategy.ID(), ps); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := strategy.CrossRun(ctx, router, trader.environment.sessions); err != nil {
|
if err := strategy.CrossRun(ctx, router, trader.environment.sessions); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -348,6 +334,72 @@ func (trader *Trader) Run(ctx context.Context) error {
|
||||||
return trader.environment.Connect(ctx)
|
return trader.environment.Connect(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (trader *Trader) LoadState() error {
|
||||||
|
if trader.environment.BacktestService != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if trader.environment.PersistenceServiceFacade == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ps := trader.environment.PersistenceServiceFacade.Get()
|
||||||
|
|
||||||
|
log.Infof("loading strategies states...")
|
||||||
|
for _, strategies := range trader.exchangeStrategies {
|
||||||
|
for _, strategy := range strategies {
|
||||||
|
if err := loadPersistenceFields(strategy, strategy.ID(), ps); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, strategy := range trader.crossExchangeStrategies {
|
||||||
|
if err := loadPersistenceFields(strategy, strategy.ID(), ps); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (trader *Trader) SaveState() error {
|
||||||
|
if trader.environment.BacktestService != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if trader.environment.PersistenceServiceFacade == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ps := trader.environment.PersistenceServiceFacade.Get()
|
||||||
|
|
||||||
|
log.Infof("saving strategies states...")
|
||||||
|
for _, strategies := range trader.exchangeStrategies {
|
||||||
|
for _, strategy := range strategies {
|
||||||
|
id := callID(strategy)
|
||||||
|
if len(id) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := storePersistenceFields(strategy, id, ps); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, strategy := range trader.crossExchangeStrategies {
|
||||||
|
id := callID(strategy)
|
||||||
|
if len(id) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := storePersistenceFields(strategy, id, ps); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var defaultPersistenceSelector = &PersistenceSelector{
|
var defaultPersistenceSelector = &PersistenceSelector{
|
||||||
StoreID: "default",
|
StoreID: "default",
|
||||||
Type: "memory",
|
Type: "memory",
|
||||||
|
|
|
@ -189,6 +189,10 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := trader.LoadState(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := trader.Run(ctx); err != nil {
|
if err := trader.Run(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -228,6 +232,10 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
|
||||||
trader.Graceful.Shutdown(shutdownCtx)
|
trader.Graceful.Shutdown(shutdownCtx)
|
||||||
cancelShutdown()
|
cancelShutdown()
|
||||||
|
|
||||||
|
if err := trader.SaveState(); err != nil {
|
||||||
|
log.WithError(err).Errorf("can not save strategy states")
|
||||||
|
}
|
||||||
|
|
||||||
for _, session := range environ.Sessions() {
|
for _, session := range environ.Sessions() {
|
||||||
if err := session.MarketDataStream.Close(); err != nil {
|
if err := session.MarketDataStream.Close(); err != nil {
|
||||||
log.WithError(err).Errorf("[%s] market data stream close error", session.Name)
|
log.WithError(err).Errorf("[%s] market data stream close error", session.Name)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user