From 8a50474ad1916cdafe6ba96b0a26687c5f801047 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 3 Oct 2022 18:45:24 +0800 Subject: [PATCH] all: add context parameter to Sync() --- pkg/bbgo/persistence.go | 3 ++- pkg/strategy/audacitymaker/strategy.go | 2 +- pkg/strategy/bollmaker/strategy.go | 4 ++-- pkg/strategy/dca/strategy.go | 3 +-- pkg/strategy/drift/strategy.go | 2 +- pkg/strategy/elliottwave/strategy.go | 2 +- pkg/strategy/ewoDgtrd/strategy.go | 2 +- pkg/strategy/factorzoo/strategy.go | 2 +- pkg/strategy/grid/strategy.go | 16 +++------------- pkg/strategy/irr/strategy.go | 2 +- pkg/strategy/pivotshort/strategy.go | 2 +- pkg/strategy/rsmaker/strategy.go | 4 ++-- pkg/strategy/schedule/strategy.go | 2 +- pkg/strategy/skeleton/strategy.go | 2 +- pkg/strategy/supertrend/strategy.go | 4 ++-- pkg/strategy/support/strategy.go | 2 +- pkg/strategy/trendtrader/strategy.go | 2 +- pkg/strategy/xbalance/strategy.go | 2 +- pkg/strategy/xgap/strategy.go | 2 +- pkg/strategy/xnav/strategy.go | 4 ++-- 20 files changed, 27 insertions(+), 37 deletions(-) diff --git a/pkg/bbgo/persistence.go b/pkg/bbgo/persistence.go index db99c20b8..f200c2762 100644 --- a/pkg/bbgo/persistence.go +++ b/pkg/bbgo/persistence.go @@ -1,6 +1,7 @@ package bbgo import ( + "context" "reflect" log "github.com/sirupsen/logrus" @@ -16,7 +17,7 @@ var DefaultPersistenceServiceFacade = &service.PersistenceServiceFacade{ var PersistenceServiceFacade = DefaultPersistenceServiceFacade // Sync syncs the object properties into the persistence layer -func Sync(obj interface{}) { +func Sync(ctx context.Context, obj interface{}) { id := dynamic.CallID(obj) if len(id) == 0 { log.Warnf("InstanceID() is not provided, can not sync persistence") diff --git a/pkg/strategy/audacitymaker/strategy.go b/pkg/strategy/audacitymaker/strategy.go index 4904fe9d5..c87b16f02 100644 --- a/pkg/strategy/audacitymaker/strategy.go +++ b/pkg/strategy/audacitymaker/strategy.go @@ -111,7 +111,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index e6b4d6049..959bdff73 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -516,7 +516,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.Bind() s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.ExitMethods.Bind(session, s.orderExecutor) @@ -531,7 +531,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OnSuspend(func() { _ = s.orderExecutor.GracefulCancel(ctx) - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.OnEmergencyStop(func() { diff --git a/pkg/strategy/dca/strategy.go b/pkg/strategy/dca/strategy.go index 38e6c9d14..db6f1abda 100644 --- a/pkg/strategy/dca/strategy.go +++ b/pkg/strategy/dca/strategy.go @@ -47,7 +47,6 @@ func (b BudgetPeriod) Duration() time.Duration { // Strategy is the Dollar-Cost-Average strategy type Strategy struct { - Environment *bbgo.Environment Symbol string `json:"symbol"` Market types.Market @@ -110,7 +109,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. s.orderExecutor.BindEnvironment(s.Environment) s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() diff --git a/pkg/strategy/drift/strategy.go b/pkg/strategy/drift/strategy.go index ebaf60078..5c8f01047 100644 --- a/pkg/strategy/drift/strategy.go +++ b/pkg/strategy/drift/strategy.go @@ -795,7 +795,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.GeneralOrderExecutor.BindProfitStats(s.ProfitStats) s.GeneralOrderExecutor.BindTradeStats(s.TradeStats) s.GeneralOrderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.GeneralOrderExecutor.Bind() diff --git a/pkg/strategy/elliottwave/strategy.go b/pkg/strategy/elliottwave/strategy.go index c1f8667f9..363429b67 100644 --- a/pkg/strategy/elliottwave/strategy.go +++ b/pkg/strategy/elliottwave/strategy.go @@ -310,7 +310,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.GeneralOrderExecutor.BindProfitStats(s.ProfitStats) s.GeneralOrderExecutor.BindTradeStats(s.TradeStats) s.GeneralOrderExecutor.TradeCollector().OnPositionUpdate(func(p *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.GeneralOrderExecutor.Bind() diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 1a4b2c0fb..6af38bc11 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -708,7 +708,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) // s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() diff --git a/pkg/strategy/factorzoo/strategy.go b/pkg/strategy/factorzoo/strategy.go index 5b4b890bf..b332dc8e6 100644 --- a/pkg/strategy/factorzoo/strategy.go +++ b/pkg/strategy/factorzoo/strategy.go @@ -109,7 +109,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol) diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index 2b9a70ec2..323d1a17a 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -533,14 +533,6 @@ func (s *Strategy) LoadState() error { return nil } -func (s *Strategy) SaveState() error { - log.Infof("backing up grid state...") - submitOrders := s.activeOrders.Backup() - s.State.Orders = submitOrders - bbgo.Sync(s) - return nil -} - // InstanceID returns the instance identifier from the current grid configuration parameters func (s *Strategy) InstanceID() string { return fmt.Sprintf("%s-%s-%d-%d-%d", ID, s.Symbol, s.GridNum, s.UpperPrice.Int(), s.LowerPrice.Int()) @@ -603,11 +595,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - if err := s.SaveState(); err != nil { - log.WithError(err).Errorf("can not save state: %+v", s.State) - } else { - bbgo.Notify("%s: %s grid is saved", ID, s.Symbol) - } + submitOrders := s.activeOrders.Backup() + s.State.Orders = submitOrders + bbgo.Sync(ctx, s) // now we can cancel the open orders log.Infof("canceling active orders...") diff --git a/pkg/strategy/irr/strategy.go b/pkg/strategy/irr/strategy.go index ab4efaece..2c77a1b3f 100644 --- a/pkg/strategy/irr/strategy.go +++ b/pkg/strategy/irr/strategy.go @@ -179,7 +179,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se cumProfit.Update(s.CalcAssetValue(trade.Price).Float64()) }) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 8659a6370..8774b1ab8 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -165,7 +165,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() diff --git a/pkg/strategy/rsmaker/strategy.go b/pkg/strategy/rsmaker/strategy.go index a9fb5b8ad..2dd477a8f 100644 --- a/pkg/strategy/rsmaker/strategy.go +++ b/pkg/strategy/rsmaker/strategy.go @@ -175,7 +175,7 @@ func (s *Strategy) Suspend(ctx context.Context) error { log.WithError(err).Errorf("graceful cancel order error") } - bbgo.Sync(s) + bbgo.Sync(ctx, s) return nil } @@ -416,7 +416,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() diff --git a/pkg/strategy/schedule/strategy.go b/pkg/strategy/schedule/strategy.go index 953d4794b..a64759237 100644 --- a/pkg/strategy/schedule/strategy.go +++ b/pkg/strategy/schedule/strategy.go @@ -88,7 +88,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se instanceID := s.InstanceID() s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() diff --git a/pkg/strategy/skeleton/strategy.go b/pkg/strategy/skeleton/strategy.go index 048da9745..536aaae10 100644 --- a/pkg/strategy/skeleton/strategy.go +++ b/pkg/strategy/skeleton/strategy.go @@ -104,7 +104,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Update our counter and sync the changes to the persistence layer on time // If you don't do this, BBGO will sync it automatically when BBGO shuts down. s.State.Counter++ - bbgo.Sync(s) + bbgo.Sync(ctx, s) // To check if we have the quote balance // When symbol = "BTCUSDT", the quote currency is USDT diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 9d60f2b55..c40a01854 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -544,14 +544,14 @@ 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) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) // StrategyController s.Status = types.StrategyStatusRunning s.OnSuspend(func() { _ = s.orderExecutor.GracefulCancel(ctx) - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.OnEmergencyStop(func() { _ = s.orderExecutor.GracefulCancel(ctx) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index 2c29bd44f..d59788a3a 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -346,7 +346,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OnSuspend(func() { // Cancel all order _ = s.orderExecutor.GracefulCancel(ctx) - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.OnEmergencyStop(func() { diff --git a/pkg/strategy/trendtrader/strategy.go b/pkg/strategy/trendtrader/strategy.go index 779d7711b..3feedfa75 100644 --- a/pkg/strategy/trendtrader/strategy.go +++ b/pkg/strategy/trendtrader/strategy.go @@ -112,7 +112,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) { - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) s.orderExecutor.Bind() s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol) diff --git a/pkg/strategy/xbalance/strategy.go b/pkg/strategy/xbalance/strategy.go index 0b8a47cac..717538246 100644 --- a/pkg/strategy/xbalance/strategy.go +++ b/pkg/strategy/xbalance/strategy.go @@ -279,7 +279,7 @@ func (s *Strategy) checkBalance(ctx context.Context, sessions map[string]*bbgo.E s.State.DailyNumberOfTransfers += 1 s.State.DailyAmountOfTransfers = s.State.DailyAmountOfTransfers.Add(requiredAmount) - bbgo.Sync(s) + bbgo.Sync(ctx, s) } } diff --git a/pkg/strategy/xgap/strategy.go b/pkg/strategy/xgap/strategy.go index 12e6050e2..fd0b67f60 100644 --- a/pkg/strategy/xgap/strategy.go +++ b/pkg/strategy/xgap/strategy.go @@ -182,7 +182,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() close(s.stopC) - bbgo.Sync(s) + bbgo.Sync(context.Background(), s) }) // from here, set data binding diff --git a/pkg/strategy/xnav/strategy.go b/pkg/strategy/xnav/strategy.go index addf67487..b0514d946 100644 --- a/pkg/strategy/xnav/strategy.go +++ b/pkg/strategy/xnav/strategy.go @@ -129,7 +129,7 @@ func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string] if s.State.IsOver24Hours() { s.State.Reset() } - bbgo.Sync(s) + bbgo.Sync(ctx, s) } } @@ -146,7 +146,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - bbgo.Sync(s) + bbgo.Sync(ctx, s) }) if s.ReportOnStart {