bbgo: optimize order cancel for back-testing

This commit is contained in:
c9s 2022-06-21 01:12:16 +08:00
parent 58c819bd75
commit 19d8013f49
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 30 additions and 14 deletions

View File

@ -69,9 +69,14 @@ func (b *ActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeout ti
// GracefulCancel cancels the active orders gracefully
func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error {
waitTime := CancelOrderWaitTime
// optimize order cancel for back-testing
if IsBackTesting {
orders := b.Orders()
return ex.CancelOrders(context.Background(), orders...)
}
log.Debugf("[ActiveOrderBook] gracefully cancelling %s orders...", b.Symbol)
waitTime := CancelOrderWaitTime
startTime := time.Now()
// ensure every order is cancelled

View File

@ -37,6 +37,16 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
// IsBackTesting is a global variable that indicates the current environment is back-test or not.
var IsBackTesting = false
var BackTestService *service.BacktestService
func SetBackTesting(s *service.BacktestService) {
BackTestService = s
IsBackTesting = true
}
var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy)
var LoadedCrossExchangeStrategies = make(map[string]CrossExchangeStrategy)

View File

@ -160,6 +160,7 @@ var BacktestCmd = &cobra.Command{
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
environ.BacktestService = backtestService
bbgo.SetBackTesting(backtestService)
if len(sessionName) > 0 {
userConfig.Backtest.Sessions = []string{sessionName}