Merge pull request #1384 from c9s/c9s/backtest-minor-improvements

CHORE: minor improvements on backtest cmd
This commit is contained in:
c9s 2023-11-01 15:35:07 +08:00 committed by GitHub
commit a8ddf9a8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -382,6 +382,14 @@ func (e *Exchange) SubscribeMarketData(
} }
log.Infof("querying klines from database with exchange: %v symbols: %v and intervals: %v for back-testing", e.Name(), symbols, intervals) log.Infof("querying klines from database with exchange: %v symbols: %v and intervals: %v for back-testing", e.Name(), symbols, intervals)
if len(symbols) == 0 {
log.Warnf("empty symbols, will not query kline data from the database")
c := make(chan types.KLine)
close(c)
return c, nil
}
klineC, errC := e.srv.QueryKLinesCh(startTime, endTime, e, symbols, intervals) klineC, errC := e.srv.QueryKLinesCh(startTime, endTime, e, symbols, intervals)
go func() { go func() {
if err := <-errC; err != nil { if err := <-errC; err != nil {

View File

@ -528,6 +528,11 @@ var BacktestCmd = &cobra.Command{
for _, session := range environ.Sessions() { for _, session := range environ.Sessions() {
for symbol, trades := range session.Trades { for symbol, trades := range session.Trades {
if len(trades.Trades) == 0 {
log.Warnf("session has no %s trades", symbol)
continue
}
tradeState := sessionTradeStats[session.Name][symbol] tradeState := sessionTradeStats[session.Name][symbol]
profitFactor := tradeState.ProfitFactor profitFactor := tradeState.ProfitFactor
winningRatio := tradeState.WinningRatio winningRatio := tradeState.WinningRatio
@ -598,8 +603,11 @@ var BacktestCmd = &cobra.Command{
}, },
} }
func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession, symbol string, trades []types.Trade, intervalProfit *types.IntervalProfitCollector, func createSymbolReport(
profitFactor, winningRatio fixedpoint.Value) ( userConfig *bbgo.Config, session *bbgo.ExchangeSession, symbol string, trades []types.Trade,
intervalProfit *types.IntervalProfitCollector,
profitFactor, winningRatio fixedpoint.Value,
) (
*backtest.SessionSymbolReport, *backtest.SessionSymbolReport,
error, error,
) { ) {
@ -669,7 +677,10 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
return &symbolReport, nil return &symbolReport, nil
} }
func verify(userConfig *bbgo.Config, backtestService *service.BacktestService, sourceExchanges map[types.ExchangeName]types.Exchange, startTime, endTime time.Time) error { func verify(
userConfig *bbgo.Config, backtestService *service.BacktestService,
sourceExchanges map[types.ExchangeName]types.Exchange, startTime, endTime time.Time,
) error {
for _, sourceExchange := range sourceExchanges { for _, sourceExchange := range sourceExchanges {
err := backtestService.Verify(sourceExchange, userConfig.Backtest.Symbols, startTime, endTime) err := backtestService.Verify(sourceExchange, userConfig.Backtest.Symbols, startTime, endTime)
if err != nil { if err != nil {
@ -709,7 +720,10 @@ func getExchangeIntervals(ex types.Exchange) types.IntervalMap {
return types.SupportedIntervals return types.SupportedIntervals
} }
func sync(ctx context.Context, userConfig *bbgo.Config, backtestService *service.BacktestService, sourceExchanges map[types.ExchangeName]types.Exchange, syncFrom, syncTo time.Time) error { func sync(
ctx context.Context, userConfig *bbgo.Config, backtestService *service.BacktestService,
sourceExchanges map[types.ExchangeName]types.Exchange, syncFrom, syncTo time.Time,
) error {
for _, symbol := range userConfig.Backtest.Symbols { for _, symbol := range userConfig.Backtest.Symbols {
for _, sourceExchange := range sourceExchanges { for _, sourceExchange := range sourceExchanges {
var supportIntervals = getExchangeIntervals(sourceExchange) var supportIntervals = getExchangeIntervals(sourceExchange)