diff --git a/pkg/bbgo/persistence.go b/pkg/bbgo/persistence.go index 2b1be9f83..b435c8f07 100644 --- a/pkg/bbgo/persistence.go +++ b/pkg/bbgo/persistence.go @@ -91,14 +91,18 @@ func (p *Persistence) Sync(obj interface{}) error { } // Sync syncs the object properties into the persistence layer -func Sync(obj interface{}) error { +func Sync(obj interface{}) { id := callID(obj) if len(id) == 0 { - return nil + log.Warnf("InstanceID() is not provided, can not sync persistence") + return } ps := PersistenceServiceFacade.Get() - return storePersistenceFields(obj, id, ps) + err := storePersistenceFields(obj, id, ps) + if err != nil { + log.WithError(err).Errorf("persistence sync failed") + } } func loadPersistenceFields(obj interface{}, id string, persistence service.PersistenceService) error { diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 9dec53ca9..a48eba33d 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -494,7 +494,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OnSuspend(func() { s.Status = types.StrategyStatusStopped _ = s.orderExecutor.GracefulCancel(ctx) - _ = s.Persistence.Sync(s) + bbgo.Sync(s) }) s.OnEmergencyStop(func() { @@ -578,9 +578,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // TODO: migrate persistance to singleton s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - if err := s.Persistence.Sync(s); err != nil { - log.WithError(err).Errorf("can not sync state to persistence") - } + bbgo.Sync(s) }) s.SmartStops.RunStopControllers(ctx, session, s.orderExecutor.TradeCollector()) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index c9b28d631..7f4250ab8 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -231,6 +231,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindEnvironment(s.Environment) s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) + s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { + bbgo.Sync(s) + }) s.orderExecutor.Bind() store, _ := session.MarketDataStore(s.Symbol) diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 5d70edfa9..e91313be3 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -3,9 +3,10 @@ package supertrend import ( "context" "fmt" - "github.com/c9s/bbgo/pkg/util" "sync" + "github.com/c9s/bbgo/pkg/util" + "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -256,9 +257,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Sync position to redis on trade s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - if err := s.Persistence.Sync(s); err != nil { - log.WithError(err).Errorf("can not sync state to persistence") - } + bbgo.Sync(s) }) s.stopC = make(chan struct{}) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index e79eca793..ec489530a 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -352,7 +352,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OnSuspend(func() { // Cancel all order _ = s.orderExecutor.GracefulCancel(ctx) - _ = s.Persistence.Sync(s) + bbgo.Sync(s) }) s.OnEmergencyStop(func() {