diff --git a/pkg/bbgo/graceful_callbacks.go b/pkg/bbgo/graceful_callbacks.go deleted file mode 100644 index f194e7b2b..000000000 --- a/pkg/bbgo/graceful_callbacks.go +++ /dev/null @@ -1,18 +0,0 @@ -// Code generated by "callbackgen -type Graceful"; DO NOT EDIT. - -package bbgo - -import ( - "context" - "sync" -) - -func (g *Graceful) OnShutdown(cb ShutdownHandler) { - g.shutdownCallbacks = append(g.shutdownCallbacks, cb) -} - -func (g *Graceful) EmitShutdown(ctx context.Context, wg *sync.WaitGroup) { - for _, cb := range g.shutdownCallbacks { - cb(ctx, wg) - } -} diff --git a/pkg/bbgo/graceful_shutdown.go b/pkg/bbgo/graceful_shutdown.go index 4dbf6824a..bc7c1e2a3 100644 --- a/pkg/bbgo/graceful_shutdown.go +++ b/pkg/bbgo/graceful_shutdown.go @@ -8,17 +8,15 @@ import ( "github.com/sirupsen/logrus" ) -var graceful = &Graceful{} - type ShutdownHandler func(ctx context.Context, wg *sync.WaitGroup) -//go:generate callbackgen -type Graceful -type Graceful struct { +//go:generate callbackgen -type GracefulShutdown +type GracefulShutdown struct { shutdownCallbacks []ShutdownHandler } // Shutdown is a blocking call to emit all shutdown callbacks at the same time. -func (g *Graceful) Shutdown(ctx context.Context) { +func (g *GracefulShutdown) Shutdown(ctx context.Context) { var wg sync.WaitGroup wg.Add(len(g.shutdownCallbacks)) @@ -31,14 +29,18 @@ func (g *Graceful) Shutdown(ctx context.Context) { cancel() } -func OnShutdown(f ShutdownHandler) { - graceful.OnShutdown(f) +func OnShutdown(ctx context.Context, f ShutdownHandler) { + isolatedContext := NewIsolationFromContext(ctx) + isolatedContext.gracefulShutdown.OnShutdown(f) } -func Shutdown() { +func Shutdown(ctx context.Context) { logrus.Infof("shutting down...") - ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second) - graceful.Shutdown(ctx) + isolatedContext := NewIsolationFromContext(ctx) + todo := context.WithValue(context.TODO(), IsolationContextKey, isolatedContext) + + timeoutCtx, cancel := context.WithTimeout(todo, 30*time.Second) + defaultIsolation.gracefulShutdown.Shutdown(timeoutCtx) cancel() } diff --git a/pkg/bbgo/gracefulshutdown_callbacks.go b/pkg/bbgo/gracefulshutdown_callbacks.go new file mode 100644 index 000000000..560b956b0 --- /dev/null +++ b/pkg/bbgo/gracefulshutdown_callbacks.go @@ -0,0 +1,18 @@ +// Code generated by "callbackgen -type GracefulShutdown"; DO NOT EDIT. + +package bbgo + +import ( + "context" + "sync" +) + +func (g *GracefulShutdown) OnShutdown(cb ShutdownHandler) { + g.shutdownCallbacks = append(g.shutdownCallbacks, cb) +} + +func (g *GracefulShutdown) EmitShutdown(ctx context.Context, wg *sync.WaitGroup) { + for _, cb := range g.shutdownCallbacks { + cb(ctx, wg) + } +} diff --git a/pkg/bbgo/isolation.go b/pkg/bbgo/isolation.go new file mode 100644 index 000000000..1f11d6be6 --- /dev/null +++ b/pkg/bbgo/isolation.go @@ -0,0 +1,30 @@ +package bbgo + +import ( + "context" +) + +const IsolationContextKey = "bbgo" + +var defaultIsolation *Isolation = nil + +func init() { + defaultIsolation = NewIsolation() +} + +type Isolation struct { + gracefulShutdown GracefulShutdown +} + +func NewIsolation() *Isolation { + return &Isolation{} +} + +func NewIsolationFromContext(ctx context.Context) *Isolation { + isolatedContext, ok := ctx.Value(IsolationContextKey).(*Isolation) + if ok { + return isolatedContext + } + + return defaultIsolation +} diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 0db80870f..85e646681 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -88,6 +88,8 @@ type Trader struct { crossExchangeStrategies []CrossExchangeStrategy exchangeStrategies map[string][]SingleExchangeStrategy + gracefulShutdown GracefulShutdown + logger Logger } @@ -218,8 +220,7 @@ func (trader *Trader) RunSingleExchangeStrategy(ctx context.Context, strategy Si } if shutdown, ok := strategy.(StrategyShutdown); ok { - // Register the shutdown callback - OnShutdown(shutdown.Shutdown) + trader.gracefulShutdown.OnShutdown(shutdown.Shutdown) } return strategy.Run(ctx, orderExecutor, session) @@ -429,6 +430,10 @@ func (trader *Trader) SaveState() error { }) } +func (trader *Trader) Shutdown(ctx context.Context) { + trader.gracefulShutdown.Shutdown(ctx) +} + var defaultPersistenceSelector = &PersistenceSelector{ StoreID: "default", Type: "memory", diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index ad6e3c4c0..7f0afceab 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -484,7 +484,7 @@ var BacktestCmd = &cobra.Command{ cmdutil.WaitForSignal(runCtx, syscall.SIGINT, syscall.SIGTERM) log.Infof("shutting down trader...") - bbgo.Shutdown() + bbgo.Shutdown(ctx) // put the logger back to print the pnl log.SetLevel(log.InfoLevel) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index ef622671b..c09742e01 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -77,7 +77,7 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM) cancelTrading() - bbgo.Shutdown() + bbgo.Shutdown(ctx) return nil } @@ -196,7 +196,7 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM) cancelTrading() - bbgo.Shutdown() + bbgo.Shutdown(ctx) if err := trader.SaveState(); err != nil { log.WithError(err).Errorf("can not save strategy states") diff --git a/pkg/strategy/audacitymaker/strategy.go b/pkg/strategy/audacitymaker/strategy.go index 249603e1d..4904fe9d5 100644 --- a/pkg/strategy/audacitymaker/strategy.go +++ b/pkg/strategy/audacitymaker/strategy.go @@ -6,10 +6,11 @@ import ( "os" "sync" + "github.com/sirupsen/logrus" + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" - "github.com/sirupsen/logrus" ) const ID = "audacitymaker" @@ -99,7 +100,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Cancel active orders _ = s.orderExecutor.GracefulCancel(ctx) // Close 100% position - //_ = s.ClosePosition(ctx, fixedpoint.One) + // _ = s.ClosePosition(ctx, fixedpoint.One) }) // initial required information @@ -123,7 +124,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.OrderFlow.Bind(session, s.orderExecutor) } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String()) diff --git a/pkg/strategy/bollgrid/strategy.go b/pkg/strategy/bollgrid/strategy.go index 676c8fea0..61be95202 100644 --- a/pkg/strategy/bollgrid/strategy.go +++ b/pkg/strategy/bollgrid/strategy.go @@ -347,7 +347,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.profitOrders.BindStream(session.UserDataStream) // setup graceful shutting down handler - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { // call Done to notify the main process. defer wg.Done() log.Infof("canceling active orders...") diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 5be8529b1..e6b4d6049 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -596,7 +596,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // s.book = types.NewStreamBook(s.Symbol) // s.book.BindStreamForBackground(session.MarketDataStream) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _ = s.orderExecutor.GracefulCancel(ctx) diff --git a/pkg/strategy/drift/strategy.go b/pkg/strategy/drift/strategy.go index 9d0c7ce9f..ebaf60078 100644 --- a/pkg/strategy/drift/strategy.go +++ b/pkg/strategy/drift/strategy.go @@ -400,9 +400,9 @@ func (s *Strategy) DrawIndicators(time types.Time) *types.Canvas { hi := s.drift.drift.Abs().Highest(Length) ratio := highestPrice / highestDrift - //canvas.Plot("upband", s.ma.Add(s.stdevHigh), time, Length) + // canvas.Plot("upband", s.ma.Add(s.stdevHigh), time, Length) canvas.Plot("ma", s.ma, time, Length) - //canvas.Plot("downband", s.ma.Minus(s.stdevLow), time, Length) + // canvas.Plot("downband", s.ma.Minus(s.stdevLow), time, Length) fmt.Printf("%f %f\n", highestPrice, hi) canvas.Plot("trend", s.trendLine, time, Length) @@ -844,12 +844,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.highestPrice = 0 s.lowestPrice = 0 } else if s.p.IsLong() { - s.buyPrice = s.p.ApproximateAverageCost.Float64() //trade.Price.Float64() + s.buyPrice = s.p.ApproximateAverageCost.Float64() // trade.Price.Float64() s.sellPrice = 0 s.highestPrice = math.Max(s.buyPrice, s.highestPrice) s.lowestPrice = s.buyPrice } else if s.p.IsShort() { - s.sellPrice = s.p.ApproximateAverageCost.Float64() //trade.Price.Float64() + s.sellPrice = s.p.ApproximateAverageCost.Float64() // trade.Price.Float64() s.buyPrice = 0 s.highestPrice = s.sellPrice if s.lowestPrice == 0 { @@ -951,7 +951,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { var buffer bytes.Buffer diff --git a/pkg/strategy/elliottwave/strategy.go b/pkg/strategy/elliottwave/strategy.go index ce4f55d49..c1f8667f9 100644 --- a/pkg/strategy/elliottwave/strategy.go +++ b/pkg/strategy/elliottwave/strategy.go @@ -10,13 +10,14 @@ import ( "sync" "time" + "github.com/sirupsen/logrus" + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/datatype/floats" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" - "github.com/sirupsen/logrus" ) const ID = "elliottwave" @@ -379,7 +380,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { var buffer bytes.Buffer for _, daypnl := range s.TradeStats.IntervalProfits[types.Interval1d].GetNonProfitableIntervals() { fmt.Fprintf(&buffer, "%s\n", daypnl) diff --git a/pkg/strategy/emastop/strategy.go b/pkg/strategy/emastop/strategy.go index e120c6469..2326f5f78 100644 --- a/pkg/strategy/emastop/strategy.go +++ b/pkg/strategy/emastop/strategy.go @@ -211,7 +211,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.place(ctx, orderExecutor, session, indicator, closePrice) }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() log.Infof("canceling trailingstop order...") s.clear(ctx, orderExecutor) @@ -255,7 +255,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se s.place(ctx, &orderExecutor, session, indicator, closePrice) }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() log.Infof("canceling trailingstop order...") s.clear(ctx, &orderExecutor) diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 668cd69e2..1a4b2c0fb 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -1202,7 +1202,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() log.Infof("canceling active orders...") diff --git a/pkg/strategy/factorzoo/strategy.go b/pkg/strategy/factorzoo/strategy.go index d038a7e16..5b4b890bf 100644 --- a/pkg/strategy/factorzoo/strategy.go +++ b/pkg/strategy/factorzoo/strategy.go @@ -6,10 +6,11 @@ import ( "os" "sync" + "github.com/sirupsen/logrus" + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" - "github.com/sirupsen/logrus" ) const ID = "factorzoo" @@ -97,7 +98,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Cancel active orders _ = s.orderExecutor.GracefulCancel(ctx) // Close 100% position - //_ = s.ClosePosition(ctx, fixedpoint.One) + // _ = s.ClosePosition(ctx, fixedpoint.One) }) // initial required information @@ -121,7 +122,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.Linear.Bind(session, s.orderExecutor) } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String()) diff --git a/pkg/strategy/flashcrash/strategy.go b/pkg/strategy/flashcrash/strategy.go index 4b5c80577..aff86f3ca 100644 --- a/pkg/strategy/flashcrash/strategy.go +++ b/pkg/strategy/flashcrash/strategy.go @@ -110,7 +110,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol) s.activeOrders.BindStream(session.UserDataStream) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() log.Infof("canceling active orders...") diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index 7fc899576..f137f67ce 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -619,7 +619,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se }) s.tradeCollector.BindStream(session.UserDataStream) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() if err := s.SaveState(); err != nil { diff --git a/pkg/strategy/irr/strategy.go b/pkg/strategy/irr/strategy.go index 71bf50c9e..ab4efaece 100644 --- a/pkg/strategy/irr/strategy.go +++ b/pkg/strategy/irr/strategy.go @@ -151,7 +151,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Cancel active orders _ = s.orderExecutor.GracefulCancel(ctx) // Close 100% position - //_ = s.ClosePosition(ctx, fixedpoint.One) + // _ = s.ClosePosition(ctx, fixedpoint.One) }) // initial required information @@ -162,14 +162,14 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) - //modify := func(p float64) float64 { + // modify := func(p float64) float64 { // return p - //} - //if s.GraphPNLDeductFee { + // } + // if s.GraphPNLDeductFee { // modify = func(p float64) float64 { // return p * (1. - Fee) // } - //} + // } profit := floats.Slice{1., 1.} price, _ := s.session.LastPrice(s.Symbol) initAsset := s.CalcAssetValue(price).Float64() @@ -195,8 +195,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.nrr.LoadK((*klines)[0:]) } - //startTime := s.Environment.StartTime() - //s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1h, startTime)) + // startTime := s.Environment.StartTime() + // s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1h, startTime)) s.session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) { @@ -253,7 +253,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se bbgo.SendPhoto(&buffer) }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String()) @@ -270,7 +270,7 @@ func (s *Strategy) CalcAssetValue(price fixedpoint.Value) fixedpoint.Value { func (s *Strategy) DrawPNL(profit types.Series) *types.Canvas { canvas := types.NewCanvas(s.InstanceID()) - //log.Errorf("pnl Highest: %f, Lowest: %f", types.Highest(profit, profit.Length()), types.Lowest(profit, profit.Length())) + // log.Errorf("pnl Highest: %f, Lowest: %f", types.Highest(profit, profit.Length()), types.Lowest(profit, profit.Length())) length := profit.Length() if s.GraphPNLDeductFee { canvas.PlotRaw("pnl (with Fee Deducted)", profit, length) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 4a2395e4b..8659a6370 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -183,7 +183,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.FailedBreakHigh.Bind(session, s.orderExecutor) } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String()) diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 5d95ec6c5..9d60f2b55 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -643,7 +643,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se })) // Graceful shutdown - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() // Output accumulated profit report diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index d8e3ec545..7b8349cd1 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -578,7 +578,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() // Cancel trailing stop order diff --git a/pkg/strategy/trendtrader/strategy.go b/pkg/strategy/trendtrader/strategy.go index c7680f1e1..779d7711b 100644 --- a/pkg/strategy/trendtrader/strategy.go +++ b/pkg/strategy/trendtrader/strategy.go @@ -3,14 +3,16 @@ package trendtrader import ( "context" "fmt" - "github.com/c9s/bbgo/pkg/dynamic" "os" "sync" + "github.com/c9s/bbgo/pkg/dynamic" + + "github.com/sirupsen/logrus" + "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" - "github.com/sirupsen/logrus" ) const ID = "trendtrader" @@ -54,8 +56,8 @@ type Strategy struct { } func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { - //session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Trend.Interval}) - //session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m}) + // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Trend.Interval}) + // session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m}) if s.TrendLine != nil { dynamic.InheritStructValues(s.TrendLine, s) @@ -99,7 +101,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Cancel active orders _ = s.orderExecutor.GracefulCancel(ctx) // Close 100% position - //_ = s.ClosePosition(ctx, fixedpoint.One) + // _ = s.ClosePosition(ctx, fixedpoint.One) }) // initial required information @@ -123,7 +125,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.TrendLine.Bind(session, s.orderExecutor) } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _, _ = fmt.Fprintln(os.Stderr, s.TradeStats.String()) diff --git a/pkg/strategy/wall/strategy.go b/pkg/strategy/wall/strategy.go index ffc9cef4a..cffcea758 100644 --- a/pkg/strategy/wall/strategy.go +++ b/pkg/strategy/wall/strategy.go @@ -376,7 +376,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }() - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() close(s.stopC) diff --git a/pkg/strategy/xbalance/strategy.go b/pkg/strategy/xbalance/strategy.go index b4655ce16..0b8a47cac 100644 --- a/pkg/strategy/xbalance/strategy.go +++ b/pkg/strategy/xbalance/strategy.go @@ -341,7 +341,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se s.State = s.newDefaultState() } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() }) diff --git a/pkg/strategy/xgap/strategy.go b/pkg/strategy/xgap/strategy.go index 0758456ed..195e5cdb8 100644 --- a/pkg/strategy/xgap/strategy.go +++ b/pkg/strategy/xgap/strategy.go @@ -192,7 +192,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se } } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() close(s.stopC) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 7c9e47b37..37f3db9a0 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -880,7 +880,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order } }() - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() close(s.stopC) diff --git a/pkg/strategy/xnav/strategy.go b/pkg/strategy/xnav/strategy.go index daceb344a..d8865b1ce 100644 --- a/pkg/strategy/xnav/strategy.go +++ b/pkg/strategy/xnav/strategy.go @@ -180,7 +180,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se return err } - bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { + bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() s.SaveState()