grid2: add TestBacktestStrategy skeleton for backtesting in unit test

This commit is contained in:
c9s 2022-12-06 02:30:18 +08:00
parent 35297b9bbf
commit d9e230a433
2 changed files with 86 additions and 1 deletions

View File

@ -17,7 +17,7 @@ sessions:
# go run ./cmd/bbgo backtest --config config/grid2.yaml --base-asset-baseline
backtest:
startTime: "2021-06-01"
endTime: "2021-12-31"
endTime: "2021-06-30"
symbols:
- BTCUSDT
sessions: [binance]

View File

@ -3,12 +3,17 @@
package grid2
import (
"context"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/backtest"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
)
@ -322,3 +327,83 @@ func TestStrategy_calculateProfit(t *testing.T) {
assert.InDelta(t, sellQuantity.Float64()-buyOrder.Quantity.Float64(), profit.Profit.Float64(), 0.001)
})
}
func TestBacktestStrategy(t *testing.T) {
startTime, err := types.ParseLooseFormatTime("2021-06-01")
assert.NoError(t, err)
endTime, err := types.ParseLooseFormatTime("2021-06-30")
assert.NoError(t, err)
backtestConfig := &bbgo.Backtest{
StartTime: startTime,
EndTime: &endTime,
RecordTrades: false,
FeeMode: bbgo.BacktestFeeModeToken,
Accounts: map[string]bbgo.BacktestAccount{
"binance": {
MakerFeeRate: number(0.075 * 0.01),
TakerFeeRate: number(0.075 * 0.01),
Balances: bbgo.BacktestAccountBalanceMap{
"USDT": number(10_000.0),
"BTC": number(1.0),
},
},
},
Symbols: []string{"BTCUSDT"},
Sessions: []string{"binance"},
SyncSecKLines: false,
}
t.Logf("backtestConfig: %+v", backtestConfig)
ctx := context.Background()
environ := bbgo.NewEnvironment()
err = environ.ConfigureDatabaseDriver(ctx, "sqlite3", "../../../data/bbgo_test.sqlite3")
assert.NoError(t, err)
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
defer func() {
err := environ.DatabaseService.DB.Close()
assert.NoError(t, err)
}()
environ.BacktestService = backtestService
bbgo.SetBackTesting(backtestService)
defer bbgo.SetBackTesting(nil)
exName, err := types.ValidExchangeName("binance")
if !assert.NoError(t, err) {
return
}
publicExchange, err := exchange.NewPublic(exName)
if !assert.NoError(t, err) {
return
}
backtestExchange, err := backtest.NewExchange(publicExchange.Name(), publicExchange, backtestService, backtestConfig)
if !assert.NoError(t, err) {
return
}
session := environ.AddExchange(exName.String(), backtestExchange)
assert.NotNil(t, session)
err = environ.Init(ctx)
assert.NoError(t, err)
for _, ses := range environ.Sessions() {
userDataStream := ses.UserDataStream.(types.StandardStreamEmitter)
backtestEx := ses.Exchange.(*backtest.Exchange)
backtestEx.MarketDataStream = ses.MarketDataStream.(types.StandardStreamEmitter)
backtestEx.BindUserData(userDataStream)
}
trader := bbgo.NewTrader(environ)
if assert.NotNil(t, trader) {
trader.DisableLogging()
}
// TODO: add grid2 to the user config and run backtest
}