Skip to content

Commit

Permalink
xmaker: allocate trading context
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 5, 2024
1 parent 80af5c7 commit f765ab4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ type Strategy struct {
Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`

tradingCtx context.Context
cancelTrading context.CancelFunc

coveredPosition fixedpoint.MutexValue

sourceBook, makerBook *types.StreamOrderBook
Expand Down Expand Up @@ -1862,6 +1865,8 @@ func (s *Strategy) hedgeWorker(ctx context.Context) {
func (s *Strategy) CrossRun(
ctx context.Context, orderExecutionRouter bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession,
) error {
s.tradingCtx, s.cancelTrading = context.WithCancel(ctx)

instanceID := s.InstanceID()

configWriter := bytes.NewBuffer(nil)
Expand Down Expand Up @@ -2034,6 +2039,8 @@ func (s *Strategy) CrossRun(
}

s.CircuitBreaker.OnPanic(func() {
s.cancelTrading()

bbgo.Sync(ctx, s)
})

Expand Down Expand Up @@ -2170,7 +2177,7 @@ func (s *Strategy) CrossRun(

go s.accountUpdater(ctx)
go s.hedgeWorker(ctx)
go s.quoteWorker(ctx)
go s.quoteWorker(s.tradingCtx)
go s.houseCleanWorker(ctx)

if s.RecoverTrade {
Expand All @@ -2179,6 +2186,8 @@ func (s *Strategy) CrossRun(
}()

bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
s.cancelTrading()

// the ctx here is the shutdown context (not the strategy context)

// defer work group done to mark the strategy as stopped
Expand Down

0 comments on commit f765ab4

Please sign in to comment.