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"`
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"`
Manifests Manifests `json:"manifests,omitempty"`

View File

@ -492,6 +492,8 @@ var BacktestCmd = &cobra.Command{
summaryReport.Symbols = append(summaryReport.Symbols, symbol)
summaryReport.SymbolReports = append(summaryReport.SymbolReports, *symbolReport)
summaryReport.TotalProfit = symbolReport.PnL.Profit
summaryReport.TotalUnrealizedProfit = symbolReport.PnL.UnrealizedProfit
// write report to a file
if generatingReport {
@ -500,18 +502,21 @@ var BacktestCmd = &cobra.Command{
return err
}
}
symbolReport.Print(wantBaseAssetBaseline)
}
}
if generatingReport && reportFileInSubDir {
if generatingReport {
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 {
return err
}
// append report index
if reportFileInSubDir {
if err := backtest.AddReportIndexRun(outputDirectory, backtest.Run{
ID: runID,
Config: userConfig,
@ -520,6 +525,11 @@ var BacktestCmd = &cobra.Command{
return err
}
}
} else {
for _, symbolReport := range summaryReport.SymbolReports {
symbolReport.Print(wantBaseAssetBaseline)
}
}
return nil
},

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")
log.Infof("cmd: %+v", c)
// c.Output()
if e.CombineOutput {
c.Stdout = os.Stdout
c.Stderr = os.Stderr

View File

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