Skip to content

Commit

Permalink
backtest: avoid inifite float64 JSON serializing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
COLDTURNIP committed Aug 25, 2022
1 parent de59c1b commit de4f372
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/cmd/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"fmt"
"math"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -621,6 +622,17 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
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)
accountConfig := userConfig.Backtest.GetAccount(session.Exchange.Name().String())
initBalances := accountConfig.Balances.BalanceMap()
Expand All @@ -635,8 +647,8 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
InitialBalances: initBalances,
FinalBalances: finalBalances,
// Manifests: manifests,
Sharpe: intervalProfit.GetSharpe(),
Sortino: intervalProfit.GetSortino(),
Sharpe: sharpeRatio,
Sortino: sortinoRatio,
}

for _, s := range session.Subscriptions {
Expand Down

0 comments on commit de4f372

Please sign in to comment.