backtest: avoid inifite float64 JSON serializing issue

This commit is contained in:
Raphanus Lo 2022-08-24 17:45:34 +08:00
parent de59c1bd13
commit de4f3721a2

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"math"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -621,6 +622,17 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
Market: market, Market: market,
} }
finiteRatio := func(ratio float64) float64 {
if math.IsInf(ratio, 1) {
return 99999.99
} else if math.IsInf(ratio, -1) {
return -99999.99
}
return ratio
}
sharpeRatio := finiteRatio(intervalProfit.GetSharpe())
sortinoRatio := finiteRatio(intervalProfit.GetSortino())
report := calculator.Calculate(symbol, trades, lastPrice) report := calculator.Calculate(symbol, trades, lastPrice)
accountConfig := userConfig.Backtest.GetAccount(session.Exchange.Name().String()) accountConfig := userConfig.Backtest.GetAccount(session.Exchange.Name().String())
initBalances := accountConfig.Balances.BalanceMap() initBalances := accountConfig.Balances.BalanceMap()
@ -635,8 +647,8 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
InitialBalances: initBalances, InitialBalances: initBalances,
FinalBalances: finalBalances, FinalBalances: finalBalances,
// Manifests: manifests, // Manifests: manifests,
Sharpe: intervalProfit.GetSharpe(), Sharpe: sharpeRatio,
Sortino: intervalProfit.GetSortino(), Sortino: sortinoRatio,
} }
for _, s := range session.Subscriptions { for _, s := range session.Subscriptions {