bbgo_origin/pkg/backtest/report.go

209 lines
7.0 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"`
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.StartPrice)
}
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 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 := filepath.Join(outputDirectory, "index.json")
if err := util.WriteJsonFile(indexFile, reportIndex); err != nil {
return err
}
return nil
}
func LoadReportIndex(outputDirectory string) (*ReportIndex, error) {
var reportIndex ReportIndex
indexFile := filepath.Join(outputDirectory, "index.json")
if _, err := os.Stat(indexFile); err == nil {
o, err := ioutil.ReadFile(indexFile)
if err != nil {
return nil, err
}
if err := json.Unmarshal(o, &reportIndex); err != nil {
return nil, err
}
}
return &reportIndex, nil
}
func AddReportIndexRun(outputDirectory string, run Run) error {
// append report index
2022-05-17 14:41:39 +00:00
lockFile := filepath.Join(outputDirectory, ".report.lock")
fileLock := flock.New(lockFile)
err := fileLock.Lock()
if err != nil {
return err
}
defer func() {
if err := fileLock.Unlock(); err != nil {
log.WithError(err).Errorf("report index file lock error: %s", lockFile)
}
if err := os.Remove(lockFile); err != nil {
2022-05-17 14:41:39 +00:00
log.WithError(err).Errorf("can not remove lock file: %s", lockFile)
}
}()
reportIndex, err := LoadReportIndex(outputDirectory)
if err != nil {
return err
}
reportIndex.Runs = append(reportIndex.Runs, run)
return WriteReportIndex(outputDirectory, 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())
}