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)
@ -69,18 +79,18 @@ const (
// Environment presents the real exchange data layer
type Environment struct {
DatabaseService *service.DatabaseService
OrderService *service.OrderService
TradeService *service.TradeService
ProfitService *service.ProfitService
PositionService *service.PositionService
BacktestService *service.BacktestService
RewardService *service.RewardService
MarginService *service.MarginService
SyncService *service.SyncService
AccountService *service.AccountService
WithdrawService *service.WithdrawService
DepositService *service.DepositService
DatabaseService *service.DatabaseService
OrderService *service.OrderService
TradeService *service.TradeService
ProfitService *service.ProfitService
PositionService *service.PositionService
BacktestService *service.BacktestService
RewardService *service.RewardService
MarginService *service.MarginService
SyncService *service.SyncService
AccountService *service.AccountService
WithdrawService *service.WithdrawService
DepositService *service.DepositService
// startTime is the time of start point (which is used in the backtest)
startTime time.Time
@ -105,7 +115,7 @@ func NewEnvironment() *Environment {
sessions: make(map[string]*ExchangeSession),
startTime: now,
syncStatus: SyncNotStarted,
syncStatus: SyncNotStarted,
}
}

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}