From f7feb7e0fc69138e1f28a20f0953f11f53246428 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Tue, 16 Aug 2022 14:42:04 +0800 Subject: [PATCH] strategy/supertrend: output acc. profit report to tsv file --- pkg/strategy/supertrend/strategy.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index ae169e2bd..5a2f2849b 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -3,6 +3,7 @@ package supertrend import ( "bufio" "context" + "encoding/csv" "fmt" "github.com/c9s/bbgo/pkg/risk" "github.com/fatih/color" @@ -100,6 +101,8 @@ type Strategy struct { lastDayAccumulatedProfit fixedpoint.Value // AccumulatedProfitLastPeriodWindow Last period window of accumulated profit AccumulatedProfitLastPeriodWindow int `json:"accumulatedProfitLastPeriodWindow"` + // AccumulatedProfitReportTsv outputs acc. profit report to specified tsv file + AccumulatedProfitReportTsv string `json:"accumulatedProfitReportTsv"` } func (s *Strategy) ID() string { @@ -307,6 +310,18 @@ func (s *Strategy) PrintResult(o *os.File) { hiyellow(f, "Accumulated Profit %dMA: %f\n", s.AccumulatedProfitMAWindow, s.accumulatedProfitMA.Last()) hiyellow(f, "Last %d day(s) Accumulated Profit: %f\n", s.AccumulatedProfitLastPeriodWindow, s.dailyAccumulatedProfits.Sum()) hiyellow(f, "\n") + + if s.AccumulatedProfitReportTsv != "" { + tf, err := os.OpenFile(s.AccumulatedProfitReportTsv, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + panic(err) + } + defer tf.Close() + csvwiter := csv.NewWriter(tf) + csvwiter.Comma = '\t' + defer csvwiter.Flush() + csvwiter.Write([]string{s.Symbol, s.accumulatedProfit.String(), fmt.Sprintf("%f", s.accumulatedProfitMA.Last()), fmt.Sprintf("%f", s.dailyAccumulatedProfits.Sum())}) + } } func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {