strategy/supertrend: use pkg/data/tsv for tsv output

This commit is contained in:
Andy Cheng 2022-08-16 15:49:08 +08:00
parent f7feb7e0fc
commit 2b638d1f8f
2 changed files with 14 additions and 7 deletions

View File

@ -21,6 +21,15 @@ func NewWriterFile(filename string) (*Writer, error) {
return NewWriter(f), nil
}
func AppendWriterFile(filename string) (*Writer, error) {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
return NewWriter(f), nil
}
func NewWriter(file io.WriteCloser) *Writer {
tsv := csv.NewWriter(file)
tsv.Comma = '\t'

View File

@ -3,8 +3,8 @@ package supertrend
import (
"bufio"
"context"
"encoding/csv"
"fmt"
"github.com/c9s/bbgo/pkg/data/tsv"
"github.com/c9s/bbgo/pkg/risk"
"github.com/fatih/color"
"os"
@ -312,15 +312,13 @@ func (s *Strategy) PrintResult(o *os.File) {
hiyellow(f, "\n")
if s.AccumulatedProfitReportTsv != "" {
tf, err := os.OpenFile(s.AccumulatedProfitReportTsv, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
tsvwiter, err := tsv.AppendWriterFile(s.AccumulatedProfitReportTsv)
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())})
defer tsvwiter.Close()
// Output symbol, total acc. profit, acc. profit 60MA, 7d acc. profit
tsvwiter.Write([]string{s.Symbol, s.accumulatedProfit.String(), fmt.Sprintf("%f", s.accumulatedProfitMA.Last()), fmt.Sprintf("%f", s.dailyAccumulatedProfits.Sum())})
}
}