Skip to content

Commit 19d8013

Browse files
committed
bbgo: optimize order cancel for back-testing
1 parent 58c819b commit 19d8013

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

pkg/bbgo/activeorderbook.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ func (b *ActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeout ti
6969

7070
// GracefulCancel cancels the active orders gracefully
7171
func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error {
72-
waitTime := CancelOrderWaitTime
72+
// optimize order cancel for back-testing
73+
if IsBackTesting {
74+
orders := b.Orders()
75+
return ex.CancelOrders(context.Background(), orders...)
76+
}
7377

7478
log.Debugf("[ActiveOrderBook] gracefully cancelling %s orders...", b.Symbol)
79+
waitTime := CancelOrderWaitTime
7580

7681
startTime := time.Now()
7782
// ensure every order is cancelled

pkg/bbgo/environment.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func init() {
3737
rand.Seed(time.Now().UnixNano())
3838
}
3939

40+
// IsBackTesting is a global variable that indicates the current environment is back-test or not.
41+
var IsBackTesting = false
42+
43+
var BackTestService *service.BacktestService
44+
45+
func SetBackTesting(s *service.BacktestService) {
46+
BackTestService = s
47+
IsBackTesting = true
48+
}
49+
4050
var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy)
4151
var LoadedCrossExchangeStrategies = make(map[string]CrossExchangeStrategy)
4252

@@ -69,18 +79,18 @@ const (
6979

7080
// Environment presents the real exchange data layer
7181
type Environment struct {
72-
DatabaseService *service.DatabaseService
73-
OrderService *service.OrderService
74-
TradeService *service.TradeService
75-
ProfitService *service.ProfitService
76-
PositionService *service.PositionService
77-
BacktestService *service.BacktestService
78-
RewardService *service.RewardService
79-
MarginService *service.MarginService
80-
SyncService *service.SyncService
81-
AccountService *service.AccountService
82-
WithdrawService *service.WithdrawService
83-
DepositService *service.DepositService
82+
DatabaseService *service.DatabaseService
83+
OrderService *service.OrderService
84+
TradeService *service.TradeService
85+
ProfitService *service.ProfitService
86+
PositionService *service.PositionService
87+
BacktestService *service.BacktestService
88+
RewardService *service.RewardService
89+
MarginService *service.MarginService
90+
SyncService *service.SyncService
91+
AccountService *service.AccountService
92+
WithdrawService *service.WithdrawService
93+
DepositService *service.DepositService
8494

8595
// startTime is the time of start point (which is used in the backtest)
8696
startTime time.Time
@@ -105,7 +115,7 @@ func NewEnvironment() *Environment {
105115
sessions: make(map[string]*ExchangeSession),
106116
startTime: now,
107117

108-
syncStatus: SyncNotStarted,
118+
syncStatus: SyncNotStarted,
109119
}
110120
}
111121

pkg/cmd/backtest.go

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ var BacktestCmd = &cobra.Command{
160160

161161
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
162162
environ.BacktestService = backtestService
163+
bbgo.SetBackTesting(backtestService)
163164

164165
if len(sessionName) > 0 {
165166
userConfig.Backtest.Sessions = []string{sessionName}

0 commit comments

Comments
 (0)