aggregate total profit and total unrealized profit

This commit is contained in:
c9s 2022-05-19 18:45:45 +08:00
parent 11ad34e9f7
commit 960f967c34
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 31 additions and 16 deletions

View File

@ -39,6 +39,10 @@ type SummaryReport struct {
InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"` InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"`
FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"` FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"`
// TotalProfit is the profit aggregated from the symbol reports
TotalProfit fixedpoint.Value `json:"totalProfit,omitempty"`
TotalUnrealizedProfit fixedpoint.Value `json:"totalUnrealizedProfit,omitempty"`
SymbolReports []SessionSymbolReport `json:"symbolReports,omitempty"` SymbolReports []SessionSymbolReport `json:"symbolReports,omitempty"`
Manifests Manifests `json:"manifests,omitempty"` Manifests Manifests `json:"manifests,omitempty"`

View File

@ -492,6 +492,8 @@ var BacktestCmd = &cobra.Command{
summaryReport.Symbols = append(summaryReport.Symbols, symbol) summaryReport.Symbols = append(summaryReport.Symbols, symbol)
summaryReport.SymbolReports = append(summaryReport.SymbolReports, *symbolReport) summaryReport.SymbolReports = append(summaryReport.SymbolReports, *symbolReport)
summaryReport.TotalProfit = symbolReport.PnL.Profit
summaryReport.TotalUnrealizedProfit = symbolReport.PnL.UnrealizedProfit
// write report to a file // write report to a file
if generatingReport { if generatingReport {
@ -500,24 +502,32 @@ var BacktestCmd = &cobra.Command{
return err return err
} }
} }
symbolReport.Print(wantBaseAssetBaseline)
} }
} }
if generatingReport && reportFileInSubDir { if generatingReport {
summaryReportFile := filepath.Join(reportDir, "summary.json") summaryReportFile := filepath.Join(reportDir, "summary.json")
// output summary report filepath to stdout, so that our optimizer can read from it
fmt.Println(summaryReportFile)
if err := util.WriteJsonFile(summaryReportFile, summaryReport); err != nil { if err := util.WriteJsonFile(summaryReportFile, summaryReport); err != nil {
return err return err
} }
// append report index // append report index
if err := backtest.AddReportIndexRun(outputDirectory, backtest.Run{ if reportFileInSubDir {
ID: runID, if err := backtest.AddReportIndexRun(outputDirectory, backtest.Run{
Config: userConfig, ID: runID,
Time: time.Now(), Config: userConfig,
}); err != nil { Time: time.Now(),
return err }); err != nil {
return err
}
}
} else {
for _, symbolReport := range summaryReport.SymbolReports {
symbolReport.Print(wantBaseAssetBaseline)
} }
} }

View File

@ -44,8 +44,7 @@ func (e *LocalProcessExecutor) Execute(configJson []byte) error {
} }
c := exec.Command(e.Bin, "backtest", "--config", tf.Name(), "--output", e.OutputDir, "--subdir") c := exec.Command(e.Bin, "backtest", "--config", tf.Name(), "--output", e.OutputDir, "--subdir")
log.Infof("cmd: %+v", c) // c.Output()
if e.CombineOutput { if e.CombineOutput {
c.Stdout = os.Stdout c.Stdout = os.Stdout
c.Stderr = os.Stderr c.Stderr = os.Stderr

View File

@ -6,6 +6,8 @@ import (
"strings" "strings"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
) )
@ -60,7 +62,6 @@ func (b Balance) String() (o string) {
return o return o
} }
type BalanceSnapshot struct { type BalanceSnapshot struct {
Balances BalanceMap `json:"balances"` Balances BalanceMap `json:"balances"`
Session string `json:"session"` Session string `json:"session"`
@ -191,15 +192,16 @@ func (m BalanceMap) Print() {
continue continue
} }
fmt.Printf(" %s: %v", balance.Currency, balance.Available) o := fmt.Sprintf(" %s: %v", balance.Currency, balance.Available)
if balance.Locked.Sign() > 0 { if balance.Locked.Sign() > 0 {
fmt.Printf(" (locked %v)", balance.Locked) o += fmt.Sprintf(" (locked %v)", balance.Locked)
} }
if balance.Borrowed.Sign() > 0 { if balance.Borrowed.Sign() > 0 {
fmt.Printf(" (borrowed %v)", balance.Borrowed) o += fmt.Sprintf(" (borrowed %v)", balance.Borrowed)
} }
fmt.Println()
log.Infoln(o)
} }
} }