bbgo_origin/pkg/backtest/report.go

260 lines
8.6 KiB
Go
Raw Normal View History

2022-05-06 11:16:15 +00:00
package backtest
import (
2022-05-17 10:23:09 +00:00
"encoding/json"
2022-05-09 11:27:02 +00:00
"fmt"
2022-05-17 10:23:09 +00:00
"io/ioutil"
"os"
"path/filepath"
2022-05-09 11:27:02 +00:00
"strings"
"time"
2022-05-17 14:31:50 +00:00
"github.com/fatih/color"
2022-05-17 14:41:39 +00:00
"github.com/gofrs/flock"
2022-05-17 14:31:50 +00:00
2022-05-06 11:16:15 +00:00
"github.com/c9s/bbgo/pkg/accounting/pnl"
2022-05-17 10:10:37 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
2022-05-06 11:16:15 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
2022-05-17 10:23:09 +00:00
"github.com/c9s/bbgo/pkg/util"
2022-05-06 11:16:15 +00:00
)
2022-05-17 10:10:37 +00:00
type Run struct {
ID string `json:"id"`
Config *bbgo.Config `json:"config"`
Time time.Time `json:"time"`
}
type ReportIndex struct {
Runs []Run `json:"runs,omitempty"`
}
2022-05-10 10:27:23 +00:00
// SummaryReport is the summary of the back-test session
type SummaryReport struct {
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Sessions []string `json:"sessions"`
2022-05-17 10:10:37 +00:00
Symbols []string `json:"symbols"`
2022-05-19 01:48:36 +00:00
Intervals []types.Interval `json:"intervals"`
2022-05-10 10:27:23 +00:00
InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"`
FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"`
2022-05-17 14:31:50 +00:00
InitialEquityValue fixedpoint.Value `json:"initialEquityValue"`
FinalEquityValue fixedpoint.Value `json:"finalEquityValue"`
// TotalProfit is the profit aggregated from the symbol reports
TotalProfit fixedpoint.Value `json:"totalProfit,omitempty"`
TotalUnrealizedProfit fixedpoint.Value `json:"totalUnrealizedProfit,omitempty"`
TotalGrossProfit fixedpoint.Value `json:"totalGrossProfit,omitempty"`
TotalGrossLoss fixedpoint.Value `json:"totalGrossLoss,omitempty"`
2022-05-17 14:31:50 +00:00
SymbolReports []SessionSymbolReport `json:"symbolReports,omitempty"`
Manifests Manifests `json:"manifests,omitempty"`
2022-05-10 10:27:23 +00:00
}
2022-05-19 12:31:25 +00:00
func ReadSummaryReport(filename string) (*SummaryReport, error) {
o, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var report SummaryReport
err = json.Unmarshal(o, &report)
return &report, err
}
2022-05-10 10:27:23 +00:00
// SessionSymbolReport is the report per exchange session
// trades are merged, collected and re-calculated
type SessionSymbolReport struct {
2022-05-17 10:45:06 +00:00
Exchange types.ExchangeName `json:"exchange"`
2022-05-06 11:16:15 +00:00
Symbol string `json:"symbol,omitempty"`
2022-05-19 01:48:36 +00:00
Intervals []types.Interval `json:"intervals,omitempty"`
Subscriptions []types.Subscription `json:"subscriptions"`
2022-05-17 10:45:06 +00:00
Market types.Market `json:"market"`
2022-05-06 11:16:15 +00:00
LastPrice fixedpoint.Value `json:"lastPrice,omitempty"`
StartPrice fixedpoint.Value `json:"startPrice,omitempty"`
2022-05-17 14:31:50 +00:00
PnL *pnl.AverageCostPnlReport `json:"pnl,omitempty"`
2022-05-06 11:16:15 +00:00
InitialBalances types.BalanceMap `json:"initialBalances,omitempty"`
FinalBalances types.BalanceMap `json:"finalBalances,omitempty"`
Manifests Manifests `json:"manifests,omitempty"`
Sharpe float64 `json:"sharpeRatio"`
Sortino float64 `json:"sortinoRatio"`
2022-05-06 11:16:15 +00:00
}
2022-05-09 10:03:03 +00:00
func (r *SessionSymbolReport) InitialEquityValue() fixedpoint.Value {
return InQuoteAsset(r.InitialBalances, r.Market, r.StartPrice)
}
func (r *SessionSymbolReport) FinalEquityValue() fixedpoint.Value {
return InQuoteAsset(r.FinalBalances, r.Market, r.LastPrice)
}
2022-05-17 14:31:50 +00:00
func (r *SessionSymbolReport) Print(wantBaseAssetBaseline bool) {
color.Green("%s %s PROFIT AND LOSS REPORT", r.Exchange, r.Symbol)
color.Green("===============================================")
r.PnL.Print()
initQuoteAsset := r.InitialEquityValue()
finalQuoteAsset := r.FinalEquityValue()
2022-05-17 14:31:50 +00:00
color.Green("INITIAL ASSET IN %s ~= %s %s (1 %s = %v)", r.Market.QuoteCurrency, r.Market.FormatQuantity(initQuoteAsset), r.Market.QuoteCurrency, r.Market.BaseCurrency, r.StartPrice)
color.Green("FINAL ASSET IN %s ~= %s %s (1 %s = %v)", r.Market.QuoteCurrency, r.Market.FormatQuantity(finalQuoteAsset), r.Market.QuoteCurrency, r.Market.BaseCurrency, r.LastPrice)
if r.PnL.Profit.Sign() > 0 {
color.Green("REALIZED PROFIT: +%v %s", r.PnL.Profit, r.Market.QuoteCurrency)
} else {
color.Red("REALIZED PROFIT: %v %s", r.PnL.Profit, r.Market.QuoteCurrency)
}
if r.PnL.UnrealizedProfit.Sign() > 0 {
color.Green("UNREALIZED PROFIT: +%v %s", r.PnL.UnrealizedProfit, r.Market.QuoteCurrency)
} else {
color.Red("UNREALIZED PROFIT: %v %s", r.PnL.UnrealizedProfit, r.Market.QuoteCurrency)
}
if finalQuoteAsset.Compare(initQuoteAsset) > 0 {
color.Green("ASSET INCREASED: +%v %s (+%s)", finalQuoteAsset.Sub(initQuoteAsset), r.Market.QuoteCurrency, finalQuoteAsset.Sub(initQuoteAsset).Div(initQuoteAsset).FormatPercentage(2))
} else {
color.Red("ASSET DECREASED: %v %s (%s)", finalQuoteAsset.Sub(initQuoteAsset), r.Market.QuoteCurrency, finalQuoteAsset.Sub(initQuoteAsset).Div(initQuoteAsset).FormatPercentage(2))
}
if r.Sharpe > 0.0 {
color.Green("REALIZED SHARPE RATIO: %.4f", r.Sharpe)
} else {
color.Red("REALIZED SHARPE RATIO: %.4f", r.Sharpe)
}
if r.Sortino > 0.0 {
color.Green("REALIZED SORTINO RATIO: %.4f", r.Sortino)
} else {
color.Red("REALIZED SORTINO RATIO: %.4f", r.Sortino)
}
2022-05-17 14:31:50 +00:00
if wantBaseAssetBaseline {
if r.LastPrice.Compare(r.StartPrice) > 0 {
color.Green("%s BASE ASSET PERFORMANCE: +%s (= (%s - %s) / %s)",
r.Market.BaseCurrency,
r.LastPrice.Sub(r.StartPrice).Div(r.StartPrice).FormatPercentage(2),
r.LastPrice.FormatString(2),
r.StartPrice.FormatString(2),
r.StartPrice.FormatString(2))
} else {
color.Red("%s BASE ASSET PERFORMANCE: %s (= (%s - %s) / %s)",
r.Market.BaseCurrency,
r.LastPrice.Sub(r.StartPrice).Div(r.StartPrice).FormatPercentage(2),
r.LastPrice.FormatString(2),
r.StartPrice.FormatString(2),
r.StartPrice.FormatString(2))
}
}
}
2022-05-09 11:27:02 +00:00
const SessionTimeFormat = "2006-01-02T15_04"
// FormatSessionName returns the back-test session name
func FormatSessionName(sessions []string, symbols []string, startTime, endTime time.Time) string {
return fmt.Sprintf("%s_%s_%s-%s",
strings.Join(sessions, "-"),
strings.Join(symbols, "-"),
startTime.Format(SessionTimeFormat),
endTime.Format(SessionTimeFormat),
)
}
2022-05-17 10:23:09 +00:00
func WriteReportIndex(outputDirectory string, reportIndex *ReportIndex) error {
indexFile := getReportIndexPath(outputDirectory)
indexLock := flock.New(indexFile)
if err := indexLock.Lock(); err != nil {
log.WithError(err).Errorf("report index file lock error while write report: %s", err)
2022-05-17 10:23:09 +00:00
return err
}
defer func() {
if err := indexLock.Unlock(); err != nil {
log.WithError(err).Errorf("report index file unlock error while write report: %s", err)
}
}()
return writeReportIndexLocked(outputDirectory, reportIndex)
2022-05-17 10:23:09 +00:00
}
func LoadReportIndex(outputDirectory string) (*ReportIndex, error) {
indexFile := getReportIndexPath(outputDirectory)
indexLock := flock.New(indexFile)
2022-05-17 10:23:09 +00:00
if err := indexLock.Lock(); err != nil {
log.WithError(err).Errorf("report index file lock error while load report: %s", err)
return nil, err
2022-05-17 10:23:09 +00:00
}
defer func() {
if err := indexLock.Unlock(); err != nil {
log.WithError(err).Errorf("report index file unlock error while load report: %s", err)
}
}()
2022-05-17 10:23:09 +00:00
return loadReportIndexLocked(indexFile)
2022-05-17 10:23:09 +00:00
}
func AddReportIndexRun(outputDirectory string, run Run) error {
// append report index
indexFile := getReportIndexPath(outputDirectory)
indexLock := flock.New(indexFile)
2022-05-17 14:41:39 +00:00
if err := indexLock.Lock(); err != nil {
log.WithError(err).Errorf("report index file lock error: %s", err)
2022-05-17 14:41:39 +00:00
return err
}
defer func() {
if err := indexLock.Unlock(); err != nil {
log.WithError(err).Errorf("report index file unlock error: %s", err)
2022-05-17 14:41:39 +00:00
}
}()
reportIndex, err := loadReportIndexLocked(indexFile)
if err != nil {
return err
}
reportIndex.Runs = append(reportIndex.Runs, run)
return writeReportIndexLocked(indexFile, reportIndex)
}
2022-05-17 14:31:50 +00:00
// InQuoteAsset converts all balances in quote asset
func InQuoteAsset(balances types.BalanceMap, market types.Market, price fixedpoint.Value) fixedpoint.Value {
2022-05-17 14:31:50 +00:00
quote := balances[market.QuoteCurrency]
base := balances[market.BaseCurrency]
return base.Total().Mul(price).Add(quote.Total())
}
func getReportIndexPath(outputDirectory string) string {
return filepath.Join(outputDirectory, "index.json")
}
// writeReportIndexLocked must be protected by file lock
func writeReportIndexLocked(indexFilePath string, reportIndex *ReportIndex) error {
if err := util.WriteJsonFile(indexFilePath, reportIndex); err != nil {
return err
}
return nil
}
// loadReportIndexLocked must be protected by file lock
func loadReportIndexLocked(indexFilePath string) (*ReportIndex, error) {
var reportIndex ReportIndex
if fileInfo, err := os.Stat(indexFilePath); err != nil {
return nil, err
} else if fileInfo.Size() != 0 {
o, err := ioutil.ReadFile(indexFilePath)
if err != nil {
return nil, err
}
if err := json.Unmarshal(o, &reportIndex); err != nil {
return nil, err
}
}
return &reportIndex, nil
}