Skip to content

Commit

Permalink
all: add context parameter to Sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Oct 3, 2022
1 parent ce318ff commit 8a50474
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 37 deletions.
3 changes: 2 additions & 1 deletion pkg/bbgo/persistence.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bbgo

import (
"context"
"reflect"

log "github.com/sirupsen/logrus"
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/audacitymaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/strategy/bollmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions pkg/strategy/dca/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/drift/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/elliottwave/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/ewoDgtrd/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/factorzoo/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 3 additions & 13 deletions pkg/strategy/grid/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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...")
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/irr/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/pivotshort/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions pkg/strategy/rsmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/schedule/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/skeleton/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/strategy/supertrend/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/support/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/trendtrader/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/xbalance/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/xgap/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/strategy/xnav/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit 8a50474

Please sign in to comment.